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

    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 int num[1000000],par[1000000];
     6 void init()
     7 {
     8     for(int i=1;i<1000000;i++)
     9      { par[i]=i;
    10        num[i]=1;    
    11      }
    12 }
    13 int find(int x)
    14 {
    15     if(par[x]!=x)
    16       return par[x]=find(par[x]);
    17     return x;
    18     
    19 }
    20 void unite(int a,int b)
    21 {
    22     int fa=find(a);
    23     int fb=find(b);
    24     if(fa!=fb)
    25     {par[fa]=fb;
    26     num[fb]+=num[fa];  //这个地方注意,第一次num【fb】写成了b,
    27         
    28     }
    29 }
    30 int main()
    31 {
    32     int a,b,n; 
    33     while(scanf("%d",&n)!=EOF)
    34     {if(n==0) 
    35       {printf("1
    ");
    36        continue;
    37           
    38       } 
    39      init();
    40      int max=0;
    41      while(n--)
    42       {
    43        scanf("%d %d",&a,&b);
    44        if(a>max) max=a;
    45        if(b>max) max=b;    
    46        unite(a,b);
    47       }
    48       int m=0;
    49      for(int i=1;i<=max;i++)
    50        {if(num[i]>m)
    51           m=num[i];
    52            
    53        } 
    54      printf("%d
    ",m);  
    55      
    56      
    57         
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    linux Mint 安装apache2
    linux Mint 安装tomcat8
    linux Mint wine安装qq,桌面快捷键配置
    linux Mint mysql 安装
    卸载linux Mint自带jdk并安装最新jdk
    linux Mint截图软件 Shutter
    linux git安装及配置(包括更新)
    linux安装wine
    百度地图用ip获取当前位置的经纬度(高精度)
    mysql 索引和视图
  • 原文地址:https://www.cnblogs.com/hss-521/p/7277109.html
Copyright © 2011-2022 走看看