HDU 1856 More is better

并查之后统计就好了……

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 10000002;

class UFS
{
private:
    int Pa[MAXV];
public:
    void clear(int l,int r)
    {
        for(int i=l;i<=r;i++) Pa[i]=i;
    }
    int Find(int u)
    {
        return Pa[u]=(Pa[u]==u?u:Find(Pa[u]));
    }
    void Union(int u,int v)
    {
        Pa[Find(u)]=Find(v);
    }
}ufs;

int n,x,y,a[MAXV];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("More is better.txt","r",stdin);
    #else
    #endif

    while(scanf("%d",&n)!=EOF)
    {
        if(!n) printf("1\n");
        else
        {
            ufs.clear(1,10000000);
            int l=+inf,r=-inf;
            while(n--)
            {
                scanf("%d %d",&x,&y);
                l=min(l,min(x,y));
                r=max(r,max(x,y));
                ufs.Union(x,y);
            }
            int ans=0;
            memset(a,0,sizeof(a));
            for(int i=l;r>=i;i++)
            {
                ans=max(ans,++a[ufs.Find(i)]);
            }
            printf("%d\n",ans);
        }
    }

    return 0;
}

你可能感兴趣的:(HDU 1856 More is better)