zoukankan      html  css  js  c++  java
  • HDU 1865 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.

    并查集...思路很简单...求生成的集合中最大的集合所含的元素个数...

    WA了八百年...最后发现当输入 0 时,男孩没有朋友,应输出 1 .

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #define maxn 10000005;
     5 int fa[maxn], sum[maxn];
     6 int ans;
     7 
     8 int find(int x){
     9     if(fa[x] == x)
    10         return fa[x];
    11     else
    12         return fa[x];
    13 }
    14 
    15 int main(){
    16     int t;
    17     while(scanf("%d", &t) != EOF){
    18         if(t==0){
    19             printf("1
    ");
    20             continue;
    21         }
    22         ans = 0;
    23         for(int i = 1; i < maxn; i++){
    24             sum[i]=1;
    25             fa[i]=i;
    26         }
    27         int x,y;
    28         while(t--){
    29             scanf("%d%d",&x,&y);
    30             int fa_x, fa_y;
    31             fa_x = find( x );
    32             fa_y = find( y );
    33             if(sum[fa_x] < sum[fa_y]){
    34                 fa[fa_x] = fa_y;
    35                 sum[fa_y] += sum[fa_x ];
    36                 ans = fmax(ans, sum[fa_y]);
    37             }
    38             else{
    39                 fa[fa_y] = fa_x;
    40                 sum[fa_x] += sum[fa_y];
    41                 ans = fmax(ans, sum[fa_x]);
    42             }
    43         }
    44         printf("%d
    ",ans);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    Multiple markers at this line
    用递归和位移进行枚举子集合
    Android开发如何双击返回键退出程序
    Android ImageView设置图片原理(下)
    北大OJ百练——2721:忽略大小写比较字符串大小
    场景示例 Nginx 访问日志
    2 插件管理
    第一章 入门示例
    zabbix 通过gateway 获取远程主机的JMX信息
    zabbix 通过gateway 获取远程主机的JMX信息
  • 原文地址:https://www.cnblogs.com/zzy9669/p/3875292.html
Copyright © 2011-2022 走看看