题目1445:How Many Tables
#include <cstdio>
#define MAX 1005
int tree[MAX];int findRoot(int x){if(tree[x]==-1)return x;else{int temp=findRoot(tree[x]);tree[x]=temp;return temp;}
}int main(){int t;while(scanf("%d",&t)!=EOF){int n,m;while(t--){for(int i=0;i<MAX;i++){tree[i]=-1;}scanf("%d%d",&n,&m);int a,b;while(m--){scanf("%d%d",&a,&b);a=findRoot(a);b=findRoot(b);if(a!=b)tree[a]=b; }int ans=0;for(int i=1;i<=n;i++){if(tree[i]==-1)ans++;}printf("%d\n",ans);}}return 0;
}