zoukankan      html  css  js  c++  java
  • hdu1856依旧并查集

    不过这个求的是最大集合的个数

    #include<iostream>
    #include<set>
    #include<cstdio>
    #include<vector>
    using namespace std;
    const int MAXN=10000005;
    const int MAXM=100005;
    
    int u[MAXN];
    int sum[MAXN];
    set<int>num;
    //bool seted[MAXN];
    int find(int p)
    {
        if(u[p]==p)
        {
           // seted[p]=1;
            return p;
        }else
        {
           // seted[p]=1;
            return u[p]=find(u[p]);
        }
    }
    void connect(int a,int b)
    {
        int f1=find(a);
        int f2=find(b);
        if(f1<f2)
        {
            u[f2]=f1;
        }else
        {
            u[f1]=f2;
        }
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int max=1;
            int i=0;
            num.clear();
            vector<int>input[2];
            input[0].clear();
            input[1].clear();
            //memset(seted,0,sizeof(seted));
            for(i=0;i<=n-1;i++)
            {
                int num1,num2;
                scanf("%d%d",&num1,&num2);
                
                num.insert(num1);
                num.insert(num2);
                input[0].push_back(num1);
                input[1].push_back(num2);
            }
            set<int>::reverse_iterator cp=num.rbegin();
            for(cp=num.rbegin();cp!=num.rend();cp++)
            {
                u[*cp]=*cp;
                sum[*cp]=0;
            }
            //memset(sum,0,sizeof(sum));
            for(i=0;i<=n-1;i++)
            {
                connect(input[0][i],input[1][i]);
            }
            for(cp=num.rbegin();cp!=num.rend();cp++)
            {
                sum[find(*cp)]++;
            }
            for(cp=num.rbegin();cp!=num.rend();cp++)
            {
                if(sum[*cp]>max)
                {
                    max=sum[*cp];
                }
            }
            printf("%d\n",max);
        }
        return 0;
    }
    

      

  • 相关阅读:
    记账本第二天
    记账本第一天
    HDU 1811
    Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
    KMP超强模板贴一份
    2014辽宁省赛总结
    Codeforces Round #244 (Div. 2)
    CodeForces 383D Antimatter
    NEU 1351 Goagain and xiaodao's romantic story I
    UVA 10692 Huge Mod
  • 原文地址:https://www.cnblogs.com/cj695/p/2609608.html
Copyright © 2011-2022 走看看