# [SDOI2008]洞穴勘测

Luogu

## 代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<set>
#include<map>
using namespace std;
#define mp make_pair
#define ll long long
#define re register
typedef pair<int,int> pii;
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=10010;
int sta[N],top;
struct node{int ff,ch[2],rev;}t[N];
bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
void reverse(int x){swap(t[x].ch[0],t[x].ch[1]);t[x].rev^=1;}
void pushdown(int x)
{
if(!t[x].rev)return;
if(t[x].ch[0])reverse(t[x].ch[0]);
if(t[x].ch[1])reverse(t[x].ch[1]);
t[x].rev=0;
}
void rotate(int x)
{
int y=t[x].ff,z=t[y].ff,k=(t[y].ch[1]==x);
if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
t[x].ch[k^1]=y;t[y].ff=x;
}
void splay(int x)
{
sta[++top]=x;
for(int i=x;!isroot(i);i=t[i].ff)sta[++top]=t[i].ff;
while(top)pushdown(sta[top--]);
while(!isroot(x))
{
int y=t[x].ff,z=t[y].ff;
if(!isroot(y))
(t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
for(int y=0;x;y=x,x=t[x].ff){splay(x);t[x].ch[1]=y;}
}
void makeroot(int x){access(x);splay(x);reverse(x);}
int findroot(int x)
{
access(x);splay(x);
while(t[x].ch[0])x=t[x].ch[0];
return x;
}
void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
void Cut(int x,int y)
{
split(x,y);
t[y].ch[0]=t[x].ff=0;
}
bool check(int x,int y)
{
makeroot(x);
return findroot(y)==x;
}
char s[10];
int n,m;
int main()
{
n=gi();m=gi();
while(m--)
{
scanf("%s",s+1);
switch(s[1])
{