标签:
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
#include <stdio.h> int b[10001]; int findx(int x) { int r; r=x; while(r!=b[r]) r=b[r]; return r; } void merge(int x,int y) { int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy) b[fx]=fy; } int main() { int x,m,n,y,i,j; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0) break; for(i=1;i<=n;i++) { b[i]=i; } while(m--) { scanf("%d %d",&x,&y); merge(x,y); } j=-1; for(i=1;i<=n;i++) { if(b[i]==i) j++; } printf("%d\n",j); } return 0; }
标签:
原文地址:http://blog.csdn.net/l55iuming/article/details/46402725