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

#1911. M 弟娃(magic)

https://sjzezoj.com/submission/69072
This commit is contained in:
Baoshuo Ren 2023-02-11 14:07:35 +08:00
parent 4921b92f03
commit f590290667
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
38 changed files with 286 additions and 0 deletions

175
S2OJ/1911/1911.cpp Normal file
View File

@ -0,0 +1,175 @@
#include <iostream>
#include <vector>
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<int> 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;
}

BIN
S2OJ/1911/data/ex_magic1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/ex_magic4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/magic9.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic1.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic2.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic3.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic4.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1911/samples/magic4.in (Stored with Git LFS) Normal file

Binary file not shown.