# bzoj2049: [Sdoi2008]Cave 洞穴探测

bzoj2049: [Sdoi2008]Cave 洞穴探测

Lct裸题

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<cmath>
const int N=200000+7;
typedef long long LL;
using namespace std;
int n,m,ch[N][2],p[N],flip[N];
char op[20];

template<typename T> void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}

#define lc ch[x][0]
#define rc ch[x][1]
void down(int x) {
if(!flip[x]) return;
swap(lc,rc);
flip[lc]^=1;
flip[rc]^=1;
flip[x]^=1;
}

int isroot(int x) {return (ch[p[x]][0]!=x&&ch[p[x]][1]!=x);}

void rotate(int x) {
int y=p[x],z=p[y],l=(x==ch[y][1]),r=l^1;
if(!isroot(y)) ch[z][y==ch[z][1]]=x; p[x]=z;
ch[y][l]=ch[x][r]; p[ch[x][r]]=y;
ch[x][r]=y; p[y]=x;
}

void splay(int x) {
static int g[N],top=0,tp;
for(tp=x;!isroot(tp);tp=p[tp]) g[++top]=tp;
g[++top]=tp;
while(top) down(g[top--]);
for(;!isroot(x);rotate(x)) {
int y=p[x],z=p[y];
if(!isroot(y))
((x==ch[y][1])^(y==ch[z][1]))?rotate(x):rotate(y);
}
}

void access(int x) {
for(int t=0;x;x=p[t=x]) {
splay(x);
ch[x][1]=t;
}
}

int findroot(int x) {
access(x);
splay(x);
while(lc) x=lc;
return x;
}

void newroot(int x) {
access(x);
splay(x);
flip[x]^=1;
}

void lik(int x,int y) {
newroot(x);
splay(x);
p[x]=y;
}

void del(int x,int y) {
newroot(x);
access(y);
splay(y);
if(ch[y][0]==x) ch[y][0]=p[x]=0;
}

int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
while(m--) {
scanf("%s",op);
int x,y;
if(op[0]=='Q') {
if(findroot(x)==findroot(y)) printf("Yes\n");
else printf("No\n");
}
if(op[0]=='C') lik(x,y);
if(op[0]=='D') del(x,y);
}
return 0;
}
/*
200 5
Query 123 127
Connect 123    127
Query 123 127
Destroy    127    123
Query 123 127
*/
View Code
posted @ 2017-12-11 07:26  啊宸  阅读(...)  评论(... 编辑 收藏