Description
There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends and B and C are friends then A and C are friends, too.
Your task is to count how many people there are in the largest group of friends.3 7
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 30030;
struct Fri
{
int f, num;
} father[MAXN];
int ran[MAXN];
int fin(int v)
{
return father[v].f = father[v].f == v ? v : fin(father[v].f);
}
void merg(int x, int y)
{
int a = fin(x), b = fin(y);
if( a == b )
return;
if( ran[a] < ran[b])
{
father[a].f = b;
father[b].num += father[a].num;
}
else
{
father[b].f = a;
father[a].num += father[b].num;
if(ran[b] == ran[a])
{
++ran[a];
}
}
}
int main()
{
int t, x, y, a, b, ans = 0, n, m;
cin >> t;
while(t--)
{
ans = 0;
memset(father, 0, sizeof(father));
memset(ran, 0, sizeof(ran));
cin >> n >> m;
for( int i=0; i<n; i++ )
{
father[i].f = i;
father[i].num = 1;
}
for( int i = 0; i<m; i++ )
{
cin >> a >> b;
merg(a, b);
x = fin(a);
y = fin(b);
ans = max(ans, max(father[x].num, father[y].num));
}
cout << ans << endl;
}
return 0;
}
最后我才发现我是看错题了,citizens都会默了///