Ant Trip
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 860 Accepted Submission(s): 324
Problem DescriptionAnt 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.
InputInput 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.
OutputFor each test case ,output the least groups that needs to form to achieve their goal.
Sample Input
3 31 22 31 34 21 23 4
Sample Output
12HintNew ~~~ 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 sample3 2,tony and his friends must form two group.其實,先用並查集,分成幾個部分,然後,把每個部分的奇點數相加除2就可以,也就是說,分的組數和奇數點的個數的一半相等,不過要注意只有一個點的集合,不能算,要被怱略掉,才行,這一點,我錯了好幾次,
#include <iostream>#include <vector>#include <stdio.h>using namespace std;int down[100005],setnum[100005],in[100005],re[100005],num[100005];bool zh[100005];int fin(int x){ if(down[x]!=x) { down[x]=fin(down[x]); } return down[x];}int main(){ int n,m,k,a,b,i,s,e,j,temp; while(~scanf("%d%d",&n,&m)) { for(i=0;i<=n;i++) { zh[i]=true; num[i]=0; in[i]=0; down[i]=i; } while(m--) { scanf("%d%d",&s,&e); a=fin(s); b=fin(e); if(a!=b) { down[a]=b; } in[s]++; in[e]++; } bool zhe; int sum=0,ans; for(i=1,j=1;i<=n;i++) { temp=fin(i); zh[temp]=false; if(in[i]%2) { num[temp]++; } } for(i=1;i<=n;i++) { if((zh[i]==false)&&(num[i]==0)) { if(in[fin(i)]) sum++; } else if((zh[i]==true)&&(num[i]==0)) { continue; } else { sum+=num[i]/2; } } printf("%d\n",sum); } return 0;}
Source2009 Multi-University Training Contest 12 - Host
by FZU