hdu--1856 More is better

Posted ⊙∽⊙Perseverance

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu--1856 More is better相关的知识,希望对你有一定的参考价值。

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<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int num[1000000],par[1000000];
 6 void init()
 7 {
 8     for(int i=1;i<1000000;i++)
 9      { par[i]=i;
10        num[i]=1;    
11      }
12 }
13 int find(int x)
14 {
15     if(par[x]!=x)
16       return par[x]=find(par[x]);
17     return x;
18     
19 }
20 void unite(int a,int b)
21 {
22     int fa=find(a);
23     int fb=find(b);
24     if(fa!=fb)
25     {par[fa]=fb;
26     num[fb]+=num[fa];  //这个地方注意,第一次num【fb】写成了b,
27         
28     }
29 }
30 int main()
31 {
32     int a,b,n; 
33     while(scanf("%d",&n)!=EOF)
34     {if(n==0) 
35       {printf("1\n");
36        continue;
37           
38       } 
39      init();
40      int max=0;
41      while(n--)
42       {
43        scanf("%d %d",&a,&b);
44        if(a>max) max=a;
45        if(b>max) max=b;    
46        unite(a,b);
47       }
48       int m=0;
49      for(int i=1;i<=max;i++)
50        {if(num[i]>m)
51           m=num[i];
52            
53        } 
54      printf("%d\n",m);  
55      
56      
57         
58     }
59     return 0;
60 }

 

以上是关于hdu--1856 More is better的主要内容,如果未能解决你的问题,请参考以下文章

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