http://acm.hdu.edu.cn/showproblem.php?pid=3018
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
思路构造:
本题是让你求这个图需要用几笔画可以画成,应注意的一点,孤立的点不计入笔画中,然后这个图就可以构造连通分支,构造完连通分支后计算每个连通分支需要几笔画可以画成,然后把每个联通分支的结果加起来就是最终的结果了。
下面是AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int pre[100005],zitu[100005],deg[100005];
int fin(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=fin(pre[x]);
}
}
void join(int x,int y)
{
int t1=fin(x);
int t2=fin(y);
if(t1!=t2)
{
pre[t1]=t2;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(deg,0,sizeof(deg));
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
memset(zitu,-1,sizeof(zitu));//假设每个点都可能是连通分支的最高点
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
if(u==v)
{
continue;
}
join(min(u,v),max(u,v));
deg[u]++,deg[v]++;
}
for(int i=1;i<=n;i++)
{
fin(i);
if(zitu[pre[i]]==-1&°[i]>0)
zitu[pre[i]]=0;
if(deg[i]%2)
{
zitu[pre[i]]++;
}
}
int re=0;
for(int i=1;i<=n;i++)
{
if(zitu[i]>0)
{
re+=zitu[i]/2;
}
else if(zitu[i]==0)
{
re++;
}
}
printf("%d\n",re);
}
return 0;
}