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

                                 More is better

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
    Total Submission(s): 720    Accepted Submission(s): 264


    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.
     

    Author
    lxlcrystal@TJU
     

    Source
     

    Recommend
    lcy
     

    Statistic | Submit | Back
    //1256387 2009-04-10 22:20:57 Wrong Answer 1856 250MS 78532K 804 B C++ Wpl 
    //1256393 2009-04-10 22:21:44 Accepted 1856 250MS 78532K 880 B C++ Wpl 
    //改变的并查集,实际上就是kruskal求最小生成树的思想
    #include <iostream>
    #define MAX 10000002  //因为这个题目内存限制是102400 K ,所以开了这么大的数组
    using namespace std;
    struct node
    {
     
    int parent;
     
    int w;
    }data[MAX];
    int MM;
    void Init()
    {
     
    int i;
     
    for(i=1;i<MAX;i++)
     {
      data[i].parent
    =-1;  //这里利用的是kruskal的思想,这里父亲结点全初始化为-1
      data[i].w=1;
     }
    }
    int Find(int x)  //这里是用来查找根的,就是当前这个x的根节点
    {
     
    int i=x;
     
    while(data[i].parent>0)
      i
    =data[i].parent;
     
    return i;
    }
    void Merge(int a,int b)
    {
     
    if(data[a].w>data[b].w)
     {
      data[a].w
    +=data[b].w;
      data[b].parent
    =a;
      
    if(data[a].w>MM)  //这里用来统计哪个集合的元素个数最多
       MM=data[a].w;
     }
     
    else
     {
      data[b].w
    +=data[a].w;
      data[a].parent
    =b;
      
    if(data[b].w>MM)
       MM
    =data[b].w;
     }
    }
    int main()
    {
     
    int n;
     
    while(scanf("%d",&n)!=EOF)
     {
      MM
    =1;
      
    int a,b,x,y;
      Init();
      
    while(n--)
      {
       scanf(
    "%d%d",&a,&b);
       x
    =Find(a);
       y
    =Find(b);
       
    if(x!=y)
        Merge(x,y);
      }
      printf(
    "%d\n",MM);
     }
     
    return 0;
    }
  • 相关阅读:
    openfiler的介绍与使用
    linux磁盘修复(home或者其他目录无法进入,系统进入emergency mode)
    安装centos系统时出现dracut-initqueue time out 报错
    Linux中sz与rz命令
    /etc/passwd与/etc/shadow
    两台设备做DNS主从同步部署(含正向解析与方向解析)
    通过xftp传输文件失败的解决方法
    /etc/hosts与/etc/resolv.conf区别
    /etc/nsswitch.conf配置文件--服务搜索顺序
    docker学习笔记
  • 原文地址:https://www.cnblogs.com/forever4444/p/1454499.html
Copyright © 2011-2022 走看看