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 }
  • 相关阅读:
    openpyxl模块的读写使用及mongodb概念
    MongoDB数据库的下载安装及配置方法
    利用while循环写的简单小游戏猜数字
    爬虫之爬取豆瓣top250电影排行榜及爬取斗图啦表情包解读及爬虫知识点补充
    红薯网防爬措施与斗图啦及豆瓣实战案例
    (小知识)结局不会的问题的解决流程
    spring2.5.6 jar包
    Struts2的DMI动态方法调用
    Struts2.1.6 jar包
    Hibernate3.3.2 jar包
  • 原文地址:https://www.cnblogs.com/hss-521/p/7277109.html
Copyright © 2011-2022 走看看