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 }
  • 相关阅读:
    2.monkey的使用
    1.monkey的安装及环境配置
    3.SQL注入系列二
    2.SQL注入系列一
    1.Appscan工具的使用
    二十二.jmeter的架构和loadrunner原理一样,都是通过中间代理,监控和收集并发客户端发出的指令,把他们生成脚本,再发送到应用服务器,再监控服务器反馈结果
    二十一.HTTP属性管理
    二十. StringFromFile与counter函数
    十九.jmeter函数---csvRead( )
    十八.jmete java工程测试
  • 原文地址:https://www.cnblogs.com/homura/p/4686914.html
Copyright © 2011-2022 走看看