Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2960 Accepted Submission(s): 1188
1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #include<vector> 5 using namespace std; 6 #define N 100006 7 int fa[N]; 8 int du[N]; 9 int vis[N]; 10 int ji[N]; 11 vector<int> q; 12 void init(int n) 13 { 14 for(int i=0;i<=n;i++) 15 { 16 fa[i]=i; 17 } 18 memset(du,0,sizeof(du)); 19 memset(vis,0,sizeof(vis)); 20 memset(ji,0,sizeof(ji)); 21 q.clear(); 22 } 23 int myfind(int x) 24 { 25 if(fa[x]!=x) 26 { 27 fa[x]=myfind(fa[x]); 28 } 29 return fa[x]; 30 } 31 void hebing(int x,int y) 32 { 33 x=myfind(x); 34 y=myfind(y); 35 if(x!=y) 36 { 37 fa[y]=x; 38 } 39 } 40 int main() 41 { 42 int n,m; 43 while(scanf("%d%d",&n,&m)!=EOF) 44 { 45 int a,b; 46 init(n); 47 int ans=0; 48 for(int i=0;i<m;i++) 49 { 50 scanf("%d%d",&a,&b); 51 hebing(a,b); 52 du[a]++; 53 du[b]++; 54 } 55 for(int i=1;i<=n;i++) 56 { 57 int f=myfind(i); 58 if(!vis[f]) 59 { 60 vis[f]=1; 61 q.push_back(f); 62 } 63 if(du[i]%2==1) 64 { 65 ji[f]++; 66 } 67 } 68 for(int i=0;i<int(q.size());i++) 69 { 70 int f=q[i]; 71 if(du[f]==0)continue; 72 if(ji[f]==0)ans++; 73 else ans+=ji[f]/2; 74 } 75 printf("%d\n",ans); 76 } 77 return 0; 78 }
原文:http://www.cnblogs.com/xxjnoi/p/7193589.html