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 998 HintHint Huge input, scanf is recommended.
#includeusing namespace std; #define maxn 1010 int f[maxn]; void ioin(int n) { int i; for(i=1;i<=n;i++) f[i]=i; } int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } void joint(int a,int b) { int fa,fb; fa=find(a); fb=find(b); if(fa!=fb) f[fa]=fb; } int main() { int n,m; while(cin>>n) { if(n==0) break; cin>>m; ioin(n); int i; for(i=1;i<=m;i++) { int a,b; cin>>a>>b; joint(a,b); } int sum=0; for(i=1;i<=n;i++) if(f[i]==i) sum=sum+1; cout<