题目:
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.
OutputFor 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 5Sample Output
2 4
题意:
办酒席,有关系的坐一桌(模板题,直接上代码)
代码:
#include<stdio.h>
using namespace std;
int t,f[1111],n,m;
void intt()
{
for(int i=1;i<=n;i++)
f[i]=i;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
void marge(int x,int y)
{
int t1,t2;
t1=getf(x);
t2=getf(y);
if(t1!=t2)
f[t2]=t1;
return ;
}
int main()
{
int i,j,a,b,sum;
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d %d",&n,&m);
intt();
for(i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
marge(a,b);
}
for(i=1;i<=n;i++)
{
if(f[i]==i)
sum++;
}
printf("%d\n",sum);
}
return 0;
}