• HDU1856--More is better


    More is better
    Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
    Total Submission(s): 9710 Accepted Submission(s): 3573


    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
    HDU 2007 Programming Contest - Final


    Recommend
    lcy

    简单并查集,题目意思是算数目最多的一个集合的个数

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<cmath>
     6 #include<map>
     7 using namespace std;
     8 
     9 int parent[10000001];
    10 int n;
    11 
    12 void Ufset()
    13 {
    14     int i;
    15     for(i=1;i<=1000000;i++)
    16     {
    17         parent[i]=-1;
    18     }
    19 }
    20 
    21 int Find(int x)
    22 {
    23     int s;
    24     for(s=x;parent[s]>0;s=parent[s]){}
    25     while(s!=x)
    26     {
    27         int temp=parent[x];
    28         parent[x]=s;
    29         x=temp;
    30     }
    31     return s;
    32 }
    33 
    34 void Union(int R1,int R2)
    35 {
    36     int r1=Find(R1),r2=Find(R2);
    37     int temp=parent[r1]+parent[r2];
    38     parent[r1]=r2;
    39     parent[r2]=temp;
    40 }
    41 
    42 int main()
    43 {
    44     int c;
    45     while(scanf("%d",&c)!=EOF)
    46     {
    47         Ufset();
    48         map<int,int>m;
    49         int flag=1;
    50         int k=c;
    51         while(c--)
    52         {
    53             int u,v;
    54             scanf("%d%d",&u,&v);
    55             if(!m[u])m[u]=flag++;
    56             if(!m[v])m[v]=flag++;
    57             if(Find(m[u])!=Find(m[v]))
    58                 Union(m[u],m[v]);
    59         }
    60         int mmax=0;
    61         for(int i=1;i<flag;i++)
    62         {
    63             if(mmax>parent[i])
    64             {
    65                 mmax=parent[i];
    66             }
    67         }
    68         if(k)
    69             printf("%d
    ",-mmax);
    70         else
    71             printf("1
    ");
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    System Idle Process SYSTEM占用CPU
    apache和nginx的rewrite的区别
    解决file_get_contents failed to open stream: HTTP request failed! 错误
    个人总结大型高并发高负载网站的系统架构(转)
    代码的抽象三原则
    mysqldump导入某库某表的数据
    mysql中insert into和replace into以及insert ignore用法区别
    【原创】学习日记4:nginx负载均衡(二)2012.01.08
    【原创】学习日记1:redis安装以及lnmp环境搭建2012.01.06
    mysql优化 mysql.ini优化
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3199710.html
Copyright © 2020-2023  润新知