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.

    InputThe 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)OutputThe 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.

    大意:

    王先生想让一些男孩帮他做一个项目。因为这个项目相当复杂,男孩越多,情况就越好。当然也有一定的要求。

    王先生选了一个大得足以容纳男孩的房间。未被选中的男孩必须立即离开房间。房间里有10000000个男孩,从一开始就从1到10000000。

    在王先生的选择之后,他们中还有两个人仍然是朋友(直接或间接),或者只剩下一个男孩。给定所有的直接朋友对,你应该决定最好的方式

    分析:

    假设A和B是朋友,B和C是朋友,那么A和C也是朋友(间接),以此类推,找出最大的朋友数,并查集加一个数组标记;
    附代码;

     1 #include<map>
     2 #include<cmath>
     3 #include<queue>
     4 #include<vector>
     5 #include<cstdio>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 int p[1000005],r[1000005];
    11 int find(int x)
    12 {
    13     int r=x;
    14     while (p[r]!=r)
    15         r=p[r ];
    16     int j,i=x;
    17     while(i!=r)
    18     {
    19         j=p[i ];
    20         p[i ]=r;
    21         i=j;
    22     }
    23     return r;
    24 }
    25 int main()
    26 {
    27     int n;
    28     while(~scanf("%d",&n))
    29     {
    30         if(n==0)
    31         {
    32             printf("1
    ");
    33             continue;
    34         }
    35         for(int i=1; i<1000005; i++)
    36         {
    37             p[i]=i;
    38             r[i]=1;
    39         }
    40         int all=0;
    41         for(int i=0; i<n; i++)
    42         {
    43             int f1,f2,p1,p2;
    44             scanf("%d%d",&p1,&p2);
    45             f1=find(p1);
    46             f2=find(p2);
    47             if(f1!=f2)
    48             {
    49                 p[f1]=f2;
    50                 r[f2]+=r[f1];
    51             }
    52             if(all<r[p[p2]])
    53                 all=r[p[p2]];
    54         }
    55         printf("%d
    ",all);
    56     }
    57     return 0;
    58 }

    第一次写博客,请多指教!!!

  • 相关阅读:
    博客园装修 js动态背景效果 一个随鼠标变换的动态线条
    转载:jQuery在线引用地址
    记录下es6的基础笔记
    js、css 、html平时的一些小笔记
    【机器学*】k*邻算法-01
    【Leetcode】718. 最长重复子数组
    【嵌入式】嵌入式系统开发与应用第二版课后答案第三章(田泽)
    【嵌入式】嵌入式系统开发与应用第二版课后答案第二章(田泽)
    【嵌入式】嵌入式系统开发与应用第二版课后答案第一章(田泽)
    【C++】关键字回忆leetcode题解
  • 原文地址:https://www.cnblogs.com/She-Chuan/p/9491766.html
Copyright © 2011-2022 走看看