# 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT

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模板题，这题不需要pushup

#include <cstdio>
#include <algorithm>
#define N 10010
#define lson c[0][x]
#define rson c[1][x]
using namespace std;
int fa[N] , c[2][N] , rev[N];
char str[10];
void pushdown(int x)
{
if(rev[x])
{
swap(c[0][lson] , c[1][lson]);
swap(c[0][rson] , c[1][rson]);
rev[lson] ^= 1 , rev[rson] ^= 1;
rev[x] = 0;
}
}
bool isroot(int x)
{
return c[0][fa[x]] != x && c[1][fa[x]] != x;
}
void update(int x)
{
if(!isroot(x)) update(fa[x]);
pushdown(x);
}
void rotate(int x)
{
int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
if(!isroot(y)) c[c[1][z] == y][z] = x;
fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
}
void splay(int x)
{
update(x);
while(!isroot(x))
{
int y = fa[x] , z = fa[y];
if(!isroot(y))
{
if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x) splay(x) , rson = t , t = x , x = fa[x];
}
void makeroot(int x)
{
access(x) , splay(x);
swap(lson , rson) , rev[x] ^= 1;
}
void link(int x , int y)
{
makeroot(x) , fa[x] = y;
}
void cut(int x , int y)
{
makeroot(x) , access(y) , splay(y) , c[0][y] = fa[x] = 0;
}
int find(int x)
{
access(x) , splay(x);
while(lson) pushdown(x) , x = lson;
return x;
}
int main()
{
int n , m , x , y;
scanf("%d%d" , &n , &m);
while(m -- )
{
scanf("%s%d%d" , str , &x , &y);
if(str[0] == 'C') link(x , y);
else if(str[0] == 'D') cut(x , y);
else printf("%s\n" , find(x) == find(y) ? "Yes" : "No");
}
return 0;
}
posted @ 2017-05-02 09:22  GXZlegend  阅读(142)  评论(0编辑  收藏