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.
      
     
     
    View Code
     1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<math.h>
    4 #include<string.h>
    5 int num[1001000], count, mm[1001000];
    6 int max = 1;
    7 int find( int x )
    8 {
    9 return num[x] == x?num[x]:num[x] = find( num[x] );
    10 }
    11 void merge( int x, int y )
    12 {
    13 int fx, fy;
    14 fx = find( x );
    15 fy = find( y );
    16 if( fx != fy )
    17 {
    18 num[fx] = fy;
    19 mm[fy] += mm[fx];
    20 max = max > mm[fy]?max:mm[fy];
    21 }
    22 }
    23 int main()
    24 {
    25 int n, x, y, i, j;
    26 while( scanf( "%d", &n ) == 1 )
    27 {
    28 max = 1;
    29 for( i = 1; i <= 1001000; i++ )
    30 {
    31 num[i] = i;
    32 mm[i] = 1;
    33 }
    34 for( j = 1; j <= n; j++ )
    35 {
    36 scanf( "%d%d", &x, &y );
    37 merge( x, y );
    38 }
    39 printf( "%d\n", max );
    40 }
    41 }

  • 相关阅读:
    自执行函数的几种不同写法的比较
    Textarea与懒惰渲染
    备忘:递归callee.caller导致死循环
    围观STK
    某台机器上IE8抛“Invalid procedure call or argument”异常
    QWrap Selector之W3C版
    onclick与listeners的执行先后问题
    随机问题之洗牌算法
    selector4 之 巧妙的主体定义符
    神奇的"javascript:"
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2405008.html
Copyright © 2011-2022 走看看