首页 > 其他 > 详细

More is better HDU - 1856 ?

时间:2017-03-06 00:50:18      阅读:175      评论:0      收藏:0      [点我收藏+]

      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.
InputThe 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)OutputThe 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

值得注意的是要用一个数组记录每棵树的秩!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define MAX 10000005
 6 using namespace std;
 7 
 8 int T,n,m;
 9 int fa[MAX],record[MAX];
10 
11 int find(int u)
12 {  
13     if(u!=fa[u]) fa[u]=find(fa[u]); 
14     return fa[u];
15 }
16 
17 void Union(int a,int b)
18 {   int x=find(a);
19     int y=find(b);
20     if(x==y) return;
21     else 
22     {   fa[y]=x;
23         record[x]+=record[y];
24     }    
25 }
26 
27 int main()
28 {   
29     while(cin>>T){
30     
31     if(T==0) cout<<"1"<<endl;
32     else{
33           for(int j=1;j<=MAX;j++) 
34           {   fa[j]=j;
35               record[j]=1;
36           }   
37           for(int i=0;i<T;i++)
38           {   scanf("%d%d",&n,&m);
39               Union(n,m);
40           }  
41           int ans=0; 
42           for(int i=1;i<MAX;i++)
43           {   if(ans<record[i]) ans=record[i];
44           }
45           cout<<ans<<endl;
46       }
47   }
48 } 

 

More is better HDU - 1856 ?

原文:http://www.cnblogs.com/zgglj-com/p/6507394.html

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