Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16616 Accepted Submission(s):
8137
#include<stdio.h> int set[2100]; int find(int fa) //找到根节点 { int ch=fa; int t; while(fa!=set[fa]) fa=set[fa]; while(ch!=fa) { t=set[ch]; set[ch]=fa; ch=t; } return fa; } int mix(int x,int y) //合并已有人数 { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) set[fx]=fy; } int main() { int n,m,j,i,table,people,s; scanf("%d",&n); while(n--) { scanf("%d %d",&people,&table); for(i=1;i<=people;i++) set[i]=i; for(i=1;i<=table;i++) { scanf("%d %d",&m,&j); mix(m,j); } s=0; for(i=1;i<=people;i++) { if(set[i]==i) s++; //需要的桌子数 } printf("%d\n",s); } return 0; }
原文:http://www.cnblogs.com/tonghao/p/4445559.html