HDU 1856 More is better

               More is better



 

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<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=1000005;
 7 
 8 int bin[maxn];
 9 int cnt[maxn];
10 
11 int find(int x)
12 {
13     int r=x;
14     while(bin[r]!=r)
15     r=bin[r];
16     int temp=x;
17     while(temp!=r)
18     {
19         int j=bin[temp];
20         bin[temp]=r;
21         temp=j;
22     }
23     return r;
24 }
25 
26 void merge(int x,int y)
27 {
28     int fx=find(x);
29     int fy=find(y);
30     if(fx!=fy)
31     bin[fy]=fx;
32 }
33 
34 int main()
35 {
36     //freopen("in.txt","r",stdin);
37     int a,b,i,n,ans;
38     while(scanf("%d",&n)!=EOF)
39     {
40         ans=0;
41         memset(cnt,0,sizeof(cnt));
42         for(i=0;i<maxn;i++)
43         bin[i]=i;
44         for(i=0;i<n;i++)
45         {
46             scanf("%d%d",&a,&b);
47             merge(a,b);
48         }
49         for(i=1;i<maxn;i++)
50         {
51             int temp=find(i);
52             cnt[temp]++;
53             ans=max(ans,cnt[temp]);
54         }
55         printf("%d\n",ans);
56     }
57     return 0;
58 }

 

posted on 2015-07-29 18:59    阅读(126)  评论(0编辑  收藏  举报

导航