From b57c4abbd177f72de719b5965a02d5f9f2010a83 Mon Sep 17 00:00:00 2001 From: TooYoungTooSimp <6648049+TooYoungTooSimp@users.noreply.github.com> Date: Mon, 3 Apr 2017 11:05:06 +0800 Subject: [PATCH] bzoj 2049 LCT --- OnlineJudges/lydsy/2049.cpp | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 OnlineJudges/lydsy/2049.cpp diff --git a/OnlineJudges/lydsy/2049.cpp b/OnlineJudges/lydsy/2049.cpp new file mode 100644 index 0000000..85ab838 --- /dev/null +++ b/OnlineJudges/lydsy/2049.cpp @@ -0,0 +1,102 @@ +#include +#include +template +inline void swap(T &x, T &y) +{ + T t = x; + x = y; + y = t; +} +inline void read(int &x) +{ + int ch = x = 0; + while (!isdigit(ch)) ch = getchar(); + for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; +} +struct node +{ + bool rev; + node *fa, *ch[2]; + int dir() { return fa == 0 ? -1 : this == fa->ch[1]; } + bool isRoot() { return fa == 0 || (fa->ch[0] != this && fa->ch[1] != this); } +} T[10001]; +inline void push_down(node *x) +{ + if (x == 0) return; + if (x->rev) + { + swap(x->ch[0], x->ch[1]); + if (x->ch[0]) x->ch[0]->rev ^= 1; + if (x->ch[1]) x->ch[1]->rev ^= 1; + x->rev = false; + } +} +void push_down_recursive(node *x) +{ + if (!x->isRoot()) push_down_recursive(x->fa); + push_down(x); +} +inline void liftup(node *x) +{ + if (x == 0 || x->isRoot()) return; + int d = x->dir(); + node *f = x->fa, *ff = f->fa, *c = x->ch[d ^ 1]; + if (ff && !f->isRoot()) ff->ch[f->dir()] = x; + x->fa = ff, f->fa = x, x->ch[d ^ 1] = f, f->ch[d] = c; + if (c) c->fa = f; +} +inline void Splay(node *x) +{ + push_down_recursive(x); + for (; !x->isRoot(); liftup(x)) + if (!x->fa->isRoot()) + liftup((x->dir() ^ x->fa->dir()) ? x : x->fa); +} +inline void access(node *x) +{ + for (node *y = 0; x; y = x, x = x->fa) + { + Splay(x), x->ch[1] = y; + if (y) y->fa = x; + } +} +inline node *findRoot(node *x) +{ + access(x), Splay(x); + while (push_down(x), x->ch[0]) x = x->ch[0]; + Splay(x); + return x; +} +inline void makeRoot(node *x) +{ + access(x), Splay(x); + x->rev ^= 1; +} +inline void Link(node *x, node *f) +{ + makeRoot(x); + x->fa = f; +} +inline void Cut(node *x, node *y) +{ + makeRoot(x), access(y), Splay(y); + x->fa = y->ch[0] = 0; +} +int main() +{ + int n, m, u, v; + read(n), read(m); + while (m--) + { + int op = getchar(); + while (!isupper(op)) op = getchar(); + read(u), read(v); + if (op == 'Q') + puts(findRoot(&T[u]) == findRoot(&T[v]) ? "Yes" : "No"); + if (op == 'C') + Link(&T[u], &T[v]); + if (op == 'D') + Cut(&T[u], &T[v]); + } + return 0; +} \ No newline at end of file