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

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

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 7478  Solved: 3501
[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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define pa t[x].fa
#define lc t[x].ch[0]
#define rc t[x].ch[1]
const int N=2e5+5;
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}

int n,Q,x,y;
char s[3];
struct node{
int ch[2],fa,rev,size,w;
}t[N];
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline int wh(int x){return t[pa].ch[1]==x;}
inline int isRoot(int x){return t[pa].ch[0]!=x&&t[pa].ch[1]!=x;}
inline void pushDown(int x){
if(t[x].rev){
t[lc].rev^=1;
t[rc].rev^=1;
swap(lc,rc);
t[x].rev=0;
}
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^1];t[t[f].ch[c]].fa=f;
t[x].ch[c^1]=f;t[f].fa=x;
update(f);update(x);
}
int st[N],top;
inline void splay(int x){
top=0;st[++top]=x;
for(int i=x;!isRoot(i);i=t[i].fa)
st[++top]=t[i].fa;
for(int i=top;i>=1;i--) pushDown(st[i]);

for(;!isRoot(x);rotate(x))
if(!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
}
inline void Access(int x){
int y=0;
for(;x;y=x,x=pa){
splay(x);
rc=y;
}
}
inline void MakeRoot(int x){
Access(x);
splay(x);
t[x].rev^=1;
}
inline int FindRoot(int x){
Access(x);
splay(x);
while(lc) x=lc;
return x;
}
inline void Link(int x,int y){
MakeRoot(x);
t[x].fa=y;
//splay(x);
}
inline void Cut(int x,int y){
MakeRoot(x);
Access(y);
splay(y);
t[y].ch[0]=t[x].fa=0;
}

int main(){
while(Q--){
else if(s[0]=='D') Cut(x,y);
else{
if(FindRoot(x)==FindRoot(y)) puts("Yes");
else puts("No");
}
}
}

posted @ 2017-01-11 14:06  Candy?  阅读(173)  评论(0编辑  收藏