码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1856 more is better

时间:2014-08-10 01:40:19      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   div   amp   log   size   

并查集基础

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll __int64
#define MAXN 1000
#define INF 0x7ffffff
using namespace std;
int maxx;
struct Children
{
    int coun;
    int father;
};
Children c[10000005];
int find(int a)
{
    int temp=a;
    while(temp!=c[temp].father)
    {
        temp=c[temp].father;
    }
    c[a].father=temp;
    return temp;
    //return c[a].father==a?a:find(c[a].father); 感觉这种写法更加简练 
}
void Union(int a,int b)
{
    int x=find(a);
    int y=find(b);
    if(x==y) return ;
    c[y].father=c[x].father;
    c[x].coun+=c[y].coun;
    if(maxx<c[x].coun) maxx=c[x].coun;
}
int main()
{
    int n,i,j,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) {cout<<1<<endl;continue;}
        maxx=0;
        for(i=1;i<=10000000;i++)//初始化并查集
        {
           c[i].coun=1;
           c[i].father=i;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            Union(a,b);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

  

hdu 1856 more is better,布布扣,bubuko.com

hdu 1856 more is better

标签:blog   os   io   for   div   amp   log   size   

原文地址:http://www.cnblogs.com/sola1994/p/3901826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!