题目
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.
给一个简单无向图,最少几笔可以画完所有边
知识点:笔画数=总奇数点/2+只含偶数点的集合个数
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int h[100005];
int book[100005];
int a[100005];
void f(int n)
{
int i;
for(i=1;i<=n;i++)
a[i]=i;
}
int find(int x)
{
if(x==a[x])
return a[x];
else
{
a[x]=find(a[x]);
return a[x];
}
}
void merge(int n,int m)
{
int t1,t2;
t1=find(n);
t2=find(m);
if(t1!=t2)
{
a[t2]=t1;
}
}
int main()
{
int m,n,i,j,ans,x,y,r;
while(~scanf("%d %d",&n,&m))
{
memset(book,0,sizeof(book));
memset(h,0,sizeof(h));
ans=0;
f(n);
for(i=1;i<=m;i++)
{
scanf("%d %d",&x,&y);
h[x]++;
h[y]++;
merge(x,y);
}
for(i=1;i<=n;i++)
{
if(h[i]%2!=0)
{
r=find(i);
book[r]=1;
ans++;
}
}
ans/=2;
for(i=1;i<=n;i++)
{
if(h[i]>0)
{
r=find(i);
if(!book[r]&&i==r)
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
//一笔画问题: 每条边过且只过一次,问至少要画几笔才能全部边都经过
//ans=总奇数点/2+只含偶数点的集合个数