您的位置:首页 > 其它

hdoj 1213.How Many Tables

2012-11-28 21:22 141 查看
How Many Tables

#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=1000+10;
int n,father[MAXN];
void init(){
for(int i=1;i<=n;i++){
father[i]=i;
}
}
int getfather(int a){
int r;
r=a;
while(r!=father[r]){
r=father[r];
}
int i=a;
while(i!=father[i]){
i=father[i];
father[i]=r;
}
return r;
/*
if(a!=father[a]){
father[a]=getfather(father[a]);
}
return a;
*/
}
void Union(int a,int b){
int fx=getfather(a);
int fy=getfather(b);
if(fx!=fy){
father[fx]=fy;
}
}
int main()
{
int ct,m;
scanf("%d",&ct);
for(int i=0;i<ct;i++){
int cnt=0;
scanf("%d %d",&n,&m);
init();
for(int j=0;j<m;j++){
int a,b;
scanf("%d %d",&a,&b);
Union(a,b);
}
for(int k=1;k<=n;k++){
if(k==father[k]){
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: