# Brief Description

1. 链接两个节点。
2. 断开两个节点之间的链接。

# Algorithm Design

lct的核心操作是access。access操作可以把虚边变为实边，通过改变splay的拓扑结构来维护实边。

# Code

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <stack>
using std::stack;
const int maxn = 10005;
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m;
int fa[maxn], ch[maxn][2];
bool rev[maxn];
inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
void pushdown(int k) {
if (rev[k]) {
rev[k] = 0;
rev[ch[k][0]] ^= 1;
rev[ch[k][1]] ^= 1;
std::swap(ch[k][0], ch[k][1]);
}
}
void zig(int x) {
int y = fa[x], z = fa[y], l = (ch[y][1] == x), r = l ^ 1;
if (!isroot(y))
ch[z][ch[z][1] == y] = x;
fa[ch[y][l] = ch[x][r]] = y;
fa[ch[x][r] = y] = x;
fa[x] = z;
}
void splay(int x) {
stack<int> st;
st.push(x);
for (int i = x; !isroot(i); i = fa[i])
st.push(fa[i]);
while (!st.empty()) {
pushdown(st.top());
st.pop();
}
for (int y = fa[x]; !isroot(x); zig(x), y = fa[x])
if (!isroot(y))
zig((ch[fa[y]][0] == y) == (ch[y][0] == x) ? y : x);
}
void access(int x) {
int t = 0;
while (x) {
splay(x);
ch[x][1] = t;
t = x;
x = fa[x];
}
}
void rever(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
void link(int x, int y) {
rever(x);
fa[x] = y;
splay(x);
}
void cut(int x, int y) {
rever(x);
access(y);
splay(y);
ch[y][0] = fa[x] = 0;
}
int find(int x) {
access(x);
splay(x);
int y = x;
while (ch[y][0])
y = ch[y][0];
return y;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
#endif
char ch[10];
int x, y;
for (int i = 1; i <= m; i++) {
scanf("%s", ch);
if (ch[0] == 'C')
else if (ch[0] == 'D')
cut(x, y);
else {
if (find(x) == find(y))
printf("Yes\n");
else
printf("No\n");
}
}
}


posted on 2017-03-08 18:08  蒟蒻konjac  阅读(88)  评论(0编辑  收藏

• 随笔 - 168
• 文章 - 0
• 评论 - 33