传送门:www.lydsy.com/JudgeOnline/problem.php?id=2049
刚说BZOJ挂了就好了= =、
又是一道模板题:
Code:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10010;
struct node{
int rev;
node *c[2],*p;
void pushdown(){
if(rev){
c[0]->rev^=1;
c[1]->rev^=1;
swap(c[1],c[0]);
rev^=1;
}
}
void sets(node *x,int d){
pushdown();
(c[d]=x)->p=this;
}
bool d(){
return p->c[1]==this;
}
bool root(){
return p->c[0]!=this&&p->c[1]!=this;
}
}nil[maxn];
int tot=0;
void newnode(node *x){
x=nil+(++tot);
x->rev=0;
x->c[0]=x->c[1]=x->p=nil;
}
void rot(node *x){
node *p=x->p;
if(!p->root())p->p->pushdown();
p->pushdown();x->pushdown();
int d=x->d();
p->sets(x->c[!d],d);
if(p->root())
x->p=p->p;
else
p->p->sets(x,p->d());
x->sets(p,!d);
}
void splay(node *x){
while(!x->root())
if(x->p->root())
rot(x);
else if(x->d()==x->p->d())
rot(x->p),rot(x);
else
rot(x),rot(x);
}
node* access(node *x){
node *y=nil;
for(;x!=nil;y=x,x=x->p){
splay(x);
x->sets(y,1);
}return y;
}
void makert(node *x){
access(x)->rev^=1;
splay(x);
}
node *findrt(node *x){
for(x=access(x);x->c[0]!=nil;x=x->c[0]);return x;
}
void link(node *x,node *y){
makert(x);
x->p=y;
access(x);
}
void cut(node *x,node *y){
makert(x);
access(y);
splay(y);
y->c[0]=x->p=nil;
}
int n,m;
int getint(){
int res=0,ok=0;char ch;
while(1){
ch=getchar();
if(ch<='9'&&ch>='0'){
res*=10;res+=ch-'0';ok=1;
}else if(ok)break;
}return res;
}
int main(){
n=getint();m=getint();
node *x;
for(int i=1;i<=n;i++)
newnode(x);
nil->c[0]=nil->c[1]=nil->p=nil;
while(m--){
char opt[55];
scanf("%s",opt);
node *u=nil+getint(),*v=nil+getint();
if(opt[0]=='Q'){
if(findrt(u)==findrt(v)){
puts("Yes");
}else
puts("No");
}else
if(opt[0]=='C'){
link(u,v);
}else{
cut(u,v);
}
}
return 0;
}