zoukankan      html  css  js  c++  java
  • P3377 【模板】左偏树(可并堆)

      

    因为路径压缩的原因  pop出根的时候记得要将根的父亲连向合并后新树的根

    #include<bits/stdc++.h>
    using namespace std;
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define ll long long
    #define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
    #define pb push_back
    #define inf 0x3f3f3f3f
    #define CLR(A,v)  memset(A,v,sizeof A)
    typedef pair<int,int>pii;
    //////////////////////////////////
    const int N=2e6+10;
    
    int val[N],lson[N],rson[N],f[N],dis[N];
    
    int find1(int x){return f[x]==x?x:f[x]=find1(f[x]);};
    
    int Merge(int x,int y)
    {
        if(!x||!y)return x+y;
        if(val[x]>val[y]||val[x]==val[y]&&x>y)swap(x,y);
        rson[x]=Merge(rson[x],y);
        if(dis[lson[x]]<dis[rson[x]])swap(lson[x],rson[x]);
        f[lson[x]]=f[rson[x]]=f[x]=x;
        dis[x]=dis[rson[x]]+1;
        return x;
    }
    
    void pop(int x){val[x]=-1;f[lson[x]]=lson[x];f[rson[x]]=rson[x];f[x]=Merge(lson[x],rson[x]);}
    
    int n,m,a,b,c;
    int main()
    {
        cin>>n>>m;
        dis[0]=-1;
        rep(i,1,n)scanf("%d",&val[i]),f[i]=i;
    
        rep(i,1,m)
        {
            cin>>a>>b;
            if(a==1)
            {
                cin>>c;
                if(val[b]==-1||val[c]==-1)continue;
                int x=find1(b),y=find1(c);
                if(x!=y)f[x]=f[y]=Merge(x,y);
            }
            else
            {
                if(val[b]==-1)printf("-1
    ");
                else printf("%d
    ",val[find1(b)]),pop(find1(b));
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    软件工程实践总结
    beta答辩总结
    beta冲刺(6/7)
    beta 冲刺(5/7)
    beta冲刺(4/7)
    beta冲刺(3/7)
    beta冲刺(2/7)
    beta冲刺(1/7)
    CentOS7安装新版git
    NameError: name 'reload' is not defined
  • 原文地址:https://www.cnblogs.com/bxd123/p/11345146.html
Copyright © 2011-2022 走看看