您的位置:首页 > 其它

HDU 1213 How Many Tables

2012-08-22 10:57 381 查看
题意:一个人不会坐在一个人也不认识的桌子上;

思路:并查集求解,初始桌子数为n ,如果有认识的人归并在一起,桌子数-1。

#include<iostream>
using namespace std;
int father[1005];
int n;
void Init()
{
for(int i=0;i<1001;i++)
father[i]=i;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void merge_set(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
n--;
father[fy]=fx;
}

}
int main()
{
int t,b,m,i,x,y;
cin>>t;
b=t;
while(t--)
{
if(t==(b-1)) getchar();
cin>>n>>m;
Init();
for(i=1;i<=m;i++)
{
cin>>x>>y;
merge_set(x,y);
}
cout<<n<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: