# 描述

http://www.lydsy.com/JudgeOnline/problem.php?id=2049

# 分析

2.cut(u,v):把u翻到根上去,然后f[v]=0(原来f[v]=u).

3.query(u,v):把u翻到根上去,然后从v开始往上找,看能否找到u.

 1 #include <bits/stdc++.h>
2 using namespace std;
3
4 const int maxn=10000+5;
5 int n,m,f[maxn];
6
7 void root(int x){ for(int t=0,fa=f[x];x;fa=f[x]){ f[x]=t; t=x; x=fa; } }
8 int main(){
9     scanf("%d%d",&n,&m);
10     char c[10];
11     for(int i=1;i<=m;i++){
12         scanf("%s",c);
13         int u,v;
14         scanf("%d%d",&u,&v);
15         root(u);
16         if(c[0]=='C') f[u]=v;
17         else if(c[0]=='D') f[v]=0;
18         else{
19             for(;v!=u&&v;v=f[v]);
20             puts(v==u?"Yes":"No");
21         }
22     }
23     return 0;
24 }
View Code

 1 #include <bits/stdc++.h>
2 using namespace std;
3
4 const int maxn=10000+5;
5 int n,m;
6
7 struct node{
8     node* ch[2],* pa;
9     bool rev;
10     node(node* x):rev(false){ ch[0]=ch[1]=pa=x; }
11     bool d(){ return pa->ch[1]==this; }
12     bool c(){ return pa->ch[0]==this||pa->ch[1]==this; }
13     void setc(node* x,bool d){ ch[d]=x; x->pa=this; }
14     void push_down(){
15         if(rev){
16             ch[0]->rev^=true;
17             ch[1]->rev^=true;
18             swap(ch[0],ch[1]);
19             rev=false;
20         }
21     }
22 }* null,* t[maxn];
23 void rot(node* x){
24     node* pa=x->pa; bool d=x->d();
25     pa->push_down(); x->push_down();
26     if(pa->c()) pa->pa->setc(x,pa->d());
27     else x->pa=pa->pa;
28     pa->setc(x->ch[!d],d);
29     x->setc(pa,!d);
30 }
31 void fix(node* x){
32     if(x->c()) fix(x->pa);
33     x->push_down();
34 }
35 void splay(node* x){
36     fix(x);
37     while(x->c())
38         if(!x->pa->c()) rot(x);
39         else x->d()==x->pa->d()?(rot(x->pa),rot(x)):(rot(x),rot(x));
40 }
41 void access(node* x){
42     node* t=x;
43     for(node* y=null;x!=null;y=x, x=x->pa){
44         splay(x);
45         x->ch[1]=y;
46     }
47     splay(t);
48 }
49 node* find_root(node* x){
50     access(x);
51     for(;x->ch[0]!=null;x=x->ch[0]);
52     return x;
53 }
54 void make_root(node* x){
55     access(x);
56     x->rev^=true;
57 }
59     make_root(x);
60     x->pa=y;
61 }
62 void cut(node* x,node* y){
63     make_root(x);
64     access(y);
65     x->pa=null; y->ch[0]=null;
66 }
67 int main(){
68     null=new node(NULL);
69     scanf("%d%d",&n,&m);
70     for(int i=1;i<=n;i++) t[i]=new node(null);
71     for(int i=1;i<=m;i++){
72         int u,v; char c[10];
73         scanf("%s%d%d",c,&u,&v);
75         else if(c[0]=='D') cut(t[u],t[v]);
76         else puts(find_root(t[u])==find_root(t[v])?"Yes":"No");
77     }
78     return 0;
79 }
View Code

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

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

posted @ 2016-05-27 23:06  晴歌。  阅读(...)  评论(... 编辑 收藏