zoukankan      html  css  js  c++  java
  • More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

    More is better 

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


    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.
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <set>
     6 #include <cmath>
     7 using namespace std;
     8 #define Max  10000000+5
     9 int per[Max];
    10 int ans[Max];
    11 int num[Max];
    12 bool vis[Max];
    13 int n;
    14 void init()
    15 {
    16     for(int i=1;i<Max;i++)
    17         per[i]=i;
    18 }
    19 int find(int a)
    20 {
    21     if(a==per[a])
    22         return a;
    23     return per[a]=find(per[a]);
    24 }
    25 int unite(int a,int b)
    26 {
    27     a=find(a);
    28     b=find(b);
    29     if(a!=b)
    30         per[a]=b;
    31     return 0;
    32 }
    33 int main()
    34 {
    35     int t,a,b;
    36     freopen("in.txt","r",stdin);
    37     while(scanf("%d",&n)!=EOF)
    38     {
    39         init();
    40         int Maxn=1,p=0;
    41         memset(vis,0,sizeof(vis));
    42         fill(ans,ans+Max,0);
    43         for(int i=0;i<n;i++)
    44         {
    45             scanf("%d%d",&a,&b);
    46             if(vis[a]==0)
    47             {
    48                 vis[a]=1;
    49                 num[p++]=a;
    50             }
    51             if(vis[b]==0)
    52             {
    53                 vis[b]=1;
    54                 num[p++]=b;
    55             }
    56             unite(a,b);
    57         }
    58         for(int i=0;i<p;i++)
    59         {
    60             t=find(num[i]);
    61             ans[t]++;
    62             //cout<<t<<endl;
    63             Maxn=max(Maxn,ans[t]);
    64         }
    65         printf("%d
    ",Maxn);
    66     }
    67 }
     
  • 相关阅读:
    5-2 bash 脚本编程之一 变量、变量类型等
    4-4 grep及正则表达式
    4-3 管理及IO重定向
    4-2 权限及权限管理
    CentOS7 发布 ASP.NET MVC 4 --- mono 4.6.0 + jexus 5.8.1
    CentOS7 安装 nginx
    Hibernate学习笔记--------4.查询
    Hibernate学习笔记--------3.缓存
    Hibernate学习笔记--------2.一多|多多的CRUD
    Hibernate学习笔记--------1.单表操作
  • 原文地址:https://www.cnblogs.com/a1225234/p/5181339.html
Copyright © 2011-2022 走看看