标签:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 10010
using namespace std;
int ch[N][2];
int fa[N];
int rev[N];
int rt[N];
int n,m;
void reverse(int x)
{
if(!x)return;
rev[x]^=1;
swap(ch[x][0],ch[x][1]);
}
void pushdown(int x)
{
if(rev[x])
{
reverse(ch[x][0]);
reverse(ch[x][1]);
rev[x]=0;
}
}
void down(int x)
{
if(!rt[x])down(fa[x]);
pushdown(x);
}
void pushup(int x){}
void rotate(int x)
{
int y=fa[x],kind=ch[y][1]==x;
ch[y][kind]=ch[x][!kind];
fa[ch[y][kind]]=y;
fa[x]=fa[y];
fa[y]=x;
ch[x][!kind]=y;
if(rt[y])
{
rt[x]=1,rt[y]=0;
}else ch[fa[x]][ch[fa[x]][1]==y]=x;
pushup(y);
}
void splay(int x)
{
down(x);
while(!rt[x])
{
int y=fa[x],z=fa[y];
if(rt[y])
{
rotate(x);
}else if((ch[y][1]==x)==(ch[z][1]==y))
{
rotate(y),rotate(x);
}else
{
rotate(x),rotate(x);
}
}
pushup(x);
}
void access(int x)
{
int y=0;
while(x)
{
splay(x);
rt[ch[x][1]]=1;rt[y]=0;
ch[x][1]=y;
pushup(x);
y=x,x=fa[x];
}
}
int find_root(int x)
{
while(fa[x])x=fa[x];
return x;
}
void mt(int x)
{
access(x);
splay(x);
reverse(x);
}
void link(int x,int y)
{
mt(x);
fa[x]=y;
}
void cut(int x,int y)
{
mt(x);
access(y);
splay(y);
fa[x]=0;
ch[y][0]=0;
rt[x]=1;
}
void init(int x)
{
fa[x]=ch[x][0]=ch[x][1]=rev[x]=0;
rt[x]=1;
}
char opt[25];
int main()
{
freopen("cave.in","r",stdin);
freopen("cave.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)init(i);
for(int i=1;i<=m;i++)
{
scanf("%s",opt);
int x,y;
scanf("%d%d",&x,&y);
if(opt[0]==‘Q‘)
{
if(find_root(x)==find_root(y))puts("Yes");
else puts("No");
}else if(opt[0]==‘D‘)cut(x,y);
else link(x,y);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
BZOJ 2049 [Sdoi2008]Cave 洞穴勘测 LCT
标签:
原文地址:http://blog.csdn.net/wzq_qwq/article/details/47344743