Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30434 Accepted Submission(s): 15993
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998Huge input, scanf is recommended.HintHint
<span style="font-size:14px;">#include<stdio.h> int bin[1002]; int findx(int x) { int r=x; if(x!=bin[x]) r=findx(bin[x]); return r; } void merge(int a,int b) { int fx=findx(a); int fy=findx(b); if(fx!=fy) bin[fx]=fy; } int main() { int i,n,m,a,b,count; while(~scanf("%d",&n),n) { for(i=1;i<=n;i++) bin[i]=i; for(scanf("%d",&m);m>0;m--) { scanf("%d%d",&a,&b); merge(a,b); } int count=-1; for(i=1;i<=n;i++) if(bin[i]==i) count++; printf("%d\n",count); } return 0; }</span>
原文:http://blog.csdn.net/ice_alone/article/details/38382233