首页 > 其他 > 详细

Problem D UVA 10608

时间:2014-07-27 22:31:29      阅读:403      评论:0      收藏:0      [点我收藏+]

 

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

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.

 

Output

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

 

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov

 

又是并查集

比赛的时候看到又混乱了

其实很简单

 

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int fa[30001];
 5 int visit[30001];
 6 int ans,m,n;
 7 
 8 void init()
 9 {
10     for(int i =0;i<=n;i++)
11     {
12         fa[i]=i;
13         visit[i]=0;
14     }
15     ans = 0;
16 }
17 
18 int find(int x)
19 {
20     while(x!=fa[x])
21         x = fa[x];
22 
23     return x;
24 }
25 
26 
27 void merge(int x,int y)
28 {
29     int a = find(x);
30     int b = find(y);
31     if(a!=b)
32     {
33         fa[a] = b;
34     }
35 }
36 
37 int main()
38 {
39     cin.sync_with_stdio(false);
40     int T;
41     cin>>T;
42     while(T--)
43     {
44         cin>>n>>m;
45         init();
46         while(m--)
47         {
48             int x,y;
49             cin>>x>>y;
50             merge(x,y);
51         }
52 
53         for(int i =0;i<=n;i++)
54         {
55             visit[find(i)]++;
56         }
57 
58         for(int i =0;i<=n;i++)
59         {
60             ans=ans>visit[i]?ans:visit[i];
61         }
62         cout<<ans<<endl;
63     }
64 
65     return 0;
66 }

 

Problem D UVA 10608,布布扣,bubuko.com

Problem D UVA 10608

原文:http://www.cnblogs.com/Run-dream/p/3871521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!