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

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

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

测lct的模板题。

/**************************************************************
Problem: 2049
User: weeping
Language: C++
Result: Accepted
Time:1936 ms
Memory:3644 kb
****************************************************************/

#include <bits/stdc++.h>

using namespace std;

{
static const int MAXN = 100000 + 7;

int ch[MAXN][2], fa[MAXN], rev[MAXN], sz[MAXN];
int sk[MAXN];

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

void reverse(int x)
{
rev[x] ^= 1, swap(ch[x][0],ch[x][1]);
}

void update(int x)
{
sz[x] = ch[x][0] +  ch[x][1] +1;
}

void push_down(int x)
{
if(!rev[x]) return ;
if(ch[x][0]) reverse(ch[x][0]);
if(ch[x][1]) reverse(ch[x][1]);
rev[x]=0;
}

void rotate(int x)
{
int f = fa[x], gf = fa[f];
int t1 = ( x != ch[f][0]), t2 = ( f != ch[gf][0]), tmp = ch[x][1^t1];
if(!isroot(f)) ch[gf][0^t2] = x;
fa[tmp] = f, fa[x] = gf, ch[x][1^t1] = f, fa[f] = x, ch[f][0^t1] = tmp;
update(f);
}

void splay(int x)
{
int top = 0;
sk[++top] = x;
for(int i = x; !isroot(i); i = fa[i])   sk[++top] = fa[i];
while(top)  push_down(sk[top--]);
for(int f = fa[x], gf = fa[f]; !isroot(x); rotate(x), f = fa[x],gf = fa[f])
if(!isroot(f))
rotate((x==ch[f][0]) ^ (f==ch[gf][0]) ? x : f);
update(x);
}

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

void makeroot(int x)
{
access(x), splay(x), reverse(x);
}

int findroot(int x)
{
access(x), splay(x);
while(ch[x][0]) x = ch[x][0];
return x;
}
{
makeroot(x), fa[x] = y;
}

void cut(int x,int y)
{
makeroot(x), access(y), splay(y);
if(ch[y][0] == x)   ch[y][0] = fa[x] = 0;
update(y);
}

}lct;

char ss[100];
int main(void)
{
int n,m;
cin>>n>>m;
while(m--)
{
int x,y;
scanf("%s%d%d",ss,&x,&y);
if(ss[0]=='Q')
{
if(lct.findroot(x)==lct.findroot(y))
printf("Yes\n");
else
printf("No\n");
}
else if(ss[0]=='C')
}