hdu1856

题意:就是询问下,一个并查集里面结点数最多的....并输出来,大致是这个意思吧,当然可以离散化下,水题.....

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 100005
struct
{
    int father;
    int sum;
} s[4*M];
int t[2*M][2],a[2*M];
int find(int x)
{
    int i=x,root;
    while(x!=s[x].father)
        x=s[x].father;
    root=x;
    x=i;
    while(x!=s[x].father)
    {
        i=s[x].father;
        s[root].sum+=s[x].sum;
        s[x].sum=0;
        s[x].father=root;
        x=i;
    }
    return root;
}
void liantong(int x,int y)
{
    s[x].sum+=s[y].sum;
    s[y].father=x;
    s[y].sum=0;
}
int erfen(int ll,int rr,int num)
{
    while(ll<=rr)
    {
        int mid=(ll+rr)/2;
        if(a[mid]>num)
        rr=mid-1;
        else
        ll=mid+1;
    }
    //printf("%d %d\n",a[rr],num);
    return rr;
}
int main()
{
    int n;
    while(scanf("%d",&n)>0)
    {
        int ans=0;
        int cnt=0;
        for(int i=0; i<=3*100005; i++)
        {
            s[i].father=i;
            s[i].sum=1;
        }
        //scanf("")
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&t[i][0],&t[i][1]);
            a[cnt++]=t[i][0];
            a[cnt++]=t[i][1];
        }
        sort(a,a+cnt);
        int cnt1=1;
        for(int i=1; i<cnt; i++)
            if(a[i]!=a[i-1])   a[cnt1++]=a[i];
        for(int i=cnt1-1; i>0; i--)
            if(a[i]!=a[i-1]+1)   a[cnt1++]=a[i-1]+1;
        sort(a,a+cnt1);
        for(int i=0; i<n; i++)
        {
            int left=0,right=cnt1-1;
            int x=erfen(left,right,t[i][0]);
            int y=erfen(left,right,t[i][1]);
            x=find(x);
            y=find(y);
            if(x!=y)
                liantong(x,y);
        }
        for(int i=0;i<=2*100005;i++)
        find(i);
        int ans1=0;
        for(int i=0;i<=2*100005;i++)
        if(ans1<s[i].sum)
        ans1=s[i].sum;
        printf("%d\n",ans1);
    }
    return 0;
}

 

posted @ 2013-12-16 14:55  紫忆  阅读(760)  评论(0编辑  收藏  举报