Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3524 Accepted Submission(s):
1393
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.
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,m,tot,ans; int vis[100010]; int fa[100010],num[100010]; int into[100010],du[100010]; int find(int x){ if(fa[x]==x) return fa[x]; else return fa[x]=find(fa[x]); } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ tot=0;ans=0; memset(du,0,sizeof(du)); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); memset(into,0,sizeof(vis)); for(int i=1;i<=n;i++) fa[i]=i; for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); into[x]++;into[y]++; int dx=find(x);int dy=find(y); if(dx!=dy) fa[dy]=dx; } for(int i=1;i<=n;i++){ int now=find(i); if(!vis[now]){ vis[now]=1; num[++tot]=now; } if(into[i]%2!=0) du[now]++; } for(int i=1;i<=tot;i++){ if(into[num[i]]==0) continue; if(du[num[i]]==0) ans++; ans+=du[num[i]]/2; } cout<<ans<<endl; } }