AI题解
树上松鼠跳跃:分块与跳跃树
题目概述
给定一棵有
每次询问给出
数据范围:
本文算法的最坏复杂度为:
其中
1. 路径外的节点不能使答案更优
树上
因此,任何可行的跳跃路线都可以投影成路径上的可行路线,跳数不增加。只需考虑这条路径。
但答案不一定是:
例如,一条路径由三条长度为
在有序路径上,每一步跳到朝目标方向最远的可达原节点是最优的。
证明可以使用交换论证:设贪心落点为
反复应用这个交换,就能得到一条最优的贪心路线。
2. 用 LCA 将询问拆成两段
设:
从
这些跳跃不会越过
假设两端分别跳了
记残余路径长度为:
剩余部分需要:
最后一种情况经由
因此:
这里
问题转化为:快速求出一个端点向其祖先贪心跳跃的次数与停止位置。
3. 一次向上跳跃做到 O(1)
以节点
按照原树的 DFS 前序遍历,维护数组
- 访问原节点
u 时,令\mathrm{path}[d[u]]=u 。 - 如果
u 的父边长度为2 ,再令\mathrm{path}[d[u]-1]=u 。
第二条赋值处理落在长度为
于是,访问
由于边长不超过
这里必须使用真正的 DFS 前序,而不能使用任意拓扑序或 BFS 顺序。进入另一个分支时,只会覆盖公共祖先以下的位置,所以当前祖先路径上的数组内容始终正确,无须回滚。
4. 固定 c ,建立跳跃树
固定跳跃距离
因为它指向原树的严格祖先,所以这些边构成一棵以
记跳跃树深度为
按照
最多有
大跳跃距离:c>B
直接贪心模拟。
当剩余距离至少为
又因为:
且每次询问的路径总长度不超过
次跳跃。
将所有大
不能对每一个大
6. 总复杂度
LCA 使用离线 Tarjan 算法,并查集按大小合并并进行路径压缩,时间为:
合计:
代入
当
每次只保存一个
7. 实现注意事项
- 所有遍历使用显式栈,避免长链造成递归栈溢出。
- 原树 DFS 前序的逆序,是兄弟子树顺序相反的合法 DFS 后序。可以据此实现非递归 Tarjan。
- Tarjan 处理节点时,先标记完成并回答另一端已完成的查询,再与父节点合并。
- 按大小合并后,并查集代表未必是语义上的祖先,需要独立的祖先标签。
- 每个小
c 只清空O(n) 的节点数组,不对全部Q 个询问反复初始化。 - 原树与跳跃树的路径数组都只读取当前祖先前缀,无须回滚。
- 当
u=v 时,答案为0 。
8. C++17 完整代码
// C++17. Time: O(n*sqrt(Q) + (n+Q)*alpha(n)); space: O(n+Q).
// Readable version: fixed sqrt(Q) threshold, iterative traversals.
#include <bits/stdc++.h>
using namespace std;
struct Query {
int u, v, c;
int lca = 0, answer = 0, remainder = 0;
};
struct DSU {
vector<int> fa, sz, label;
explicit DSU(int n) : fa(n + 1), sz(n + 1, 1), label(n + 1) {
iota(fa.begin(), fa.end(), 0);
iota(label.begin(), label.end(), 0);
}
int find(int x) {
while (x != fa[x]) {
fa[x] = fa[fa[x]];
x = fa[x];
}
return x;
}
void merge(int u, int p) {
int a = find(u), b = find(p);
if (sz[a] < sz[b]) swap(a, b);
fa[b] = a;
sz[a] += sz[b];
label[a] = p;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
vector<vector<pair<int, int>>> g(n + 1);
for (int i = 1, u, v, w; i < n; ++i) {
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
// A genuine DFS preorder; no recursive call stack.
vector<int> parent(n + 1), dist(n + 1), pw(n + 1), order, st;
order.reserve(n);
st.reserve(n);
st.push_back(1);
int maxDepth = 0;
while (!st.empty()) {
int u = st.back(); st.pop_back();
order.push_back(u);
for (auto [v, w] : g[u]) if (v != parent[u]) {
parent[v] = u;
pw[v] = w;
dist[v] = dist[u] + w;
maxDepth = max(maxDepth, dist[v]);
st.push_back(v);
}
}
int Q;
cin >> Q;
vector<Query> qs(Q);
// Event 2*i is endpoint u, event 2*i+1 is endpoint v.
vector<int> head(n + 1, -1), nextEvent(2 * Q);
auto attach = [&](int u, int e) {
nextEvent[e] = head[u];
head[u] = e;
};
for (int i = 0; i < Q; ++i) {
auto &q = qs[i];
cin >> q.u >> q.v >> q.c;
if (q.u == q.v) q.lca = q.u;
else { attach(q.u, 2 * i); attach(q.v, 2 * i + 1); }
}
// Offline Tarjan LCA: reversed DFS preorder is a valid postorder.
{
DSU dsu(n);
vector<char> black(n + 1);
for (int k = n - 1; k >= 0; --k) {
int u = order[k];
black[u] = true;
for (int e = head[u]; e != -1; e = nextEvent[e]) {
auto &q = qs[e / 2];
int other = (e & 1) ? q.u : q.v;
if (black[other]) q.lca = dsu.label[dsu.find(other)];
}
if (parent[u]) dsu.merge(u, parent[u]);
}
}
int B = 1;
while (1LL * B * B < Q) ++B;
vector<vector<int>> small(B + 1);
vector<int> pending;
fill(head.begin(), head.end(), -1);
for (int i = 0; i < Q; ++i) {
auto &q = qs[i];
int D = dist[q.u] + dist[q.v] - 2 * dist[q.lca];
if (D <= q.c) { q.answer = (D != 0); continue; }
pending.push_back(i);
if (q.c <= B) small[q.c].push_back(i);
else { attach(q.u, 2 * i); attach(q.v, 2 * i + 1); }
}
// At the current DFS vertex, path[t] rounds depth t downward
// to the next original vertex (toward the current vertex).
vector<int> path(maxDepth + 1, 1);
auto enter = [&](int u) {
path[dist[u]] = u;
if (pw[u] == 2) path[dist[u] - 1] = u;
};
// All large-c endpoints share this one original-tree traversal.
for (int u : order) {
enter(u);
for (int e = head[u]; e != -1; e = nextEvent[e]) {
auto &q = qs[e / 2];
int x = u, base = dist[q.lca];
while (dist[x] - base >= q.c) {
x = path[dist[x] - q.c];
++q.answer;
}
q.remainder += dist[x] - base;
}
}
vector<int> to(n + 1), J(n + 1), child(n + 1), sibling(n + 1);
vector<int> jumpPath(n + 1);
for (int c = 2; c <= B; ++c) if (!small[c].empty()) {
fill(head.begin(), head.end(), -1);
fill(child.begin(), child.end(), -1);
to[1] = 1;
J[1] = 0;
for (int u : order) {
enter(u);
if (u == 1) continue;
int p = path[max(0, dist[u] - c)];
to[u] = p;
J[u] = J[p] + 1;
sibling[u] = child[p];
child[p] = u;
}
for (int i : small[c]) {
attach(qs[i].u, 2 * i);
attach(qs[i].v, 2 * i + 1);
}
// DFS of the jump tree: each endpoint is answered in O(1).
st.clear();
st.push_back(1);
while (!st.empty()) {
int u = st.back(); st.pop_back();
jumpPath[J[u]] = u;
for (int e = head[u]; e != -1; e = nextEvent[e]) {
auto &q = qs[e / 2];
int l = q.lca;
int target = min(J[u], J[l] + 1);
int x = jumpPath[target];
if (dist[x] - dist[l] >= c) x = to[x];
q.answer += J[u] - J[x];
q.remainder += dist[x] - dist[l];
}
for (int v = child[u]; v != -1; v = sibling[v]) st.push_back(v);
}
}
for (int i : pending) {
auto &q = qs[i];
q.answer += q.remainder > q.c ? 2 : (q.remainder > 0 ? 1 : 0);
}
for (auto &q : qs) cout << q.answer << '\n';
}
9. 验证与版本说明
本题解代码已通过独立跳跃图 BFS 校验:共 204 棵树、110,346 个查询,包括题目样例、单节点、
BFS 允许松鼠跳到任意原树节点,不预先假设最优路线局限于
本文代码采用固定分界,便于说明算法和证明。先前实测的工程优化版另加入自适应分组、相邻