diff --git a/BZOJ/2049/2049.cpp b/BZOJ/2049/2049.cpp new file mode 100644 index 00000000..7d2aa811 --- /dev/null +++ b/BZOJ/2049/2049.cpp @@ -0,0 +1,165 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e4 + 5; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + bool rev; + + node(const size_t &_f = 0) + : l(0), r(0), f(_f), rev(false) {} + + size_t &child(unsigned x) { + return !x ? l : r; + } + } tr[N]; + + 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; + } + + 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; + } + } + + 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: + bool check(int x, int y) { + return findRoot(x) == findRoot(y); + } + + 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; + } + } +} lct; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + + cin >> n >> m; + + while (m--) { + std::string op; + int u, v; + + cin >> op >> u >> v; + + if (op == "Connect") { + lct.link(u, v); + } else if (op == "Destroy") { + lct.cut(u, v); + } else { // op == "Query" + cout << (lct.check(u, v) ? "Yes" : "No") << endl; + } + } + + return 0; +} diff --git a/BZOJ/2049/data/1.in b/BZOJ/2049/data/1.in new file mode 100644 index 00000000..97d06b58 --- /dev/null +++ b/BZOJ/2049/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e8accf6b8950a0666609848ffa4feef185b8f3b4d6bd44055e8f5a18be3042c5 +size 2421092 diff --git a/BZOJ/2049/data/1.out b/BZOJ/2049/data/1.out new file mode 100644 index 00000000..d3890e76 --- /dev/null +++ b/BZOJ/2049/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:45da574588b784528deefe9f9617238431ccba274e04d912dd442413219c410c +size 102242 diff --git a/BZOJ/2049/data/10.in b/BZOJ/2049/data/10.in new file mode 100644 index 00000000..2a588676 --- /dev/null +++ b/BZOJ/2049/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a3a102257e97fcd46c31aa8cff476f34f21fcbb01b313bfdc39fe87581a06d90 +size 1012634 diff --git a/BZOJ/2049/data/10.out b/BZOJ/2049/data/10.out new file mode 100644 index 00000000..f2d7f775 --- /dev/null +++ b/BZOJ/2049/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:342ac530f1988639abf6c9eee716d8f57ae7c11ca6682fea8ccfe2d96360993b +size 43907 diff --git a/BZOJ/2049/data/2.in b/BZOJ/2049/data/2.in new file mode 100644 index 00000000..420ea263 --- /dev/null +++ b/BZOJ/2049/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3fd6186427dbc4334a2d3b7a1075e7bf089f98ae6cdb86fe22d06e98338b04c +size 3124461 diff --git a/BZOJ/2049/data/2.out b/BZOJ/2049/data/2.out new file mode 100644 index 00000000..b3549e6b --- /dev/null +++ b/BZOJ/2049/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d37499f366343eb2b9f5e08602d90135732306fb34a0692eaa1aef99e125f8c8 +size 132961 diff --git a/BZOJ/2049/data/3.in b/BZOJ/2049/data/3.in new file mode 100644 index 00000000..b3e17b81 --- /dev/null +++ b/BZOJ/2049/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:004e48140d9c97336b3c3c394f90922c0f1ee544baed268e9ce595511c4937f6 +size 2066803 diff --git a/BZOJ/2049/data/3.out b/BZOJ/2049/data/3.out new file mode 100644 index 00000000..5c2a2520 --- /dev/null +++ b/BZOJ/2049/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:016e2470734b2d04a16b8b2e5ecea4401f3598c9dd745d15a2e8285c771b081f +size 89130 diff --git a/BZOJ/2049/data/4.in b/BZOJ/2049/data/4.in new file mode 100644 index 00000000..b2e1bd04 --- /dev/null +++ b/BZOJ/2049/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:53317c56fddf7491badb226b209498db69554fa772babf47a37c58fecb996cf2 +size 1715477 diff --git a/BZOJ/2049/data/4.out b/BZOJ/2049/data/4.out new file mode 100644 index 00000000..78d9a980 --- /dev/null +++ b/BZOJ/2049/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a318bc89b3b4edf0c45d541f1f28bfd4a3699e3b1b63514f33a25e83b0cb162 +size 74545 diff --git a/BZOJ/2049/data/5.in b/BZOJ/2049/data/5.in new file mode 100644 index 00000000..7425e68b --- /dev/null +++ b/BZOJ/2049/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a9750a3934d9f21a995f019586a466015eb49fcf659f635f4ad4a5d16c04a5d +size 2769960 diff --git a/BZOJ/2049/data/5.out b/BZOJ/2049/data/5.out new file mode 100644 index 00000000..050dbb40 --- /dev/null +++ b/BZOJ/2049/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9f556efd0e59c1123435cd0fc774b7401aea464877a9672ac93225c6aaa48d0d +size 117533 diff --git a/BZOJ/2049/data/6.in b/BZOJ/2049/data/6.in new file mode 100644 index 00000000..6538ad20 --- /dev/null +++ b/BZOJ/2049/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd885871735d10062d7800af6e24536a19fb9afd905a321b6fb1b7bbfea056d1 +size 3474167 diff --git a/BZOJ/2049/data/6.out b/BZOJ/2049/data/6.out new file mode 100644 index 00000000..469dcfc5 --- /dev/null +++ b/BZOJ/2049/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f39ea3f87be966e576e8f5b1043e50bb2f3f838bf170ec61b22341c24bbd6d6 +size 147753 diff --git a/BZOJ/2049/data/7.in b/BZOJ/2049/data/7.in new file mode 100644 index 00000000..d00e3360 --- /dev/null +++ b/BZOJ/2049/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:17da133be40fd5a278fcdeddcbc091552d806b434c3243df42fa135704106ccd +size 1363185 diff --git a/BZOJ/2049/data/7.out b/BZOJ/2049/data/7.out new file mode 100644 index 00000000..f40d35a0 --- /dev/null +++ b/BZOJ/2049/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:84a5a757af698a37bc7822b2ba41608c41f21048f5d9276dd493ec6965944098 +size 59912 diff --git a/BZOJ/2049/data/8.in b/BZOJ/2049/data/8.in new file mode 100644 index 00000000..96201ec0 --- /dev/null +++ b/BZOJ/2049/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:adb105dd97a1d54f4385ac83beeebbc7162eb64ec00d94f764b7c9ebad2882fa +size 308086 diff --git a/BZOJ/2049/data/8.out b/BZOJ/2049/data/8.out new file mode 100644 index 00000000..0e79eaa1 --- /dev/null +++ b/BZOJ/2049/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:303bac05393b109a0b5a9ce12009adc9e4e8c5a1ca8505193493b0135690ef17 +size 14478 diff --git a/BZOJ/2049/data/9.in b/BZOJ/2049/data/9.in new file mode 100644 index 00000000..e9db7a5b --- /dev/null +++ b/BZOJ/2049/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6b4c9de48a85c67a29265a4e04339a7fcd5e0f990ae844ba5e9b2f512abb147 +size 659826 diff --git a/BZOJ/2049/data/9.out b/BZOJ/2049/data/9.out new file mode 100644 index 00000000..4b2caf0c --- /dev/null +++ b/BZOJ/2049/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e65d1f78464f2c793f36f076ddefa6fdd8504aeb4173d31bc5a008f427a09f3 +size 29335