术语

Binary jumping 更常被称为 binary lifting,中文通常称为“倍增”。

倍增

重点例题 – 继续学习之前,请先尽力独立解决这道题!

讲解

倍增会对所有相关的 kk,计算每个节点的第 2k2^k 级祖先并存入表中。

利用这张表,就能高效回答任意节点的第 kk 级祖先查询,因为任何 kk 都能根据其 二进制表示拆成若干个 22 的幂之和。

例如,不必直接求某节点的第 1313 级祖先,而可以依次向上跳 88、44、11 级。 这样求第 kk 级祖先只需对数复杂度。

如果仍不清楚,下面的动画展示了跳跃过程:

实际计算倍增表时,先求每个节点的第 20=12^0=1 级祖先,也就是直接父节点。然后利用 “第 22 级祖先是第 11 级祖先的第 11 级祖先”计算第 21=22^1=2 级祖先。 用同样逻辑继续计算 22,232^2,2^3 等。当 2n2^n 大于树的大小时停止,因为此时一定 已经到达根。

实现

时间复杂度: O((N+Q)log⁡N)\mathcal{O}((N+Q)\log N)

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

习题

StatusSourceProblem NameDifficultyTags
CSESEasy
Show TagsBinary Jumping
CSESMedium
Show TagsFunctional Graph
CSESMedium
Show TagsBinary Jumping, Binary Search, Two Pointers
POIMedium
Show TagsBinary Jumping, Sliding Window
CFMedium
Show TagsBinary Jumping
Baltic OIMedium
Baltic OIMedium
Show TagsBinary Jumping, Matrix
PlatinumHard
Show TagsBinary Jumping
Baltic OIVery Hard
Show TagsBinary Jumping

最近公共祖先

重点例题 – 继续学习之前,请先尽力独立解决这道题!

View Internal Solution
Resources
CPH

简要说明与题解

SansPapyrus683

另一种实现

解法一

为求 lca(a,b)\textrm{lca}(a,b),先把 a,ba,b 中较深的节点提升到与另一个节点同深, 再从大到小同时向上跳。最后,任一节点的父节点就是二者的 LCA。

实现

时间复杂度: O((N+Q)log⁡N)\mathcal{O}((N+Q)\log N)

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

解法二

也可以借助树的 Euler 序计算 LCA。

令 start\texttt{start} 和 end\texttt{end} 分别记录树中节点进入与离开的时间,填表方式 与 Euler 序模块完全相同。

在填写 start\texttt{start} 和 end\texttt{end} 的同时,还能用上一种解法完全相同的方式 计算倍增表。因为 DFS 保证节点的所有祖先都先于该节点处理,所以访问某个节点时, 它所有祖先的表都已经填好。

若没有节点深度,可以利用如下事实计算 LCA:当且仅当 start[a]≤start[b]\texttt{start}[a]\le\texttt{start}[b] 且 end[b]≤end[a]\texttt{end}[b]\le\texttt{end}[a] 时,节点 aa 是节点 bb 的祖先。

在 LCA 函数中,先检查一个节点是否已经是另一个的祖先;若是,直接返回该祖先。 否则,用与前面倍增算法基本相同的方法向上提升其中一个节点,直到其父节点成为 另一个节点的祖先。答案就是被提升节点的父节点。

实现

时间复杂度: O((N+Q)log⁡N)\mathcal{O}((N+Q)\log N)

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Tree {
private:

解法三

也可以用 Tarjan 离线 LCA 算法求两个节点的 LCA。

利用 DFS 遍历,可以在形成子树的过程中,用类似并查集的结构计算公共祖先,从而 预先求出所有查询的答案。

实现

#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 1;
bool vis[MAX];
int lca[MAX], fa[MAX];
vector<array<int, 2>> adj[MAX], qry[MAX];
int find(int u) { return (fa[u] == u) ? u : fa[u] = find(fa[u]); }
void tarjan(int node) {
vis[node] = true;
Resources
cp-algo

重点例题 – 继续学习之前,请先尽力独立解决这道题!

讲解

已知所有节点的深度,节点 a,ba,b 间的距离为 depth[a]+depth[b]−2⋅depth[lca(a,b)]\texttt{depth}[a] + \texttt{depth}[b] - 2 \cdot \texttt{depth}[\textrm{lca}(a, b)].

直观地说,从 aa 到 bb 可以先走到树根,再走到 bb,距离为 depth[a]+depth[b]\texttt{depth}[a]+\texttt{depth}[b]。但这样会把 LCA 上方的路径走_两遍_, 所以要减去 LCA 深度的两倍,得到最终公式。

实现

时间复杂度: O((N+Q)log⁡N)\mathcal{O}((N+Q)\log N)

#include <cmath>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
Code Snippet: LCA Tree (Click to expand)

习题

USACO

StatusSourceProblem NameDifficultyTags
PlatinumEasy
Show TagsLCA
PlatinumMedium
Show TagsLCA
Old GoldMedium
Show TagsBinary Jumping, Euler Tour, Small to Large
PlatinumHard
Show TagsLCA
PlatinumHard
Show TagsDiameter
PlatinumHard
Show TagsLCA
PlatinumVery Hard
Show TagsLCA

综合

StatusSourceProblem NameDifficultyTags
CFEasy
Show TagsBinary Jumping
CFMedium
Show TagsLCA
CFMedium
Show TagsLCA
CFMedium
Show TagsLCA
CSAMedium
Show TagsLCA
CFMedium
Show TagsLCA
Back to SchoolMedium
Show TagsLCA
CFMedium
Show TagsLCA, Shortest Path
Google KickstartHard
Show TagsBinary Jumping, DFS, LCA
CFHard
Show TagsBinary Jumping, LCA
TLXHard
Show TagsLCA
TLXHard
Show TagsLCA

Module Progress: