zoukankan      html  css  js  c++  java
  • 杭电1856--More is better

    More is better

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


    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   |   We have carefully selected several similar problems for you:  1879 1875 1102 1198 1162 
    //节点最多的并查集的节点数; (这段代码有一个优化, 根据数节点数不同在合并节点时进行区分)
    ac 代码:
     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 
     5 const int N = 10000002;
     6 
     7 struct node
     8 {
     9     int parent;
    10     int cnt; 
    11 } boy[N];
    12 
    13 int n;
    14 int result;
    15 
    16 void init()
    17 {
    18     for(int i=0; i<N; i++)
    19     {
    20         boy[i].parent = i;
    21         boy[i].cnt = 1;
    22     }
    23 }
    24 
    25 int find(int x)
    26 {
    27     int y = x, tmp;
    28     while(boy[y].parent != y)
    29         y = boy[y].parent;
    30     while(x != y)
    31     {
    32         tmp = boy[x].parent;     // 暂存父节点; 
    33         boy[x].parent = y;        // 父节点指向根;
    34         x = tmp;               // x指向根; 
    35     }
    36     return y;
    37 }
    38 
    39 void mercy(int a, int b)     //******** ***********//
    40 {
    41     if(boy[a].cnt > boy[b].cnt)
    42     {
    43         boy[b].parent = a;
    44         boy[a].cnt += boy[b].cnt;
    45         if(result < boy[a].cnt)
    46             result = boy[a].cnt;
    47     }
    48     else
    49     {
    50         boy[a].parent = b;
    51         boy[b].cnt += boy[a].cnt;
    52         if(result < boy[b].cnt)
    53             result = boy[b].cnt;
    54     }
    55 }
    56 
    57 
    58 int main()
    59 {
    60     int a, b, i;
    61 //    bool flag;
    62     while(cin >> n)
    63     {
    64         result = 1;
    65         init();
    66         while(n--)
    67         {
    68             scanf("%d %d", &a, &b);
    69             a = find(a);
    70             b = find(b);
    71             if(a != b)
    72             mercy(a, b);
    73         }
    74         cout << result << endl; 
    75     }
    76     return 0;    
    77 } 

     //又敲了一遍。

     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 const int N = 10000010;
     5 int father[N], have[N], result;
     6 void init()
     7 {
     8     result = 1;
     9     for(int i=1; i<N; i++)
    10     {
    11         father[i] = i;
    12         have[i] = 1;
    13     }
    14 }
    15 
    16 int find(int a)
    17 {
    18     int  r, j, k;
    19     r = a; 
    20     while(r != father[r])
    21         r = father[r];
    22     //father[a] = a;
    23     j = a;
    24     while(j != r)
    25     {
    26         k = father[j];
    27         father[j] = r;
    28         j = k;
    29     }
    30     return r;
    31 }
    32 
    33 void mercy(int a,int b)  //非递归压缩路径过, 
    34 {
    35     int q = find(a);
    36     int p = find(b);
    37     if(q != p)
    38     {
    39         father[q] = p;
    40         have[p] += have[q];
    41         //printf("%d
    ", have[p]);
    42         if(have[p] > result)
    43             result = have[p];
    44     }
    45 }
    46 
    47 int main()
    48 {
    49     int a, b, n;
    50     while(~scanf("%d", &n))
    51     {
    52         init();
    53         while(n--)
    54         {
    55             scanf("%d %d", &a, &b);
    56             mercy(a, b);
    57         }
    58         printf("%d
    ", result);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    html5的键盘事件
    阻止滑屏
    JS 复制到黏贴板上
    最新拖动原理
    方法——<37>
    验证——正则<37>
    《高级程序设计》 9 客户端检测
    《高级程序设计》8 BOM
    《高级程序设计》7 函数表达式
    《高级程序设计》6 面向对象的程序设计
  • 原文地址:https://www.cnblogs.com/soTired/p/4686979.html
Copyright © 2011-2022 走看看