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 }
  • 相关阅读:
    首页下拉广告代码
    索引 CREATE INDEX
    苏三起解 故事
    日期正则表达式
    除去非空字符串函数
    数据库什么是字段应该设置索引
    警务通
    动态生成缩略图
    超级实用且不花哨的js代码大全
    无线警务移动办公系统解决方案
  • 原文地址:https://www.cnblogs.com/homura/p/4686914.html
Copyright © 2011-2022 走看看