More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了More is better(hdu 1856 计算并查集集合中元素个数最多的集合)相关的知识,希望对你有一定的参考价值。

More is better 

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 21167    Accepted Submission(s): 7720


Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang‘s selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 

 

Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 

 

Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep. 
 

 

Sample Input

4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8

 

 

Sample Output
4
2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <set>
 6 #include <cmath>
 7 using namespace std;
 8 #define Max  10000000+5
 9 int per[Max];
10 int ans[Max];
11 int num[Max];
12 bool vis[Max];
13 int n;
14 void init()
15 {
16     for(int i=1;i<Max;i++)
17         per[i]=i;
18 }
19 int find(int a)
20 {
21     if(a==per[a])
22         return a;
23     return per[a]=find(per[a]);
24 }
25 int unite(int a,int b)
26 {
27     a=find(a);
28     b=find(b);
29     if(a!=b)
30         per[a]=b;
31     return 0;
32 }
33 int main()
34 {
35     int t,a,b;
36     freopen("in.txt","r",stdin);
37     while(scanf("%d",&n)!=EOF)
38     {
39         init();
40         int Maxn=1,p=0;
41         memset(vis,0,sizeof(vis));
42         fill(ans,ans+Max,0);
43         for(int i=0;i<n;i++)
44         {
45             scanf("%d%d",&a,&b);
46             if(vis[a]==0)
47             {
48                 vis[a]=1;
49                 num[p++]=a;
50             }
51             if(vis[b]==0)
52             {
53                 vis[b]=1;
54                 num[p++]=b;
55             }
56             unite(a,b);
57         }
58         for(int i=0;i<p;i++)
59         {
60             t=find(num[i]);
61             ans[t]++;
62             //cout<<t<<endl;
63             Maxn=max(Maxn,ans[t]);
64         }
65         printf("%d\n",Maxn);
66     }
67 }

 

 

以上是关于More is better(hdu 1856 计算并查集集合中元素个数最多的集合)的主要内容,如果未能解决你的问题,请参考以下文章

More is better HDU - 1856 ?

hdu 1856 More is better(并查集)

hdu1856 More is better (并查集)

HDU 1856 More is better

HDU 1856More is better (裸并查集+记录秩)

[HDU - 1856]More is better