标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 39291 Accepted Submission(s): 20852
#include <stdio.h> int s[1005]; int find(int x) { if(x!=s[x]) s[x]=find(s[x]); return s[x]; /*int r; r=x; while(r!=s[r]) r=s[r]; return r; 对的*/ /*if(x!=s[x]) x=s[x]; return x; 错的*/ } void hebin(int x,int y) { x=find(x); y=find(y); if(x!=y) s[x]=y; } int main() { int i,x,n,m,y,temp; while(scanf("%d",&n),n) { for(i=1;i<=n;i++) s[i]=i; temp=0; scanf("%d",&m); while(m--) { scanf("%d %d",&x,&y); hebin(x,y); } for(i=1;i<=n;i++) if(s[i]==i) temp++; printf("%d\n",temp-1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangfengnick/p/4871993.html