Problem 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 ds, too.
Your task is to count how many people there are in the largest group of friends.
Input
Input consists of several datasets. the first line of the input consists of a line with the number of test cases to follow. the first line of each dataset contains tho numbers N and M, where N is the number of town'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 lines consists of two integers A and B (1 ≤ A ≤ n, 1 ≤ B ≤ n, A = B) which describe that A and B are friends. there cocould be repetitions among the given pairs.
Output
The output for each test case shoshould contain one number denoting 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 |
There are n people in a town and M Groups of Friends between them. There is a famous saying: "My friends are also my friends ". Ask the person with the most friends how many friends they can have.
A simple query set:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 30005 4 5 int f[N], s[N]; 6 int a, b, n, m, i, num, ans; 7 8 int _find(int x) 9 {10 if (x == f[x])11 return f[x];12 else13 return _find(f[x]);14 }15 16 int main()17 {18 scanf("%d", &num);19 while(num--){20 ans = 0;21 scanf("%d%d", &n, &m);22 for (i = 1; i <= n; i++){23 f[i] = i;24 s[i] = 1;25 }26 for (i = 0; i < m; i ++){27 scanf("%d%d", &a, &b);28 int pa = _find(a);29 int pb = _find(b);30 if (pa != pb) {31 f[pa] = pb;32 s[pb] += s[pa];33 if(ans < s[pb])34 ans = s[pb];35 }36 }37 printf("%d\n", ans);38 }39 return 0;40 }