zoukankan      html  css  js  c++  java
  • hdu1856

    More is better

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


    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
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<cstdlib>
     4 using namespace std;
     5 struct node
     6 {
     7     int pre;
     8     int rank;
     9 } tree[10000005];
    10 int maxn;
    11 int find(int x)
    12 {
    13     int p = x;
    14     while(tree[p].pre!=p)
    15     {
    16         p = tree[p].pre;
    17     }
    18     int i = x;
    19     while(i!=p)
    20     {
    21         int j = tree[i].pre;
    22         tree[i].pre = p;
    23         i = j;
    24     }
    25     return p;
    26 }
    27 void unin(int x,int y)
    28 {
    29     int fx = find(x);
    30     int fy = find(y);
    31     if(fx!=fy)
    32     {
    33         tree[fy].rank+=tree[fx].rank;//统计一个节点下的人数
    34         if(tree[fy].rank > maxn)
    35         {
    36             maxn = tree[fy].rank;//找出最大的节点数
    37         }
    38         tree[fx].pre = fy;//fx 的前一个节点是fy
    39     }
    40 }
    41 int main()
    42 {
    43     int n;
    44     int i,j;
    45     int x,y;
    46     while(scanf("%d",&n)!=EOF)
    47     {
    48         maxn = 1;
    49         for(i = 0; i < 10000005; i++)
    50         {
    51             tree[i].pre = i;//初始化
    52             tree[i].rank = 1;//记录每个节点下有几个人
    53         }
    54         for(i = 0; i < n; i++)
    55         {
    56             scanf("%d %d",&x,&y);
    57             unin(x,y);//合并
    58         }
    59         printf("%d
    ",maxn);
    60     }
    61     return 0;
    62 
    63 }
    View Code
  • 相关阅读:
    WPF操作ini 文件的读写示例
    WPF调用图片路径,或资源图片
    WPF笔记一
    WPF 获取程序路径的一些方法,根据程序路径获取程序集信息
    10进制转62进制,实现穷举指定位数的所有密码组合(暴力破解)
    匿名对象和object的转换
    构造函数和:this()的应用
    WPF TextBox自动滚动到最户一行
    C#接口的使用场合,接口应用
    读匿名object对象的属性值
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/3474383.html
Copyright © 2011-2022 走看看