From dceb17b890f7ef27718062fab69407f598369c5a Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 15 Jul 2022 15:15:31 +0800 Subject: [PATCH] =?UTF-8?q?102.=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91Link?= =?UTF-8?q?-Cut=20Tree?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/53749 --- S2OJ/102/102.cpp | 209 +++++++++++++++++++++++++++++++++++++ S2OJ/102/data/problem.conf | 3 + S2OJ/102/data/tree1.ans | 3 + S2OJ/102/data/tree1.in | 3 + S2OJ/102/data/tree10.ans | 3 + S2OJ/102/data/tree10.in | 3 + S2OJ/102/data/tree2.ans | 3 + S2OJ/102/data/tree2.in | 3 + S2OJ/102/data/tree3.ans | 3 + S2OJ/102/data/tree3.in | 3 + S2OJ/102/data/tree4.ans | 3 + S2OJ/102/data/tree4.in | 3 + S2OJ/102/data/tree5.ans | 3 + S2OJ/102/data/tree5.in | 3 + S2OJ/102/data/tree6.ans | 3 + S2OJ/102/data/tree6.in | 3 + S2OJ/102/data/tree7.ans | 3 + S2OJ/102/data/tree7.in | 3 + S2OJ/102/data/tree8.ans | 3 + S2OJ/102/data/tree8.in | 3 + S2OJ/102/data/tree9.ans | 3 + S2OJ/102/data/tree9.in | 3 + 22 files changed, 272 insertions(+) create mode 100644 S2OJ/102/102.cpp create mode 100644 S2OJ/102/data/problem.conf create mode 100644 S2OJ/102/data/tree1.ans create mode 100644 S2OJ/102/data/tree1.in create mode 100644 S2OJ/102/data/tree10.ans create mode 100644 S2OJ/102/data/tree10.in create mode 100644 S2OJ/102/data/tree2.ans create mode 100644 S2OJ/102/data/tree2.in create mode 100644 S2OJ/102/data/tree3.ans create mode 100644 S2OJ/102/data/tree3.in create mode 100644 S2OJ/102/data/tree4.ans create mode 100644 S2OJ/102/data/tree4.in create mode 100644 S2OJ/102/data/tree5.ans create mode 100644 S2OJ/102/data/tree5.in create mode 100644 S2OJ/102/data/tree6.ans create mode 100644 S2OJ/102/data/tree6.in create mode 100644 S2OJ/102/data/tree7.ans create mode 100644 S2OJ/102/data/tree7.in create mode 100644 S2OJ/102/data/tree8.ans create mode 100644 S2OJ/102/data/tree8.in create mode 100644 S2OJ/102/data/tree9.ans create mode 100644 S2OJ/102/data/tree9.in diff --git a/S2OJ/102/102.cpp b/S2OJ/102/102.cpp new file mode 100644 index 00000000..15612ce7 --- /dev/null +++ b/S2OJ/102/102.cpp @@ -0,0 +1,209 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + unsigned v, s; + bool rev; + + node() + : 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]; + + inline void pushup(size_t u) { + tr[u].s = tr[tr[u].l].s ^ tr[u].v ^ tr[tr[u].r].s; + } + + inline void pushdown(const size_t &u) { + if (!tr[u].rev) return; + + std::swap(tr[u].l, tr[u].r); + tr[tr[u].l].rev = !tr[tr[u].l].rev; + tr[tr[u].r].rev = !tr[tr[u].r].rev; + tr[u].rev = false; + } + + unsigned relation(const size_t &u) { + return u == tr[tr[u].f].l ? 0 : 1; + } + + 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; + + if (tr[u].child(x ^ 1)) { + tr[tr[u].child(x ^ 1)].f = p; + } + tr[p].child(x) = tr[u].child(x ^ 1); + + tr[u].child(x ^ 1) = p; + tr[p].f = u; + + pushup(p); + pushup(u); + } + + void splay(size_t u) { + std::stack st; + + size_t cur = u; + st.push(cur); + while (!isRoot(cur)) { + st.push(tr[cur].f); + cur = tr[cur].f; + } + + while (!st.empty()) { + pushdown(st.top()); + st.pop(); + } + + while (!isRoot(u)) { + if (isRoot(tr[u].f)) { + rotate(u); + } else if (relation(u) == relation(tr[u].f)) { + rotate(tr[u].f); + rotate(u); + } else { + rotate(u); + rotate(u); + } + } + } + + void access(size_t u) { + for (size_t f = 0; u; u = tr[f = u].f) { + 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) { + u = tr[u].l; + } + + return u; + } + + void split(const size_t &x, const size_t &y) { + makeRoot(x); + access(y); + splay(y); + } + + public: + void set(int p, int v) { + tr[p].s = tr[p].v = v; + } + + unsigned query(int x, int y) { + split(x, y); + + return tr[y].s; + } + + void link(const int &x, const int &y) { + makeRoot(x); + + if (findRoot(y) != x) { + tr[x].f = y; + } + } + + void cut(int x, int y) { + split(x, y); + + if (tr[y].l == x) { + tr[y].l = 0; + tr[x].f = 0; + } + } + + void change(int p, int v) { + access(p); + splay(p); + tr[p].v = v; + pushup(p); + } +} lct; + +int n, m; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + lct.set(i, x); + } + + while (m--) { + int op, x, y; + + cin >> op >> x >> y; + + switch (op) { + case 0: { + cout << lct.query(x, y) << endl; + + break; + } + case 1: { + lct.link(x, y); + + break; + } + case 2: { + lct.cut(x, y); + + break; + } + case 3: { + lct.change(x, y); + + break; + } + } + } + + return 0; +} diff --git a/S2OJ/102/data/problem.conf b/S2OJ/102/data/problem.conf new file mode 100644 index 00000000..5131809a --- /dev/null +++ b/S2OJ/102/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:81177b3583cc6905d4a9637b5237829596767010c49c8f433e457f3426e07fb1 +size 193 diff --git a/S2OJ/102/data/tree1.ans b/S2OJ/102/data/tree1.ans new file mode 100644 index 00000000..e0fca11a --- /dev/null +++ b/S2OJ/102/data/tree1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:487bbad82294aa14f2d1a6ff7355771208dc9e2dc8149e20a068cfe7dfb87340 +size 269885 diff --git a/S2OJ/102/data/tree1.in b/S2OJ/102/data/tree1.in new file mode 100644 index 00000000..1cc0f65e --- /dev/null +++ b/S2OJ/102/data/tree1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:942eeed478c5a181f63cd15a685d3bbed419f35343b6234386fa1669ddc24b8c +size 1693097 diff --git a/S2OJ/102/data/tree10.ans b/S2OJ/102/data/tree10.ans new file mode 100644 index 00000000..ef9b8acf --- /dev/null +++ b/S2OJ/102/data/tree10.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:53ce624b8d42e7a244a5a769a73ae480b667e8ea97a708157ffce4e3e8190016 +size 384849 diff --git a/S2OJ/102/data/tree10.in b/S2OJ/102/data/tree10.in new file mode 100644 index 00000000..3b082e92 --- /dev/null +++ b/S2OJ/102/data/tree10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:66c10394d6b00965ea07f8857e5a324caa2f4c64d86b21dffa9b68d70f42eb9f +size 2426607 diff --git a/S2OJ/102/data/tree2.ans b/S2OJ/102/data/tree2.ans new file mode 100644 index 00000000..fb04baed --- /dev/null +++ b/S2OJ/102/data/tree2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b0d9b8c199a49751ee557e626eaf29b425442235b316ac005e8b1cf2095e1e97 +size 2467 diff --git a/S2OJ/102/data/tree2.in b/S2OJ/102/data/tree2.in new file mode 100644 index 00000000..0f2cfce1 --- /dev/null +++ b/S2OJ/102/data/tree2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eee28e01b27b6f436f4a14426644111c7874330196a7ed41bc085fcffa87f83b +size 21146 diff --git a/S2OJ/102/data/tree3.ans b/S2OJ/102/data/tree3.ans new file mode 100644 index 00000000..110d4046 --- /dev/null +++ b/S2OJ/102/data/tree3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7db423cbf162004fdfa201b28a15d2668151a125d48320dd590592c6b6a1a62f +size 2135393 diff --git a/S2OJ/102/data/tree3.in b/S2OJ/102/data/tree3.in new file mode 100644 index 00000000..adf04a5c --- /dev/null +++ b/S2OJ/102/data/tree3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ebc429cbc055eb463eea78b8e65c2adebf85773c4f086ce6e59d8cecf0607499 +size 4466295 diff --git a/S2OJ/102/data/tree4.ans b/S2OJ/102/data/tree4.ans new file mode 100644 index 00000000..3df86438 --- /dev/null +++ b/S2OJ/102/data/tree4.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e1b39118d817ef1a1153697f111de4f06e63d9c58a6b11f9cde7bb3e8c60a6c4 +size 1494779 diff --git a/S2OJ/102/data/tree4.in b/S2OJ/102/data/tree4.in new file mode 100644 index 00000000..e6f58728 --- /dev/null +++ b/S2OJ/102/data/tree4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e796aa7ae4cef11a758e2fb91db4c93fb6c22b64349c5cefd0357c070199154 +size 2571795 diff --git a/S2OJ/102/data/tree5.ans b/S2OJ/102/data/tree5.ans new file mode 100644 index 00000000..6d7c3e4c --- /dev/null +++ b/S2OJ/102/data/tree5.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4fe6573dd935cc13060d11ad21f44dcb9667debe30d10a0f2dd52a97e00d3dfc +size 38869 diff --git a/S2OJ/102/data/tree5.in b/S2OJ/102/data/tree5.in new file mode 100644 index 00000000..480d6970 --- /dev/null +++ b/S2OJ/102/data/tree5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2f3a2e512ba6ff1da54e09124e0c77849e51fe396303d20e2c3bfba0b52b084c +size 224122 diff --git a/S2OJ/102/data/tree6.ans b/S2OJ/102/data/tree6.ans new file mode 100644 index 00000000..dee295ba --- /dev/null +++ b/S2OJ/102/data/tree6.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:925b6fb3d451806574741899bb649364ef3adf4e71b0cd7316179fe5c1e9398f +size 153604 diff --git a/S2OJ/102/data/tree6.in b/S2OJ/102/data/tree6.in new file mode 100644 index 00000000..d4c408d7 --- /dev/null +++ b/S2OJ/102/data/tree6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:005a6a288e7de5e8d71f4ff0e61992475a2e88b01958b67e4c225b3a7b529be6 +size 958470 diff --git a/S2OJ/102/data/tree7.ans b/S2OJ/102/data/tree7.ans new file mode 100644 index 00000000..f1df9ef0 --- /dev/null +++ b/S2OJ/102/data/tree7.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:154b6b2c5c006716f04f918f2aa8d88386c107310f8c0daab27c56e1457fd87f +size 1154858 diff --git a/S2OJ/102/data/tree7.in b/S2OJ/102/data/tree7.in new file mode 100644 index 00000000..1548f088 --- /dev/null +++ b/S2OJ/102/data/tree7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:19d050989f0ca9c97d3135321b3ccf82b1a701f8644ac19a97b347da2713ec3e +size 7690444 diff --git a/S2OJ/102/data/tree8.ans b/S2OJ/102/data/tree8.ans new file mode 100644 index 00000000..ede63406 --- /dev/null +++ b/S2OJ/102/data/tree8.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0850fd74de82a693dc1f9ed470b092acc5c5304a394e131a321bfbe45bf03d0b +size 77290 diff --git a/S2OJ/102/data/tree8.in b/S2OJ/102/data/tree8.in new file mode 100644 index 00000000..c5ce06ac --- /dev/null +++ b/S2OJ/102/data/tree8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:543f78df01ef034bfea33e1ca53f68ce92d1f0e638835795c791bda6fd74e135 +size 468289 diff --git a/S2OJ/102/data/tree9.ans b/S2OJ/102/data/tree9.ans new file mode 100644 index 00000000..bbe3f97b --- /dev/null +++ b/S2OJ/102/data/tree9.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a14fa1568da1b584766284081861c3022d051c26af8733f3e45a875ee18430c6 +size 4023 diff --git a/S2OJ/102/data/tree9.in b/S2OJ/102/data/tree9.in new file mode 100644 index 00000000..b159285c --- /dev/null +++ b/S2OJ/102/data/tree9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ac4d88896dddba22879b1232945ed954394a5e140b4144dcb873f10347b181d +size 20575