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 } 
  • 相关阅读:
    P3822 [NOI2017]整数
    P4630 [APIO2018] Duathlon 铁人两项
    P3230 [HNOI2013]比赛
    P2570 [ZJOI2010]贪吃的老鼠
    P4576 [CQOI2013]棋盘游戏
    P3256 [JLOI2013]赛车
    P3297 [SDOI2013]逃考
    CF487E Tourists
    设置一个双色球脚本(2)并带颜色输出
    设置一个双色球脚本
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6507394.html
Copyright © 2011-2022 走看看