# [BZOJ 1455]罗马游戏（左偏树+并查集）

Description

Solution

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#define MAXN 1000005
using namespace std;
int n,m,father[MAXN];
bool die[MAXN];
struct Node
{
int lch,rch,w,d;
Node():lch(0),rch(0),w(0),d(0){}
}heap[MAXN];
int merge(int x,int y)
{
if(!x||!y)return x+y;
if(heap[x].w>heap[y].w)swap(x,y);
heap[x].rch=merge(y,heap[x].rch);
if(heap[heap[x].lch].d<heap[heap[x].rch].d)swap(heap[x].lch,heap[x].rch);
heap[x].d=heap[heap[x].rch].d+1;
return x;
}
int find(int x)
{
if(x==father[x])return x;
return father[x]=find(father[x]);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&heap[i].w);
father[i]=i;}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
char opt[3];int x,y;
scanf("%s",opt);
if(opt[0]=='M')
{
scanf("%d%d",&x,&y);
if(die[x]||die[y])continue;
int fx=find(x),fy=find(y);
if(fx!=fy)
father[fx]=father[fy]=merge(fx,fy);
}
else
{
scanf("%d",&x);
if(die[x])printf("0\n");
else
{
int fx=find(x);
die[fx]=1;
printf("%d\n",heap[fx].w);
father[fx]=merge(heap[fx].lch,heap[fx].rch);
father[father[fx]]=father[fx];
}
}
}
return 0;
} 

posted @ 2017-05-24 23:35  Zars19  阅读(165)  评论(0编辑  收藏  举报