您的位置:首页 > 其它

hdu 1213 How Many Tables

2012-09-01 20:13 357 查看
#include<iostream>
using namespace std;
int father[1005];
int ff(int y)
{
int t=father[y];
while(t!=father[t])
{
t=father[t];
}
return t;
}
void result(int x,int y)
{
int xx=ff(x);
int yy=ff(y);
if(xx!=yy)
{
father[xx]=yy;
}
}
int main()
{
int t;
while(cin>>t)
{
for(int i=1;i<=t;i++)
{
int n,m,x,y;
int sum=0;
cin>>n>>m;
for(int j=1;j<=n;j++)
{
father[j]=j;
}
for(int j=1;j<=m;j++)
{
cin>>x>>y;
result(x,y);
}
for(int j=1;j<=n;j++)
{
if(father[j]==j) sum++;
}
cout<<sum<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: