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

                   More is better



     

    Problem Description
    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.
     

     

    Input
    The 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)
     

     

    Output
    The 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
     
    Hint
    A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
     
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 const int maxn=1000005;
     7 
     8 int bin[maxn];
     9 int cnt[maxn];
    10 
    11 int find(int x)
    12 {
    13     int r=x;
    14     while(bin[r]!=r)
    15     r=bin[r];
    16     int temp=x;
    17     while(temp!=r)
    18     {
    19         int j=bin[temp];
    20         bin[temp]=r;
    21         temp=j;
    22     }
    23     return r;
    24 }
    25 
    26 void merge(int x,int y)
    27 {
    28     int fx=find(x);
    29     int fy=find(y);
    30     if(fx!=fy)
    31     bin[fy]=fx;
    32 }
    33 
    34 int main()
    35 {
    36     //freopen("in.txt","r",stdin);
    37     int a,b,i,n,ans;
    38     while(scanf("%d",&n)!=EOF)
    39     {
    40         ans=0;
    41         memset(cnt,0,sizeof(cnt));
    42         for(i=0;i<maxn;i++)
    43         bin[i]=i;
    44         for(i=0;i<n;i++)
    45         {
    46             scanf("%d%d",&a,&b);
    47             merge(a,b);
    48         }
    49         for(i=1;i<maxn;i++)
    50         {
    51             int temp=find(i);
    52             cnt[temp]++;
    53             ans=max(ans,cnt[temp]);
    54         }
    55         printf("%d
    ",ans);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    spark系列-6、对Application,Driver,Job,Task,Stage的理解
    spark系列-5、RDD、DataFrame、Dataset的区别和各自的优势
    spark系列-4、spark序列化方案、GC对spark性能的影响
    spark系列-2、Spark 核心数据结构:弹性分布式数据集 RDD
    nginx学习(九):跨域配置和防盗链配置
    nginx学习(八):nginx配置gzip
    nginx学习(七):nginx提供静态资源服务
    nginx学习(六):日志切割
    nginx学习(五):nginx.conf 核心配置文件详解
    nginx学习(四):nginx处理web请求机制
  • 原文地址:https://www.cnblogs.com/homura/p/4686914.html
Copyright © 2011-2022 走看看