# 【bzoj1455】【罗马游戏】左偏树+并查集（模板）

Description

Input

Output

Sample Input
5
100 90 66 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4
Sample Output
10
100
0
66

fa[fax]=fa[fay]=merge(fax,fay)

fa[fax]=merge(le,ri);fa[fa[fax]]=fa[fax]

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=2000000+5;

struct node{
int ls,rs;
int dis,key;
}t[N];
int n,m,fa[N];
bool kill[N];

int getfa(int x){
if(fa[x]==x) return x;
return fa[x]=getfa(fa[x]);
}
int merge(int x,int y){
if(x==0) return y;
if(y==0) return x;
if(t[x].key>t[y].key) swap(x,y);
t[x].rs=merge(t[x].rs,y);
if(t[t[x].ls].dis<t[t[x].rs].dis) swap(t[x].ls,t[x].rs);
t[x].dis=t[t[x].rs].dis+1;
return x;
}
int main(){
t[0].ls=t[0].rs=0;
t[0].dis=-1,t[0].key=0x3f3f3f3f;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&t[i].key);
fa[i]=i;
}
scanf("%d",&m);
char opt[2];
int x,y;
while(m--){
scanf("%s",opt);
if(opt[0]=='M'){
scanf("%d%d",&x,&y);
if(kill[x]||kill[y]) continue;
int fax=getfa(x),fay=getfa(y);
if(fax==fay) continue;
int tmp=merge(fax,fay);
fa[fay]=fa[fax]=tmp;
}
else{
scanf("%d",&x);
if(kill[x]){
printf("0\n");continue;
}
int fax=getfa(x);
printf("%d\n",t[fax].key);
kill[fax]=1;
int le=t[fax].ls,ri=t[fax].rs;
fa[fax]=merge(le,ri);
fa[fa[fax]]=fa[fax];
}
}
}
posted @ 2017-10-31 19:09  LinnBlanc  阅读(153)  评论(0编辑  收藏  举报