How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1131 Accepted Submission(s): 476
Total Submission(s): 1131 Accepted Submission(s): 476
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.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2 5 3 1 2 2 3 4 5 5 1 2 5
Sample Output
2 4
Author
Ignatius.L
Source
Recommend
Eddy
//1254986 2009-04-10 18:00:00 Accepted 1213 15MS 180K 640 B C++ Wpl
//这个是最简单的并查集;
#include <iostream>
#define MAX 1001
using namespace std;
int un[MAX],t,n,m;
void Init()
{
int i;
for(i=1;i<=n;i++)
un[i]=i; //初始化每个节点都是一棵独立的树
}
int Find(int x) //查找每个元素所属的集合好选择是不是要并如果集合号相同的话,就证明他们是一个集合的(有回路)
{
return un[x];
}
void Merge(int a,int b)//时间复杂度O(N) //所以如果输入的元素对数过多就不能用这个并查集了
{
int mmax,mmin,i,temp;
mmax=Find(a);
mmin=Find(b);
if(mmax==mmin) //如果原本是一个集合,那么就不需要并了,这里也可以证明是否形成回路
return;
if(mmin>mmax)
{
temp=mmin;
mmin=mmax;
mmax=temp;
}
for(i=1;i<=n;i++) //这里选择每次将集合号大的并到集合号小的上面
{
if(un[i]==mmax)
un[i]=mmin;
}
}
int main()
{
int a,b,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
Init();
while(m--)
{
scanf("%d%d",&a,&b);
Merge(a,b);
}
int sum=0;
for(i=1;i<=n;i++)
{
if(un[i]==i)
sum++;
}
printf("%d\n",sum);
}
return 0;
}
//这个是最简单的并查集;
#include <iostream>
#define MAX 1001
using namespace std;
int un[MAX],t,n,m;
void Init()
{
int i;
for(i=1;i<=n;i++)
un[i]=i; //初始化每个节点都是一棵独立的树
}
int Find(int x) //查找每个元素所属的集合好选择是不是要并如果集合号相同的话,就证明他们是一个集合的(有回路)
{
return un[x];
}
void Merge(int a,int b)//时间复杂度O(N) //所以如果输入的元素对数过多就不能用这个并查集了
{
int mmax,mmin,i,temp;
mmax=Find(a);
mmin=Find(b);
if(mmax==mmin) //如果原本是一个集合,那么就不需要并了,这里也可以证明是否形成回路
return;
if(mmin>mmax)
{
temp=mmin;
mmin=mmax;
mmax=temp;
}
for(i=1;i<=n;i++) //这里选择每次将集合号大的并到集合号小的上面
{
if(un[i]==mmax)
un[i]=mmin;
}
}
int main()
{
int a,b,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
Init();
while(m--)
{
scanf("%d%d",&a,&b);
Merge(a,b);
}
int sum=0;
for(i=1;i<=n;i++)
{
if(un[i]==i)
sum++;
}
printf("%d\n",sum);
}
return 0;
}