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 }
    
    
  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2389227.html
Copyright © 2011-2022 走看看