zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    多列布局之等分布局
    布局之不定宽与自适应
    多列布局之一列、多列定宽及一列自适应布局
    居中布局之水平垂直布局
    JQuery 学习记录
    初遇GitHub
    关于JS中的函数定义及函数表达式
    类型识别
    页面制作(PS/HTML/CSS)易错点总结
    工欲善其事必先利其器系列之:更换Visual Studio代码风格.
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3199710.html
Copyright © 2011-2022 走看看