zoukankan      html  css  js  c++  java
  • More is better——并查集求最大集合(王道)

    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 <iostream>
     2 #include<cstdio>
     3 #include<string.h>
     4 using namespace std;
     5 #define N 100
     6 
     7 int Tree[N];
     8 int findRoot(int x){//查找x的根节点
     9     if(Tree[x]==-1)
    10         return x;
    11     else{
    12         int temp=findRoot(Tree[x]);
    13         Tree[x]=temp;
    14         return temp;
    15     }
    16 }
    17 
    18 
    19 int main()
    20 {
    21     int sum[N];//结点i为根的树的节点数
    22     int n;
    23     scanf("%d",&n);
    24     //初始化
    25     for(int i=1;i<N;i++){
    26         Tree[i]=-1;
    27         sum[i]=1;
    28     }
    29 
    30     while(n--){
    31         int a,b;
    32         scanf("%d %d",&a,&b);
    33         a=findRoot(a);//找到a所在树的根节点
    34         b=findRoot(b);//找到b所在树的根节点
    35         if(a!=b){//ab不在一棵树上
    36             Tree[a]=b;//a树连接到b树上
    37             sum[b]+=sum[a];//以b为根节点的树上节点数添加a上节点的数目
    38         }
    39     }
    40     int ans=1;
    41     for(int i=1;i<N;i++){
    42         if(Tree[i]==-1 && sum[i]>ans)//找到最大值
    43             ans=sum[i];
    44     }
    45     printf("%d",ans);
    46     return 0;
    47 }

    这里特别说明一下,王道上面的答案有问题,问题出在第42行,因为N=100,而 i 作为数组的下标,应该是0-99,100越界了,如果按照王道上面的做法,最后答案会成100.

  • 相关阅读:
    基于文件数据库的规则引擎处理海量高复杂度数据(二,解决方案)
    内存数据库内核开发 工作日志(innodb的原理,算法详细剖析)(九)
    swift检测字符串是否在数组字符串中
    swift cell自定义左滑手势处理
    rxswift cell按钮绑定的重用问题
    swift代码统一编码规范
    TZImagePickerController获取原图
    swift 地区选择器选中数据操作
    iOS是否审核需要关闭一些操作
    项目概要评审
  • 原文地址:https://www.cnblogs.com/xym4869/p/8602693.html
Copyright © 2011-2022 走看看