P3379 【模板】最近公共祖先(LCA)
dfs1: 处理一个点的深度、父结点、子树大小,重儿子。
dfs2: 记录每个点的最顶部。
query: 哪个 top 的深度小跳哪个。
#include <bits/stdc++.h>
using namespace std;
const int N = 500010, M = 1000010;
struct edge {
int to, next;
} e[M];
int head[N], idx = 1;
void add(int u, int v) {
idx++, e[idx].to = v, e[idx].next = head[u], head[u] = idx;
}
int n, w[N];
int dep[N], fa[N], sz[N], son[N];
int top[N], rk[N], id[N], cnt;
void dfs1(int u, int f) {
sz[u] = 1, fa[u] = f, dep[u] = dep[f] + 1;
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (to == f) continue;
dfs1(to, u);
sz[u] += sz[to];
if (sz[son[u]] < sz[to]) son[u] = to;
}
}
void dfs2(int u, int t) {
top[u] = t; id[u] = ++cnt; rk[cnt] = u;
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (to == fa[u] || to == son[u]) continue;
dfs2(to, to);
}
}
int query(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
if (dep[x] < dep[y]) return x;
else return y;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q, root;
cin >> n >> q >> root;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
dfs1(root, root);
dfs2(root, root);
while (q--) {
int x, y;
cin >> x >> y;
cout << query(x, y) << '\n';
}
return 0;
}
P2590 [ZJOI2008] 树的统计
按照访问的顺序为点赋值。
如果是重链剖分,一条链上的所有点编号是连续的,所以我们可以考虑使用数据结构进行维护,此题我们可以使用线段树维护 \(sum\) 和 \(max\)。
#include <bits/stdc++.h>
using namespace std;
const int N = 30010, M = 60010;
struct edge {
int to, next;
} e[M];
int head[N], idx;
void add(int u, int v) {
idx++, e[idx].to = v, e[idx].next = head[u], head[u] = idx;
}
int n, w[N];
int sz[N], son[N], fa[N], dep[N];
int top[N], rk[N], id[N], cnt;
void dfs1(int u, int f) {
sz[u] = 1; dep[u] = dep[f] + 1; fa[u] = f;
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (to == f) continue;
dfs1(to, u);
sz[u] += sz[to];
if (sz[son[u]] < sz[to]) son[u] = to;
}
}
void dfs2(int u, int t) {
top[u] = t; id[u] = ++cnt, rk[cnt] = u;
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (to == fa[u] || to == son[u]) continue;
dfs2(to, to);
}
}
struct node {
int max, sum;
} tr[N << 2];
node pushup(node l, node r) {
return {max(l.max, r.max), l.sum + r.sum};
}
void init(int u, int l, int r) {
if (l == r) {
tr[u].max = tr[u].sum = w[rk[l]];
return;
}
int mid = l + r >> 1;
init(u << 1, l, mid);
init(u << 1 | 1, mid + 1, r);
tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
}
void modify(int u, int l, int r, int x, int v) {
if (l == r) {
tr[u].max = tr[u].sum = v;
return;
}
int mid = l + r >> 1;
if (x <= mid) modify(u << 1, l, mid, x, v);
else modify(u << 1 | 1, mid + 1, r, x, v);
tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
}
node query(int u, int l, int r, int pl, int pr) {
if (pl <= l && r <= pr) return tr[u];
int mid = l + r >> 1;
if (pr <= mid) return query(u << 1, l, mid, pl, pr);
else if (pl > mid) return query(u << 1 | 1, mid + 1, r, pl, pr);
else return pushup(query(u << 1, l, mid, pl, pr), query(u << 1 | 1, mid + 1, r, pl, pr));
}
node ask(int x, int y) {
node ans = {-0x3f3f3f3f, 0};
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans = pushup(ans, query(1, 1, n, id[top[x]], id[x]));
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
ans = pushup(ans, query(1, 1, n, id[x], id[y]));
return ans;
}
int main() {
freopen("a.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v), add(v, u);
}
dfs1(1, 1);
dfs2(1, 1);
for (int i = 1; i <= n; i++) cin >> w[i];
init(1, 1, n);
int q;
cin >> q;
string opt;
int x, y;
while (q--) {
cin >> opt >> x >> y;
if (opt == "CHANGE") modify(1, 1, n, id[x], y);
else if (opt == "QMAX") cout << ask(x, y).max << '\n';
else cout << ask(x, y).sum << '\n';
}
return 0;
}
标签:sz,idx,剖分,int,top,son,dep,树链
From: https://www.cnblogs.com/Yuan-Jiawei/p/18306523