0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 14:18:47 +00:00

P3690 【模板】动态树(Link Cut Tree)

https://www.luogu.com.cn/record/79663457
This commit is contained in:
Baoshuo Ren 2022-07-15 14:35:19 +08:00
parent 5648f2252f
commit 0ea3413730
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,6 +1,4 @@
#include <iostream> #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <stack> #include <stack>
using std::cin; using std::cin;
@ -9,171 +7,158 @@ const char endl = '\n';
const int N = 1e5 + 5; const int N = 1e5 + 5;
// Link-Cut Tree
class LinkCutTree { class LinkCutTree {
private: private:
std::stack<bool> st;
struct node { struct node {
int p, // 父亲节点 size_t l, r, f;
l, // 左儿子 unsigned v, s;
r; // 右儿子 bool rev;
int pre;
int val, // 节点值
sum; // 异或和
int key; // 权值
bool rev; // 翻转标记
node() node()
: p(0), l(0), r(0), pre(0), val(0), sum(0), key(rand()), rev(false) {} : l(0), r(0), f(0), s(0), v(0), rev(false) {}
node(unsigned _v, size_t _f)
: l(0), r(0), f(_f), s(_v), v(_v), rev(false) {}
size_t &child(unsigned x) {
return !x ? l : r;
}
} tr[N]; } tr[N];
void pushup(int u) { inline void pushup(size_t u) {
// 计算异或和 tr[u].s = tr[tr[u].l].s ^ tr[u].v ^ tr[tr[u].r].s;
tr[u].sum = tr[tr[u].l].sum ^ tr[u].val ^ tr[tr[u].r].sum;
// 标记父亲节点
if (tr[u].l) tr[tr[u].l].p = u;
if (tr[u].r) tr[tr[u].r].p = u;
} }
void pushdown(int u) { inline void pushdown(const size_t &u) {
if (!tr[u].rev) return; if (!tr[u].rev) return;
tr[u].rev = false;
std::swap(tr[u].l, tr[u].r); std::swap(tr[u].l, tr[u].r);
tr[tr[u].l].rev ^= 1; tr[tr[u].l].rev = !tr[tr[u].l].rev;
tr[tr[u].r].rev ^= 1; tr[tr[u].r].rev = !tr[tr[u].r].rev;
tr[u].rev = false;
} }
std::pair<int, int> split(int u) { unsigned relation(const size_t &u) {
if (st.empty()) { return u == tr[tr[u].f].l ? 0 : 1;
pushdown(u); }
auto t = std::make_pair(u, tr[u].r);
tr[u].r = 0;
pushup(u);
return t; bool isRoot(const size_t &u) {
return tr[tr[u].f].l != u && tr[tr[u].f].r != u;
}
void rotate(size_t u) {
size_t p = tr[u].f;
unsigned x = relation(u);
if (!isRoot(p)) {
tr[tr[p].f].child(relation(p)) = u;
} }
tr[u].f = tr[p].f;
bool d = st.top() ^ tr[u].rev; if (tr[u].child(x ^ 1)) {
st.pop(); tr[tr[u].child(x ^ 1)].f = p;
pushdown(u);
if (d) {
auto t = split(tr[u].l);
tr[u].l = t.second;
pushup(u);
return std::make_pair(t.first, u);
} }
tr[p].child(x) = tr[u].child(x ^ 1);
auto t = split(tr[u].r); tr[u].child(x ^ 1) = p;
tr[u].r = t.first; tr[p].f = u;
pushup(p);
pushup(u); pushup(u);
return std::make_pair(u, t.second);
} }
// 合并 void splay(size_t u) {
int merge(int x, int y) { std::stack<size_t> st;
if (!x || !y) return x | y;
if (tr[x].key < tr[y].key) { size_t cur = u;
pushdown(x); st.push(cur);
tr[x].r = merge(tr[x].r, y); while (!isRoot(cur)) {
pushup(x); st.push(tr[cur].f);
return x; cur = tr[cur].f;
} }
pushdown(y); while (!st.empty()) {
tr[y].l = merge(x, tr[y].l); pushdown(st.top());
pushup(y); st.pop();
return y; }
}
// 是否是根节点
bool isRoot(int u) {
return !tr[u].p || (tr[tr[u].p].l != u && tr[tr[u].p].r != u);
}
// 查找根节点
int findRoot(int u) {
while (!st.empty()) st.pop();
while (!isRoot(u)) { while (!isRoot(u)) {
// pushdown(u); if (isRoot(tr[u].f)) {
st.push(tr[tr[u].p].l == u); rotate(u);
u = tr[u].p; } else if (relation(u) == relation(tr[u].f)) {
rotate(tr[u].f);
rotate(u);
} else {
rotate(u);
rotate(u);
}
} }
return u;
} }
int findLeft(int u) { void access(size_t u) {
u = findRoot(u); for (size_t f = 0; u; u = tr[f = u].f) {
pushdown(u); splay(u);
tr[u].r = f;
pushup(u);
}
}
void makeRoot(const size_t &u) {
access(u);
splay(u);
tr[u].rev = !tr[u].rev;
}
size_t findRoot(size_t u) {
access(u);
splay(u);
while (tr[u].l) { while (tr[u].l) {
u = tr[u].l; u = tr[u].l;
pushdown(u);
} }
return u; return u;
} }
int access(int u) { void split(const size_t &x, const size_t &y) {
int lst = 0; makeRoot(x);
access(y);
while (u) { splay(y);
auto t = split(findRoot(u));
tr[findLeft(lst)].pre = 0;
lst = merge(t.first, lst);
tr[findLeft(t.second)].pre = u;
u = tr[findLeft(lst)].pre;
}
return lst;
}
void makeRoot(int u) {
tr[access(u)].rev ^= 1;
} }
public: public:
int getRoot(int u) { void set(int p, int v) {
return findLeft(access(u)); tr[p].s = tr[p].v = v;
} }
void link(int x, int y) { unsigned query(int x, int y) {
split(x, y);
return tr[y].s;
}
void link(const int &x, const int &y) {
makeRoot(x); makeRoot(x);
tr[x].pre = y;
if (findRoot(y) != x) {
tr[x].f = y;
}
} }
void cut(int x, int y) { void cut(int x, int y) {
makeRoot(x); split(x, y);
access(y);
access(x); if (tr[y].l == x) {
tr[y].pre = 0; tr[y].l = 0;
tr[x].f = 0;
}
} }
int query(int x, int y) { void change(int p, int v) {
makeRoot(x); access(p);
access(y); splay(p);
tr[p].v = v;
auto t = split(findRoot(y)); pushup(p);
int res = tr[t.first].sum;
merge(t.first, t.second);
return res;
}
void change(int u, int val) {
makeRoot(u);
auto t = split(findRoot(u));
tr[u].val = val;
merge(t.first, t.second);
}
void set(int u, int val) {
tr[u].sum = tr[u].val = val;
} }
} lct; } lct;
@ -203,9 +188,7 @@ int main() {
break; break;
} }
case 1: { case 1: {
if (lct.getRoot(x) != lct.getRoot(y)) { lct.link(x, y);
lct.link(x, y);
}
break; break;
} }