标签:
今天学了一下可并堆的左偏树实现。
以下来自hzwer:
#include<bits/stdc++.h> #define N 1000005 using namespace std; int n,m,fa[N],l[N],r[N],d[N],v[N]; bool die[N]; char ch[10]; inline int read() { int a=0,f=1; char c=getchar(); while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1; c=getchar();} while (c>=‘0‘&&c<=‘9‘) {a=a*10+c-‘0‘; c=getchar();} return a*f; } int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);} int merge(int x,int y) { if (!x) return y; if (!y) return x; if (v[x]>v[y]) swap(x,y); r[x]=merge(r[x],y); if (d[r[x]]>d[l[x]]) swap(l[x],r[x]); d[x]=d[r[x]]+1; return x; } int main() { n=read(); for (int i=1;i<=n;i++) v[i]=read(),fa[i]=i; m=read(); d[0]=-1; for (int i=1;i<=m;i++) { scanf("%s",ch); if (ch[0]==‘M‘) { int x=read(),y=read(); if (die[x]||die[y]) continue; int p=find(x),q=find(y); if (p!=q) { int t=merge(p,q); fa[p]=fa[q]=t; } } else { int x=read(); if (die[x]) printf("0\n"); else { int p=find(x); die[p]=1; printf("%d\n",v[p]); fa[p]=merge(l[p],r[p]); fa[fa[p]]=fa[p]; } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4771877.html