FRIENDS
There is a town with N citizens. It is known that somepairs of people are friends. According to the famous saying that “The friendsof my friends are my friends, too” it follows that if A and B are friends and Band C are friends then A and C are friends, too.
Your task is to count how many people there are in thelargest group of friends.
Input
Input consists of several datasets. The first line of theinput consists of a line with the number of test cases to follow. The firstline of each dataset contains tho numbers N and M, where N is the number oftown's citizens (1≤N≤30000) and M is the number of pairs of people(0≤M≤500000), which are known to be friends. Each of the following M linesconsists of two integers A and B (1≤A≤N, 1≤B≤N, A≠B) which describe that A andB are friends. There could be repetitions among the given pairs.
Output
The output for each test case should contain one numberdenoting how many people there are in the largest group of friends.
Sample Input | Sample Output |
2 3 2 1 2 2 3 10 12 1 2 3 1 3 4 5 4 3 5 4 6 5 2 2 1 7 10 1 2 9 10 8 9 | 3 6 |
#include<stdio.h>
#include<string.h>
int parent[30002];
int team[30002];
int find(int x)
{
return parent[x]==x?x:parent[x]=find(parent[x]);
}
int main()
{
int t,i,n,m,x,y,sum;
scanf("%d",&t);
while(t--)
{
sum=1;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
parent[i]=i;
team[i]=1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x=find(x);
y=find(y);
if(x!=y){
parent[x]=y;
team[y]+=team[x];
}
if(team[y]>sum) sum=team[y];
}
printf("%d\n",sum);
}
return 0;
}