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 } 
  • 相关阅读:
    python05-循环
    python03-列表
    python03 input
    python02-灭霸的选择
    python学习小记01--萌新的进化
    Linux—-软件安装
    linux-认识与分析日志
    Esxi遇到问题汇总。
    xx
    Pramp mock interview (4th practice): Matrix Spiral Print
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6507394.html
Copyright © 2011-2022 走看看