记录编号 |
143892 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[SDOI 2008]Cave 洞穴勘测 |
最终得分 |
100 |
用户昵称 |
cstdio |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
1.064 s |
提交时间 |
2014-12-18 15:58:37 |
内存使用 |
0.42 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int SIZEN=10010;
class Node{
public:
int lc,rc,fa;
bool inv;
Node(){lc=rc=fa=inv=0;}
#define lc(x) Tree[x].lc
#define rc(x) Tree[x].rc
#define fa(x) Tree[x].fa
#define inv(x) Tree[x].inv
}Tree[SIZEN];
bool Is_Root(int x){return lc(fa(x))!=x&&rc(fa(x))!=x;}
void Push_Down(int x){
if(inv(x)){
swap(lc(x),rc(x));
inv(lc(x))^=1;
inv(rc(x))^=1;
inv(x)=false;
}
}
void Zig(int x){//右旋
int y=fa(x),z=fa(y);
if(lc(z)==y) lc(z)=x;
if(rc(z)==y) rc(z)=x;
fa(x)=z;
lc(y)=rc(x);fa(lc(y))=y;
rc(x)=y;fa(y)=x;
}
void Zag(int x){//左旋
int y=fa(x),z=fa(y);
if(lc(z)==y) lc(z)=x;
if(rc(z)==y) rc(z)=x;
fa(x)=z;
rc(y)=lc(x);fa(rc(y))=y;
lc(x)=y;fa(y)=x;
}
void Splay(int x){
Push_Down(x);
while(!Is_Root(x)){
int y=fa(x),z=fa(y);
Push_Down(z);Push_Down(y);Push_Down(x);
if(Is_Root(y)){
if(lc(y)==x) Zig(x);
else Zag(x);
return;
}
if(lc(z)==y){
if(lc(y)==x) Zig(y),Zig(x);
else Zag(x),Zig(x);
}
else{
if(rc(y)==x) Zag(y),Zag(x);
else Zig(x),Zag(x);
}
}
}
void Access(int x){
int y=0;
while(x){
Splay(x);
rc(x)=y;
y=x;
x=fa(x);
}
}
int Get_Root(int x){
Access(x);
Splay(x);
while(lc(x)){
Push_Down(x);
x=lc(x);
}
Splay(x);
return x;
}
void Make_Root(int x){
Access(x);
Splay(x);
inv(x)^=1;
}
void Link(int x,int y){
Make_Root(x);
fa(x)=y;
Splay(x);
}
void Cut(int x,int y){
Make_Root(x);
Access(y);
Splay(y);
fa(lc(y))=0;lc(y)=0;
}
void Query(int x,int y){
if(Get_Root(x)==Get_Root(y)) printf("Yes\n");
else printf("No\n");
}
int N,Q;
void Process(void){
char cmd[10];
scanf("%d%d",&N,&Q);
int a,b;
for(int i=1;i<=Q;i++){
scanf("%s",cmd);
scanf("%d%d",&a,&b);
if(cmd[0]=='C') Link(a,b);
else if(cmd[0]=='D') Cut(a,b);
else if(cmd[0]=='Q') Query(a,b);
}
}
int main(){
freopen("sdoi2008_cave.in","r",stdin);
freopen("sdoi2008_cave.out","w",stdout);
Process();
return 0;
}