• [HDU 1856] More is better


    More is better

    Time Limit: 5000/1000 MS (Java/Others)    

    Memory Limit: 327680/102400 K (Java/Others)

    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.
     
    【题解】并查集问题:求最大rank
    我加入了路径压缩和按秩合并。
    然后轻松AC了= =
    注意的是有一个特判,n==0时,输出1.
    注意不需要开始就全部初始化,那样复杂度为O(10000005*T) T为数据组数,那么肯定受不了。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int pre[10000005],r[10000005],n,maxx;
     4 bool vis[10000005];
     5 int find(int x) {
     6     int r=x;
     7     while(pre[r]!=r) r=pre[r];
     8     int i=x,j;
     9     while(i!=r) {
    10         j=pre[i];
    11         pre[i]=r;
    12         i=j;
    13     }
    14     return r;
    15 }
    16 void join(int a,int b) {
    17     int a1=find(a),b1=find(b);
    18     if (a1!=b1) {
    19         pre[a1]=b1;
    20         r[b1]+=r[a1];
    21         if (r[b1]>maxx) maxx=r[b1];
    22     }
    23 }
    24 
    25 int main() {
    26     while(scanf("%d",&n)!=EOF) {
    27         maxx=-1;memset(vis,0,sizeof(vis));
    28         if(n==0) {printf("1
    "); continue;}
    29         for (int i=1;i<=n;++i) {
    30             int a,b;
    31             scanf("%d%d",&a,&b);
    32             if(!vis[a]) {
    33                 vis[a]=1;
    34                 pre[a]=a;
    35                 r[a]=1;
    36             }
    37             if(!vis[b]) {
    38                 vis[b]=1;
    39                 pre[b]=b;
    40                 r[b]=1;
    41             }
    42             join(a,b);
    43         }
    44         printf("%d
    ",maxx);
    45     }
    46     return 0;
    47 }
    View Code
    这篇文章由TonyFang发布。 所有解释权归TonyFang所有。 Mailto: tony-fang@map-le.net
  • 相关阅读:
    在Ubuntu系统上手动安装GCC环境
    linux查看操作系统版本信息
    (已解决)Public Key Retrieval is not allowed异常
    idea中terminal的配置
    IDE提交Git出现husky>precommit错误
    【IntelliJ IDEA】idea支持vue文件,设置对vue的支持
    Vue3 计算属性
    Vue3 组件
    Vue.js 循环语句
    Vue3 监听属性
  • 原文地址:https://www.cnblogs.com/TonyNeal/p/hdu1856.html
Copyright © 2020-2023  润新知