HDU 3018 Ant Trip 一筆畫問題

Ant Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2560    Accepted Submission(s): 1009


Problem Description
Ant Country consist of N towns.There are M roads connecting the towns.

Ant Tony,together with his friends,wants to go through every part of the country.

They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.
 

Input
Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
 

Output
For each test case ,output the least groups that needs to form to achieve their goal.
 

Sample Input
3 3 1 2 2 3 1 3 4 2 1 2 3 4
 

Sample Output
1 2
Hint
New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town. In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3. In sample 2,tony and his friends must form two group.
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3013 3015 3016 3011 3010 

    題意:一筆畫問題,求出最少需要畫幾筆能夠遍歷所有的點。

    分析:對於一筆畫問題,對於一個連通塊,除了歐拉回路的情況,其他的情況需要的筆數都是 連通塊奇數度的點的個數/2 。根據此定理可以先用並查集判斷連通塊,注意此題終點的孤立點是不需要計數的,此時需要添加vis數組,只要有一條邊的起點或者終點含有則說明其不是孤立點。在判斷完連通塊後可以掃描一遍奇數度數的點並計數,此時需要一個mark數組標記它的那一塊的最初始元素爲訪問,爲下一遍掃描時統計歐拉回路個數做好準備。在統計完奇數度點的個數cnt後,res+=cnt/2。下一次循環時尋找出歐拉回路的個數即可。見AC代碼:

//一筆畫問題
//歐拉回路或者歐拉路的話都可以一筆畫
//一個連通塊想要遍歷所有的邊需要 奇度個數/2 筆
#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
const int maxn=1e5+5;
int pre[maxn];
int io[maxn];
int vis[maxn];
int used[maxn];
int mark[maxn];
set<int> s;
int find(int x)
{
	int r=x;
	while (pre[r]!=r)
		r=pre[r];
	int i=x,j ;
	while( i != r )
	{
		j=pre[i];
		pre[i]=r ;
		i=j;
	}
	return r ;
}
void join(int x,int y)
{
	int fx=find(x),fy=find(y);
	if(fx!=fy)
		pre[fx]=fy;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{

		for(int i=0; i<=n; i++)
			pre[i]=i;
		for(int i=0; i<=n; i++)
		{
			io[i]=0;
			vis[i]=0;
			used[i]=0;
		}
		int u,v;
		while(m--)
		{
			scanf("%d%d",&u,&v);
			io[u]++;
			io[v]++;
			vis[u]=1;//標記其訪問過  則說明它不是孤立點
			vis[v]=1;
			join(u,v);
		}
		for(int i=1; i<=n; i++)
			if(vis[i])
				s.insert(find(i));
		int res=0,cnt=0;
		for(int i=1; i<=n; i++)
		{
			if(io[i]%2==1&&vis[i])
			{
				if(!mark[find(i)])
					mark[find(i)]=1;//標記已經處理過的連通塊
				cnt++;              //奇度點的個數
				used[i]=1;		//優化  在下一輪判斷歐拉回路的時候不遍歷已經遍歷過的點
			}
		}
		res+=cnt/2;
		for(int i=1; i<=n; i++)
		{
			if(vis[i]&&!mark[find(i)]&&!used[i])//此時得到的都是 歐拉回路  能夠一筆畫的情況
			{
				res++;
				mark[find(i)]=1;
			}
		}
		printf("%d\n",res);
		//s.clear();
		/*set<int>::iterator it;
		for(it =s.begin(); it!=s.end(); it++)//兩重循環會超時
		{
			int cnt=0;
			for(int j=1; j<=n; j++)
				if(find(j)==(*it)&&vis[j]&&io[j]%2==1&&!used[j])
				{
					cnt++;
					used[j]=1;
				}

			if(cnt==1||cnt==0)
				res++;
			else
				res+=cnt/2;
		}
		printf("%d\n",res);*/
	}
}
    剛開始寫的時候,將不同的連通塊放入了set,再二重循環遍歷判斷個數時因爲數據量很大,所以T了。看來滿足於得出結果還是太天真。

    特記下,以備後日回顧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章