HDU 1856 并查集

More is better

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


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
就是求并查集的秩
形成其结构后
用find统计个数
#include<cstdio> #include<iostream> using namespace std; int pre[100005]; bool flag[100005]; int g[100005]={0}; int find(int x) {     int r=x;     while (r != pre[r])     {         r = pre[r];     }     int i=x,j;     while (i != r)     {         j = pre[i];         pre[i] = r;         i = j;     }     return r; } void mix(int x,int y) {     int fx=find(x),fy=find(y);     if (fx != fy)     {         pre[fx] = fy;         } } int main() { int n=0; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=100000;i++) { pre[i]=i; g[i]=0; flag[i]=0;    }    int max=1;    int MAX=-999; for(int i=1;i<=n;i++) { int x=0; int y=0; scanf("%d%d",&x,&y); mix(x,y); if(x>max) max=x; if(y>max) max=y; } for(int i=1;i<=max;i++) { g[find(i)]++; } for(int i=1;i<=max;i++) { if(g[i]>MAX) MAX=g[i]; } printf("%d\n",MAX); } return 0; }
2
别人的代码直接结构中统计
/*我的思路大概是,先建立一个并查集数组,一个标记数组(主要为了防止超时),一个组员数量数组。首先先把并查集数组初始话为本身,并把组员数量初始化为1,之后就是按代码上理解下思路*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 10000010

int tree[MAX],big[MAX];//并查集数组,标记数组,和老大手下多少人
bool mark[MAX];
int find(int a)
{
    int x=a;
    while(tree[x]!=x)
        x=tree[x];
    tree[a]=x;//这里很重要,没这个就超时
    return x;
}
int father(int a,int b)
{
    int i,j;
    i=find(a);
    j=find(b);
    if(i!=j)
    {
        tree[i]=j;
        big[j]+=big[i];//换老大的时候记得把对方的组员放到另一个老大那里
    }
    return 0;
}//函数不解释
int main()
{
    int t,i,a,b,max;
    while(~scanf("%d",&t))
    {
        if(t==0)//输入0返回1
        {
            printf("1\n");
            continue;
        }
        for(i=0;i<MAX;i++)//初始化
        {
            tree[i]=i;
            big[i]=1;
        }
        while(t--)
        {
            scanf("%d%d",&a,&b);
            father(a,b);
            mark[a]=1;
            mark[b]=1;
        }
        max=0;
        for(i=0;i<MAX;i++)
            if(mark[i]&&i==tree[i])
            {
                if(big[i]>max)
                    max=big[i];
            }
        printf("%d\n",max);
        memset(mark,0,sizeof(mark));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值