zoukankan      html  css  js  c++  java
  • More is better HDU

          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 } 
  • 相关阅读:
    2017-3-28 javaScript DOM 操作
    2017-3-28 JavaScript 基础、语法
    2017-3-25 CSS 样式表(二)
    js判断文件是否存在的方法
    关于eclipse添加自动查找文件以及svn的插件
    关于jqgrid的一些使用
    ztree框架使用问题汇总
    bootstrap框架的使用
    UIButton 设置阴影效果
    xcode升级至9.0之后,新建xib报错: Safe Area Layout Guide Before IOS 9.0
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6507394.html
Copyright © 2011-2022 走看看