diff --git a/S2OJ/1911/1911.cpp b/S2OJ/1911/1911.cpp new file mode 100644 index 00000000..c9bd6523 --- /dev/null +++ b/S2OJ/1911/1911.cpp @@ -0,0 +1,175 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +int n, m; +int id[N], rid[N], fa[N], dep[N], siz[N], son[N], top[N]; +std::vector g[N]; + +struct node { + int l, r, m, d; + + node(const int &_l = 0, const int &_r = 0) + : l(_l), r(_r), m(0), d(0) {} +} tr[N << 2]; + +void dfs1(int u, int f) { + fa[u] = f; + dep[u] = dep[f] + 1; + siz[u] = 1; + + for (int v : g[u]) { + if (v == f) continue; + + dfs1(v, u); + siz[u] += siz[v]; + + if (siz[son[u]] < siz[v]) son[u] = v; + } +} + +void dfs2(int u, int t) { + static int cnt = 0; + + rid[id[u] = ++cnt] = u; + top[u] = t; + + if (son[u]) dfs2(son[u], t); + + for (int v : g[u]) { + if (v == fa[u] || v == son[u]) continue; + + dfs2(v, v); + } +} + +void pushup(int u) { + tr[u].m = std::max(tr[u << 1].m, tr[u << 1 | 1].m); +} + +void pushdown(int u) { + if (!tr[u].d) return; + + tr[u << 1].m += tr[u].d; + tr[u << 1].d += tr[u].d; + + tr[u << 1 | 1].m += tr[u].d; + tr[u << 1 | 1].d += tr[u].d; + + tr[u].d = 0; +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + + if (l == r) return; + + int mid = (l + r) >> 1; + + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); + + pushup(u); +} + +void modify(int u, int l, int r, int x) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].m += x; + tr[u].d += x; + + return; + } + + int mid = (tr[u].l + tr[u].r) >> 1; + + pushdown(u); + + if (l <= mid) modify(u << 1, l, r, x); + if (r > mid) modify(u << 1 | 1, l, r, x); + + pushup(u); +} + +int query(int u, int l, int r) { + if (l <= tr[u].l && tr[u].r <= r) return tr[u].m; + + int mid = (tr[u].l + tr[u].r) >> 1; + + pushdown(u); + + int res = 0; + + if (l <= mid) res = std::max(res, query(u << 1, l, r)); + if (r > mid) res = std::max(res, query(u << 1 | 1, l, r)); + + return res; +} + +void modify_tree(int u, int d) { + modify(1, id[u], id[u] + siz[u] - 1, d); +} + +int lca(int u, int v) { + while (top[u] != top[v]) { + if (dep[top[u]] < dep[top[v]]) std::swap(u, v); + u = fa[top[u]]; + } + + return dep[u] < dep[v] ? u : v; +} + +int find(int u, int v) { + while (top[u] != top[v]) { + if (fa[top[v]] == u) return top[v]; + + v = fa[top[v]]; + } + + return son[u]; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, x, y; i < n; i++) { + cin >> x >> y; + + g[x].emplace_back(y); + g[y].emplace_back(x); + } + + dfs1(1, 1); + dfs2(1, 1); + build(1, 1, n); + + for (int i = 1, x, y; i <= m; i++) { + cin >> x >> y; + + if (x != y) { + int g = lca(x, y); + + if (dep[x] < dep[y]) std::swap(x, y); + + if (y == g) { // x 在以 y 为根的子树内 + modify_tree(find(y, x), -1); + modify_tree(x, 1); + } else { + modify_tree(1, -1); + modify_tree(x, 1); + modify_tree(y, 1); + } + } + + cout << i + query(1, 1, n) << endl; + } + + return 0; +} diff --git a/S2OJ/1911/data/ex_magic1.in b/S2OJ/1911/data/ex_magic1.in new file mode 100644 index 00000000..44552377 --- /dev/null +++ b/S2OJ/1911/data/ex_magic1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10a779f90ad1c37cc13e528c490776a4447496258bd89a98d2a94cbaa286d1c3 +size 32 diff --git a/S2OJ/1911/data/ex_magic1.out b/S2OJ/1911/data/ex_magic1.out new file mode 100644 index 00000000..e59e3d54 --- /dev/null +++ b/S2OJ/1911/data/ex_magic1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a1ad737f99cef0b2fd9575aa63bc4c4664152f4c3ff5c1e2a4807cf6db61108 +size 6 diff --git a/S2OJ/1911/data/ex_magic2.in b/S2OJ/1911/data/ex_magic2.in new file mode 100644 index 00000000..04fd36be --- /dev/null +++ b/S2OJ/1911/data/ex_magic2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0e6db68cfc3b0212b1c7d0ab472ee207750018e4de6b3dc969a75ed9dce0989c +size 24 diff --git a/S2OJ/1911/data/ex_magic2.out b/S2OJ/1911/data/ex_magic2.out new file mode 100644 index 00000000..76b7ceb2 --- /dev/null +++ b/S2OJ/1911/data/ex_magic2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6e2b7a040683432de03a18fd8a1939a2fdf82585b364bfc874bdd4095c4cae1 +size 4 diff --git a/S2OJ/1911/data/ex_magic3.in b/S2OJ/1911/data/ex_magic3.in new file mode 100644 index 00000000..2c999fc1 --- /dev/null +++ b/S2OJ/1911/data/ex_magic3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e9e4080b044639e39b4e8bd7e94905d58755347919daf75af37d3fcf55404a9 +size 24 diff --git a/S2OJ/1911/data/ex_magic3.out b/S2OJ/1911/data/ex_magic3.out new file mode 100644 index 00000000..20088ffe --- /dev/null +++ b/S2OJ/1911/data/ex_magic3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f6b49467f595b1a44e442c198b3df4d221e88efcaabc26254f8e0ad4f79b6242 +size 10 diff --git a/S2OJ/1911/data/ex_magic4.in b/S2OJ/1911/data/ex_magic4.in new file mode 100644 index 00000000..4718d414 --- /dev/null +++ b/S2OJ/1911/data/ex_magic4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77d395e6fe8770ed3c19930c2a18a54244419a04440a379dc8c39d1db5a41fe8 +size 7956246 diff --git a/S2OJ/1911/data/ex_magic4.out b/S2OJ/1911/data/ex_magic4.out new file mode 100644 index 00000000..e018d6f6 --- /dev/null +++ b/S2OJ/1911/data/ex_magic4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d89ad799548ff52fe078c8c977c31137736c3a70d629f8ce032cb45e707705b6 +size 1292813 diff --git a/S2OJ/1911/data/magic1.in b/S2OJ/1911/data/magic1.in new file mode 100644 index 00000000..bfe60eb2 --- /dev/null +++ b/S2OJ/1911/data/magic1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2edd6881078e1ffe425db79a94b124fa5fe237acf773b863f822924ddf36741b +size 54150 diff --git a/S2OJ/1911/data/magic1.out b/S2OJ/1911/data/magic1.out new file mode 100644 index 00000000..273cf6eb --- /dev/null +++ b/S2OJ/1911/data/magic1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0cf0df6a06705d8278b270dea46eae816d6d4a3033b56f96f7df3587ed480475 +size 8513 diff --git a/S2OJ/1911/data/magic10.in b/S2OJ/1911/data/magic10.in new file mode 100644 index 00000000..9ddec579 --- /dev/null +++ b/S2OJ/1911/data/magic10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7e568edcad4ec99642848683a9174595437518117a394f23da4e09ca9781e3b6 +size 7883011 diff --git a/S2OJ/1911/data/magic10.out b/S2OJ/1911/data/magic10.out new file mode 100644 index 00000000..d65917cb --- /dev/null +++ b/S2OJ/1911/data/magic10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:89e4dcff0978f693e8d764ab08769301ca3a98818a0b3da4f3211e9775ee7765 +size 1951835 diff --git a/S2OJ/1911/data/magic2.in b/S2OJ/1911/data/magic2.in new file mode 100644 index 00000000..35174b85 --- /dev/null +++ b/S2OJ/1911/data/magic2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:834a98258677416c14a570fa864e98a16ca573a91083f8f739f93c744a3d85cf +size 54061 diff --git a/S2OJ/1911/data/magic2.out b/S2OJ/1911/data/magic2.out new file mode 100644 index 00000000..635415db --- /dev/null +++ b/S2OJ/1911/data/magic2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d0348cb96bbdd785cda872667ef2ec6e4bc40e50f0dd14ea1cd479652f04489c +size 8710 diff --git a/S2OJ/1911/data/magic3.in b/S2OJ/1911/data/magic3.in new file mode 100644 index 00000000..539927eb --- /dev/null +++ b/S2OJ/1911/data/magic3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3430eee15b2b79678e009d3e8254a024ac8d01578375b0ae90dc5888be552a52 +size 6565692 diff --git a/S2OJ/1911/data/magic3.out b/S2OJ/1911/data/magic3.out new file mode 100644 index 00000000..d837ab03 --- /dev/null +++ b/S2OJ/1911/data/magic3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:24f17a5c1f927f779f2412fb610dbb14ff8e27973fe82dd256ea1e67c5c3d898 +size 762809 diff --git a/S2OJ/1911/data/magic4.in b/S2OJ/1911/data/magic4.in new file mode 100644 index 00000000..9c1b767c --- /dev/null +++ b/S2OJ/1911/data/magic4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05ff61cb5672fa6773b60f1e908f80d11eb6e85cbd8734574d7a44f8f2fad881 +size 7881696 diff --git a/S2OJ/1911/data/magic4.out b/S2OJ/1911/data/magic4.out new file mode 100644 index 00000000..1875a53f --- /dev/null +++ b/S2OJ/1911/data/magic4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c43dba04082cb73d66f0089ececb4536816dad37c308d7580db6cdec06030c4 +size 1951711 diff --git a/S2OJ/1911/data/magic5.in b/S2OJ/1911/data/magic5.in new file mode 100644 index 00000000..606214d8 --- /dev/null +++ b/S2OJ/1911/data/magic5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a1c3369b803b80b85ffefb9a1b49a8bde25957aeefb5b6ea6ab13abe4c7f080 +size 7881846 diff --git a/S2OJ/1911/data/magic5.out b/S2OJ/1911/data/magic5.out new file mode 100644 index 00000000..d867e50d --- /dev/null +++ b/S2OJ/1911/data/magic5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75befadc0ec717e1b1c06db1fb01e8673d5de718c797adb595a74dbb0ecf3915 +size 1951740 diff --git a/S2OJ/1911/data/magic6.in b/S2OJ/1911/data/magic6.in new file mode 100644 index 00000000..93ef6ab4 --- /dev/null +++ b/S2OJ/1911/data/magic6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:13269422c9b6efa1025bcc61288f70f8e32d7dc6a8099790a9e6dc6677385bbe +size 7882266 diff --git a/S2OJ/1911/data/magic6.out b/S2OJ/1911/data/magic6.out new file mode 100644 index 00000000..30309ff1 --- /dev/null +++ b/S2OJ/1911/data/magic6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:04fb7bac15902ac8c0b735d2efdf3bc2039c5713d6d9dc2bcfff1d354ae20554 +size 1952043 diff --git a/S2OJ/1911/data/magic7.in b/S2OJ/1911/data/magic7.in new file mode 100644 index 00000000..fbfc78ca --- /dev/null +++ b/S2OJ/1911/data/magic7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a35c537e675e048ee252118187d9d3c85d177ab367236c1f0ec5a9f86c809634 +size 7880877 diff --git a/S2OJ/1911/data/magic7.out b/S2OJ/1911/data/magic7.out new file mode 100644 index 00000000..24987c93 --- /dev/null +++ b/S2OJ/1911/data/magic7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ee3ab1dc127e9954170b4c6884d868daeddbde88ddcb231dc552d1ac14c01b0 +size 1951611 diff --git a/S2OJ/1911/data/magic8.in b/S2OJ/1911/data/magic8.in new file mode 100644 index 00000000..b6b8ab8c --- /dev/null +++ b/S2OJ/1911/data/magic8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8335e0cef0cd84a081617b6659a5280ad75090eb26b5136b0bf2229e16e43eb +size 7882459 diff --git a/S2OJ/1911/data/magic8.out b/S2OJ/1911/data/magic8.out new file mode 100644 index 00000000..05b27b08 --- /dev/null +++ b/S2OJ/1911/data/magic8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c9221b613e8f35eb79ff1110acea202f60e706b6b1fb03b2c1d8e644ca25c8df +size 1951758 diff --git a/S2OJ/1911/data/magic9.in b/S2OJ/1911/data/magic9.in new file mode 100644 index 00000000..30822eca --- /dev/null +++ b/S2OJ/1911/data/magic9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e64926628d9076eb10c4dd3ade355d639c1ac2fab9a29cb82f01d1f0aed8d06a +size 7882571 diff --git a/S2OJ/1911/data/magic9.out b/S2OJ/1911/data/magic9.out new file mode 100644 index 00000000..651ab277 --- /dev/null +++ b/S2OJ/1911/data/magic9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dab6130476a6c149e31b1c59e955b210992c51d3c77595f603d019d84d5328e2 +size 1951710 diff --git a/S2OJ/1911/data/problem.conf b/S2OJ/1911/data/problem.conf new file mode 100644 index 00000000..cf340205 --- /dev/null +++ b/S2OJ/1911/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:54bb154cba725596162777ed21f8a1f3442f2c1e6a25f464dc0bff3a202e28b2 +size 298 diff --git a/S2OJ/1911/samples/magic1.ans b/S2OJ/1911/samples/magic1.ans new file mode 100644 index 00000000..5415508d --- /dev/null +++ b/S2OJ/1911/samples/magic1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:03a8e34278b929202f2150a9f0d3b43d048d530d1bb643d217c07ec668e0fb15 +size 9 diff --git a/S2OJ/1911/samples/magic1.in b/S2OJ/1911/samples/magic1.in new file mode 100644 index 00000000..ef9bcf82 --- /dev/null +++ b/S2OJ/1911/samples/magic1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:348cfc0c305b16a81767e86ea47184ce90a9cab12d39d7f98c4a1a9d1d501729 +size 40 diff --git a/S2OJ/1911/samples/magic2.ans b/S2OJ/1911/samples/magic2.ans new file mode 100644 index 00000000..3edcf6ca --- /dev/null +++ b/S2OJ/1911/samples/magic2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b473937a154bd045a97897598c994065977bae6e521296011356b15a91f0f4db +size 6 diff --git a/S2OJ/1911/samples/magic2.in b/S2OJ/1911/samples/magic2.in new file mode 100644 index 00000000..fd1f3dd4 --- /dev/null +++ b/S2OJ/1911/samples/magic2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:10163bf4fea987383f40f38fcea64c373c57a9781d853b47b56ed7483e1be295 +size 30 diff --git a/S2OJ/1911/samples/magic3.ans b/S2OJ/1911/samples/magic3.ans new file mode 100644 index 00000000..6ab08ab1 --- /dev/null +++ b/S2OJ/1911/samples/magic3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:424e5065abca68ad323dc204affd7d402a8421dc5f870981d2444f15c05d472d +size 15 diff --git a/S2OJ/1911/samples/magic3.in b/S2OJ/1911/samples/magic3.in new file mode 100644 index 00000000..502aa683 --- /dev/null +++ b/S2OJ/1911/samples/magic3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f03cdc4a2853807b45901b7fd6c653921cb7a1a2759762d8260c2f7b20cd3b41 +size 30 diff --git a/S2OJ/1911/samples/magic4.ans b/S2OJ/1911/samples/magic4.ans new file mode 100644 index 00000000..783829c1 --- /dev/null +++ b/S2OJ/1911/samples/magic4.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3595265c8c80c25a0cb6dd31b6a3a1c9064f386dc3b84df71e9bfb7782040f0c +size 1592813 diff --git a/S2OJ/1911/samples/magic4.in b/S2OJ/1911/samples/magic4.in new file mode 100644 index 00000000..0aef2496 --- /dev/null +++ b/S2OJ/1911/samples/magic4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7fcb8a126a985187c94fc9f7f12fb9f3eaa62285f32e542c29eaf24a3b0bb139 +size 8556246