标签:des style blog color strong io for div
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.
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 could be repetitions among the given pairs.
The output for each test case should 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 |
题意:一个镇上有N个人,他们之间有M组朋友关系。有一句名言说:“我朋友的朋友也是我的朋友”。问朋友最多的那个人能有多少朋友。
一个简单的并查集:
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 else 13 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 }
UVA 10608 FRIENDS,布布扣,bubuko.com
标签:des style blog color strong io for div
原文地址:http://www.cnblogs.com/zzy9669/p/3871473.html