# 2049: [Sdoi2008]Cave 洞穴勘测

Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 10233 Solved: 4964
[Submit][Status][Discuss]

## Sample Input

### 样例输入1 cave.in

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

# 题解

LCT模板题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <sstream>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
template<class T>
inline void swap(T &a, T &b)
{
T tmp = a;a = b;b = tmp;
}
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 10000 + 10;
int ch[MAXN][2], fa[MAXN], data[MAXN], size[MAXN], lazy[MAXN];
inline int son(int x){return x == ch[fa[x]][1];}
inline int isroot(int x){return ch[fa[x]][0] != x && ch[fa[x]][1] != x;}
inline void pushup(int x){size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;return;}
inline void rever(int x){lazy[x] ^= 1;swap(ch[x][0], ch[x][1]);}
inline void pushdown(int x){if(lazy[x])lazy[x] = 0, rever(ch[x][0]), rever(ch[x][1]);}
void rotate(int x)
{
int y = fa[x], z = fa[y], b = son(x), c = son(y), a = ch[x][!b];
if(!isroot(y)) if(z) ch[z][c] = x; fa[x] = z;
if(a) fa[a] = y;ch[y][b] = a;
ch[x][!b] = y, fa[y] = x;
pushup(y), pushup(x);
}
void dfs(int x)
{
if(!x) return;
dfs(fa[x]), pushdown(x);
}
void splay(int x)
{
dfs(x);
while(!isroot(x))
{
int y = fa[x], z = fa[y];
if(!isroot(y))
if(son(x) == son(y)) rotate(y);
else rotate(x);
rotate(x);
}
}
void access(int x){for(int y = 0;x;y = x, x = fa[x]) splay(x), ch[x][1] = y, pushup(x);}
/*换根*/void makeroot(int x){access(x), splay(x), rever(x);}
/*找根*/int findroot(int x){access(x), splay(x);while(ch[x][0]) x = ch[x][0];return x;}
/*连接*/void link(int x, int y){makeroot(x);fa[x] = y;}
/*切割*/void cut(int x, int y){makeroot(x), access(y), splay(y);if(ch[y][0] == x)fa[x] = ch[y][0] = 0, pushup(y);}
/*询问路径，此时以x为根的平衡树代表x->v的路径*/void path(int x, int y){makeroot(x), access(y), splay(y);}
int n,m,tmp1,tmp2;
char s[1000];
int main()
{
freopen("data.txt", "r", stdin);
for(int i = 1;i <= m;++ i)
{
scanf("%s", s + 1);