# bzoj 2049: [Sdoi2008]Cave 洞穴勘测

## 2049: [Sdoi2008]Cave 洞穴勘测

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 9950  Solved: 4818
[Submit][Status][Discuss]

200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3

No
Yes
No

Yes
No

## HINT

https://wenku.baidu.com/view/766233c50c22590102029d6c.html?from=search

https://wenku.baidu.com/view/7857b870aaea998fcc220ed8.html

#include<iostream>
#include<cstdio>
#define maxn 100010
using namespace std;
int n,m,fa[maxn],st[maxn],c[maxn][2];
bool rev[maxn];
bool isroot(int x){
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void pushdown(int x){
int l=c[x][0],r=c[x][1];
if(rev[x]){
rev[x]^=1;rev[l]^=1;rev[r]^=1;
swap(c[x][0],c[x][1]);
}
}
void rotate(int x){
int y=fa[x],z=fa[fa[x]],l,r;
if(c[y][0]==x)l=0;else l=1;r=l^1;
if(!isroot(y)) c[z][c[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
}
void splay(int x){
int top=0;st[++top]=x;
for(int i=x;!isroot(i);i=fa[i])st[++top]=fa[i];
for(int i=top;i;i--)pushdown(st[i]);
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if((c[y][0]==x)^(c[z][0]==y))rotate(x);
else rotate(x);
}
rotate(x);
}
}
void access(int x){
for(int t=0;x;x=fa[x])splay(x),c[x][1]=t,t=x;
}
void rever(int x){
access(x);splay(x);rev[x]^=1;
}
rever(x);fa[x]=y;splay(x);
}
void cut(int x,int y){
rever(x);
access(y);splay(y);c[y][0]=fa[x]=0;
}
int find(int x){
access(x);splay(x);
int y=x;
while(c[y][0])y=c[y][0];
return y;
}
int main(){
freopen("Cola.txt","r",stdin);
int x,y;char s[10];
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%s",s);scanf("%d%d",&x,&y);
}