hdu 3018 Ant Trip (欧拉回路之一笔画问题)

Ant Trip

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


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.
hdu 3018 Ant Trip (欧拉回路之一笔画问题)_第1张图片
 

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.
/* 一笔画问题: 每条边过且只过一次,问至少要画几笔才能全部边都经过(不考虑鼓励的点) 
   图有多个集合构成  集合有两种 
   一种为含奇数点的  一种为只含偶数点的
   对于含奇数点的     笔画数=奇数点个数/2 
   对于只含偶数点的   存在欧拉回路  笔画数=1
   对于整张图  则  ans=总奇数点/2+只含偶数点的集合个数
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n,m,ans;
int set[100005],way[100005];  
int mark[100005];                   // 标记集合是否有奇数点(只需对根进行标记即可)
void init(int n)
{
	int i;
	memset(way,0,sizeof(way));
	memset(mark,0,sizeof(mark));        
	for(i=1;i<=n;i++)
		set[i]=i;
}
int find(int x)
{
	while(set[x]!=x) x=set[x];
    return x;
}
void merge(int a,int b)
{
    int x,y;
	x=find(a);
	y=find(b);
	if(x!=y) 
	{
		set[x]=y;
	}
}
void solve()
{
	int i,r;
	ans=0;
	for(i=1;i<=n;i++)
	{
		if(way[i]%2==1)
		{
			r=find(i);
			mark[r]=1;       // 标记集合是含奇数点(对根标记)
			ans++;           // 记录奇数点个数
		}
	}	
	ans/=2;
	for(i=1;i<=n;i++)
	{
		if(way[i]>0)
		{
			r=find(i);
			if(mark[r]==0&&i==r)     // 对根操作  防止加重复
			{
				ans++;
//				printf("i:%d r:%d ans:%d\n",i,r,ans);
			}
		}
	}
}
int main()
{
	int i,j,t,l,r;
	while(~scanf("%d%d",&n,&m))
	{	
		init(n);		
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&l,&r);
			way[l]++;
			way[r]++;
			merge(l,r);
		}
		solve();
		printf("%d\n",ans);
	}
	return 0;
} 


你可能感兴趣的:(hdu 3018 Ant Trip (欧拉回路之一笔画问题))