zoukankan      html  css  js  c++  java
  • hdu 1856(hash+启发式并查集)

    More is better

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


    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
    题意:找最大的"朋友圈"吧。。
    题解:数据量有点大。。我用hash离散了一下,然后再扫一遍。记录下每个集团的最大值然后再找的,然后还用了启发式并查集。可能有更优的方法吧!
    注意的是输入0的时候要输出1,因为他自己也算一个。。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    const int N = 200005;
    const int M = 10000001;
    int Hash[M];
    int cnt[N];
    int father[N],dep[N];
    
    int _find(int x){
        if(x==father[x]) return x;
        return _find(father[x]);
    }
    int Union(int a,int b){
        int x = _find(a);
        int y = _find(b);
        if(x==y) return 0;
        if(dep[x]==dep[y]){
            father[x] = y;
            dep[y]++;
        }else if(dep[x]<dep[y]){
            father[x] = y;
        }else father[y]=x;
        return 1;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF){
            if(n==0) {
                printf("1
    ");
                continue;
            }
            memset(Hash,-1,sizeof(Hash));
            memset(cnt,0,sizeof(cnt));
            for(int i=1;i<=2*n;i++){
                father[i] = i;
                dep[i]=0;
            }
            int k=0;
            for(int i=0;i<n;i++){
                int a,b;
                scanf("%d%d",&a,&b);
                if(Hash[a]==-1){
                    Hash[a]=++k;
                }
                if(Hash[b]==-1){
                    Hash[b]=++k;
                }
                Union(Hash[a],Hash[b]);
            }
            for(int i=1;i<=2*n;i++){
                cnt[_find(i)]++;
            }
            int Max = -1;
            for(int i=1;i<=2*n;i++){
                Max = max(Max,cnt[i]);
            }
            printf("%d
    ",Max);
        }
    }
  • 相关阅读:
    mysql分组统计后将结果顺序排列(union实现)
    mysql格式化日期
    yaf框架安装
    如何通过PHP将excel的数据导入MySQL中
    yii日志保存机制
    安装PyInstaller打包python
    python正则表达式详解
    Python中类的定义与使用
    例子 使用sqlite3 数据库建立数据方式
    python操作轻量级数据库
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5474598.html
Copyright © 2011-2022 走看看