how many tables
#include<stdio.h> #include<stdlib.h> #include<string.h> int father[1100]; int find(int x) { return x==father[x]?x:father[x]=find(father[x]); } void merge(int x,int y) { x=find(x),y=find(y); if(x!=y) father[x]=y; } int main( ) { int P,N,M,d[1100],count,i,j,a,b; scanf("%d",&P); while(P--) { scanf("%d%d",&N,&M); count=0; memset(d,0,sizeof(d)); for(i=1;i<=1100;i++) father[i]=i; for(i=1;i<=M;i++) { scanf("%d%d",&a,&b); if(find(a)!=find(b)) merge(a,b); } for(i=1;i<=N;i++) d[find(i)]++; for(i=1;i<=N;i++) if(d[i]!=0) count++; printf("%d\n",count); } return 0; }
posted on 2011-04-30 22:26 more think, more gains 阅读(141) 评论(0) 编辑 收藏 举报