这题和洛谷上的左偏树模板的解题思路是一模一样的,所以只要贴上左偏树的模板就好了。
附上AC代码:
#include <cstdio>
#include <cctype>
#include <algorithm>
#define N 1000010
using namespace std;
int n,m,dis[N],w[N],x,y,f[N],ls[N],rs[N];
void read(int& a){
static char c=getchar();a=0;int f=1;
while (!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
while (isdigit(c)) a=a*10+c-'0',c=getchar();
}
int gf(int x){
while (f[x]) x=f[x];
return x;
}
int hb(int a,int b){
if (a*b==0) return a+b;
if (w[a]>w[b]||(w[a]==w[b]&&a>b)) swap(a,b);
rs[a]=hb(rs[a],b);
f[rs[a]]=a;
if (dis[ls[a]]<dis[rs[a]]) swap(ls[a],rs[a]);
dis[a]=dis[rs[a]]+1;
return a;
}
int main(void){
read(n),dis[0]=-1;
for (int i=1; i<=n; ++i) read(w[i]);
read(m);
while (m--){
char c=getchar();
while (c!='M'&&c!='K') c=getchar();
switch (c){
case 'M':
read(x),read(y);
if (w[x]==-1||w[y]==-1) continue;
x=gf(x),y=gf(y);
if (x!=y) hb(x,y);
break;
case 'K':
read(x);
if (w[x]==-1) puts("0");
else {
x=gf(x),printf("%d\n",w[x]),w[x]=-1;
f[ls[x]]=f[rs[x]]=0,hb(ls[x],rs[x]);
}
break;
}
}
}
orzLYF大佬,如此的强,在半个月前就把这题A掉了。别问我是怎么知道的。
(因为这题需要权限,而我又没有氪金,所以交这题时我用的是他的号。别问我是怎么知道他的密码的)