题目:http://acm.hdu.edu.cn/showproblem.php?pid=1213
Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.4
基础的并查集,数集合个数问题,和畅通工程一样的解题方案(我拿它代码改了下输入格式就A了。。。)
http://blog.csdn.net/lttree/article/details/23820679
#include <stdio.h>
const int MAX=1000;
int father[MAX];
//初始化函数
void Init(int n)
{
int i;
for(i=1;i<=n;i++)
father[i]=i;
}
//查找函数
int Find(int x)
{
while(father[x]!=x)
x=father[x];
return x;
}
//合并函数
void combine(int a,int b)
{
int temp_a,temp_b;
temp_a=Find(a);
temp_b=Find(b);
if(temp_a!=temp_b)
father[temp_a]=temp_b;
}
//确定连通分量个数
int find_ans(int n)
{
int i,sum=0;
for(i=1;i<=n;++i)
if(father[i]==i)
++sum;
return sum;
}
int main()
{
int i,n,m,a,b,test;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
Init(n);
for(i=0;i<m;++i)
{
scanf("%d%d",&a,&b);
combine(a,b);
}
printf("%d\n",find_ans(n));
}
return 0;
}