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 } 
  • 相关阅读:
    保证测试通过的ip正则,antdIP/IP段的校验方法,antd的textArea中可以输入多个以换行分隔的ip/IP段,并自动检测出错行的原因
    TP5接口出错只能返回500
    UDP服务只能本机访问问题
    有出现了找半天的小BUG
    PHP本地安装redis扩展
    MYSQL数据库和es数据库同步
    QQ互联应用申请失败
    axios跨域问题解决
    elastic和kibana安装心得
    自增运算符理解
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6507394.html
Copyright © 2011-2022 走看看