# [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

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

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

## Source

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 using namespace std;
9     char ch=getchar();int x=0,f=1;
10     for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
11     for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
12     return x*f;
13 }
14 int n,m;
15 struct data {
16     int s[2],fa;
17     bool re;
18     data() {fa=0;re=0;s[0]=s[1]=0;}
19 }a[10005];
20 bool isroot(int x) {return a[a[x].fa].s[0]!=x&&a[a[x].fa].s[1]!=x;}
21 void pushdown(int x){
22     if(a[x].re) {
23         int l=a[x].s[0],r=a[x].s[1];
24         a[x].re^=1;a[l].re^=1;a[r].re^=1;
25         swap(a[x].s[0],a[x].s[1]);
26     }
27 }
28 void rorate(int x){
29     int y=a[x].fa,z=a[y].fa;
30     bool l,r;
31     if(a[y].s[0]==x) l=0;else l=1;r=l^1;
32     if(!isroot(y)) if(a[z].s[0]==y) a[z].s[0]=x;else a[z].s[1]=x;
33     a[x].fa=z;a[a[x].s[r]].fa=y;a[y].fa=x;
34     a[y].s[l]=a[x].s[r];a[x].s[r]=y;
35 }
36 void push(int x) {
37     if(!isroot(x)) push(a[x].fa);
38     pushdown(x);
39 }
40 void splay(int x) {
41
42     push(x);
43     while(!isroot(x)) {
44         int y=a[x].fa,z=a[y].fa;
45         if(!isroot(y)) {
46             if(a[y].s[0]==x^a[z].s[0]==y) rorate(x);
47             else rorate(y);
48         }
49         rorate(x);
50     }
51 }
52 void access(int x) {
53     int y=0;
54     while(x){
55         splay(x);
56         a[x].s[1]=y;
57         y=x;x=a[x].fa;
58     }
59 }
60 void mroot(int x) {
61     access(x);
62     splay(x);
63     a[x].re^=1;
64 }
65 void link(int x,int y) {
66     mroot(x);
67     a[x].fa=y;
68     splay(x);
69 }
70 void cut(int x,int y) {
71     mroot(x);
72     access(y);
73     splay(y);
74     a[y].s[0]=a[x].fa=0;
75 }
76 bool check(int x,int y) {
77     while(a[x].fa) x=a[x].fa;
78     while(a[y].fa) y=a[y].fa;
79     return x==y;
80 }
82 int main() {
84     for(int i=1;i<=m;i++) {
94 }