版本比较
LCA 笔记
正文
增加 212 行删除 37 行
112345678910111213149151617181920152122171823242125262728293031323334353637333839404150 行未改动92939495929394969798997 行未改动10710810911011011111211311411511611711811511912011712112211912312412112512612312412712812913013113213313413213513613713831 行未改动1701711721731721741751761771786 行未改动1851861871881871891901911921931941951961971971981991981992002018 行未改动2102112122132162142152162172182192202212222232242252252262272282292305 行未改动23623723823924024124224124324424524657 行未改动3043053063073073083093083093103117 行未改动31932032132232532332432532632732832933033133215 行未改动34834935035135235335435335535635735848 行未改动4074084094104104114124134144154164174174184194184194204215 行未改动427428429430433431432433436434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612感觉学斜二进制的效益不大,所以没学。介绍了 $5$ 种求最近公共祖先的方法。
看情况未来添加 tarjin 和 LCT 求 $\text{lca}$ 的笔记。
斜二倍增给出详细证明,有笔误欢迎提出。
---
### 倍增
倍增方法只在两年前另一个 oj 上打过板子,所以复习一下。
设 $f_{i,j}$ 表示 $i$ 往上跳 $2^j$ 次的结点。
先 $\texttt{dfs}$ 预处理深度 $dep_i$ 和父节点 $f_{i,0}$,然后考虑处理 $f$。先 $\texttt{dfs}$ 预处理深度 $\text{dep}_i$ 和父节点 $f_{i,0}$,然后考虑处理 $f$。
枚举 $j=1\to p$(最大的 $p$ 满足 $2^p\le n$),枚举 $i=1\to n$,令 $f_{i,j}=f_{f_{i,j-1},j-1}$。
证明显然,处理 $j$ 时 $j-1$ 都处理过了,$f_{f_{i,j-1},j-1}$ 就是跳两次 $2^{j-1}$ 等于 $2^{j-1}\times2=2^j$。
给我一种后缀数组的感觉说是。求解 $\text{lca}(x,y)$ 时,先令 $\text{dep}_x\ge \text{dep}_y$(不满足就 $\texttt{swap}$),然后让 $x$ 往上跳到与 $y$ 持平。然后一起往上跳,跳到深度最小的不想等的一对点,然后返回父结点即可。
求解 $\text{lca}(x,y)$ 时,先令 $dep_x\ge dep_y$(不满足就 $\texttt{swap}$),然后让 $x$ 往上跳到与 $y$ 持平。然后一起往上跳,跳到深度最小的不想等的一对点,然后返回父结点即可。
对于怎么跳是好做的。首先我们对一个结点 $x$ 的所有祖先结点的深度都要考虑到,这个是好做的,因为任何数字都可以拆分成不同的 $2$ 的幂相加,只需要枚举跳 $2^i$($i=22\to0$)即可。如果满足限制条件就跳,不满足就继续枚举。
$x$ 跳到与 $y$ 持平的限制条件就是 $dep_{f_{x,i}}\ge dep_y$,一起跳的限制条件是 $f_{x,i}\ne f_{y,i}$。$x$ 跳到与 $y$ 持平的限制条件就是 $\text{dep}_{f_{x,i}}\ge \text{dep}_y$,一起跳的限制条件是 $f_{x,i}\ne f_{y,i}$。
对于边界条件 $22$ 是因为 $22$ 足够大,缩小一点或者扩大一点都行,但是你需要考虑树的最大深度和 $\texttt{int}$ 的数据范围。
考虑时间复杂度:
- $\texttt{dfs}$ 预处理为 $O(n)$;- 预处理 $f$ 为 $O(n\log n)$;- 单次查询 $\text{lca}$ 为 $O(\log n)$。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 5e5+5;50 行未改动signed main(){ ios::sync_with_stdio(0); cin.tie(0);
// freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){7 行未改动 cin >> x >> y; cout << lca(x,y) << endl; } return 0;} }```::::
---
### 树剖
记 $top_i$ 为 $i$ 这条链里深度最小的结点,$id_i$ 为 $i$ 的 $\texttt{dfs}$ 序。记 $\text{top}_i$ 为 $i$ 这条链里深度最小的结点,$\text{id}_i$ 为 $i$ 的 $\texttt{dfs}$ 序。
剖分完毕后求解 $\text{lca}$,对于 $\text{lca}(x,y)$,如果 $top_i=top_j$,那么显然在一条链,直接返回 $dep_x$ 或 $id_x$ 更小者即可。剖分完毕后求解 $\text{lca}$,对于 $\text{lca}(x,y)$,如果 $\text{top}_i=\text{top}_j$,那么显然在一条链,直接返回 $\text{dep}_x$ 或 $\text{id}_x$ 更小者即可。
当不相等时,令 $dep_{top_x}$ 更大者往上跳,直到 $top_i=top_j$ 即可。当不相等时,令 $\text{dep}_{top_x}$ 更大者往上跳,直到 $\text{top}_i=\text{top}_j$ 即可。
当然,可以直接令 $id_x$ 更大者往上跳,因为很显然这里就不过多赘述了。跳的时候直接跳到 $f_{\text{top}_x}$ 即可。
跳的时候直接跳到 $f_{top_x}$ 即可,因为我们只关心 $top_x$ 的值。
考虑时间复杂度:
- 两次 $\texttt{dfs}$ 预处理都为 $O(n)$;- 单次查询 $\text{lca}$ 为 $O(\log n)$,但是很难到 $O(\log n)$,实际不卡都跑的飞快。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 5e5+5;31 行未改动}
inline void dfs2(int x,int t){ top[x] = t,id[x] = ++cnt;
if(!wson[x]) return;
dfs2(wson[x],t);
6 行未改动
inline int lca(int x,int y){ while(top[x] != top[y]){ if(dep[top[x]] < dep[top[y]]) swap(x,y); // if(id[x] < id[y]) swap(x,y); x = f[top[x]]; } return id[x]<id[y] ? x : y;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
// freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){8 行未改动 cin >> x >> y; cout << lca(x,y) << endl; } return 0;} }```::::
---
### 欧拉序
将 $\texttt{dfs}$ 搜索回溯过程中经过的所有结点都记录下来,记为 $b_i$($1\le i\le n'$)。
设 $i$ 号结点第一次出现的索引为 $a_i$,记 $x=\min(a_i,a_j),y=\max(a_i,a_j)$,那么 $\text{lca}(i,j)$ 为 $b$ 的区间 $[x,y]$ 中深度最小的结点。
即 $\text{lca}(i,j)$ 为 $b_k$($k\in[x,y]$)中 $dep$ 值最小的结点 $b_k$。即 $\text{lca}(i,j)$ 为 $b_k$($k\in[x,y]$)中 $\text{dep}$ 值最小的结点 $b_k$。
简易的证明是,设 $r$ 为 $\text{lca}(i,j)$。那么显然 $r$ 在 $[x,y]$ 这个区间内的,而且显然没有比 $r$ 深度更小的结点。因为深度更小的结点,要么从 $r$ 的祖先搜索过来,要么从 $r$ 回溯上去。这分别在这个区间的左边和右边。
此时问题转化为 $\texttt{RMQ}$ 问题了,显然是好做的。5 行未改动- $\texttt{dfs}$ 预处理为 $O(n)$;- 预处理 $\texttt{st}$ 表数组 $f$ 为 $O(n\log n)$;- 单次查询 $\text{lca}$ 为 $O(1)$。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 5e5+5;57 行未改动signed main(){ ios::sync_with_stdio(0); cin.tie(0);
// freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){7 行未改动 cin >> x >> y; cout << lca(x,y) << endl; } return 0;} }```::::
---
### $\texttt{dfs}$ 序
设时间戳 $\text{dfn}_i$ 表示结点 $i$ 在 $\texttt{dfs}$ 序中的索引。
15 行未改动- 单次查询 $\text{lca}$ 为 $O(1)$。
实际上比欧拉序少了一半常数。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 5e5+5;48 行未改动 int l = dfn[x],r = dfn[y]; if(l > r) swap(l,r); int s = lg[r-(++l)+1]; x = f[l][s],y = f[r-(1<<s)+1][s]; return (dep[x] < dep[y] ? x : y); return fa[(dep[x] < dep[y] ? x : y)];}
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
// freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){5 行未改动
while(m--){ cin >> x >> y; if(x == y) cout << x << endl; else cout << fa[lca(x,y)] << endl; else cout << lca(x,y) << endl; } return 0;} }```::::
---
### 斜二倍增
参考原文:<https://www.luogu.com.cn/article/x0sfn9kl>。
斜二倍增求解 $\text{lca}$ 由金怀恩在 2023 年提出,继承了倍增算法大部分功能,并且将预处理时间复杂度优化到了 $O(n)$。
斜二倍增的核心为数组 $\text{lb}$($\texttt{left\_bound}$,据本人口述是历史遗留产物),为了方便下文用 $g_x$ 代替。
$g_x$ 是 $x$ 的某一个祖先,$g_x$ 的深度由 $x$ 的深度唯一决定。
设 $f_{x}$ 表示 $x$ 的父亲结点,$p$ 为 $g_{f_x}$,$q$ 为 $g_p$。
跑 $\texttt{dfs}$ 预处理,若 $\text{dep}_{f_{x}}-\text{dep}_p\ne\text{dep}_p-\text{dep}_q$,则 $g_x\leftarrow f_x$;否则 $g_x\leftarrow q$。
求解 $\text{lca}$ 时,先将 $x$ 跳到与 $y$ 同一深度,然后同时跳。若 $g_x$ 满足条件则跳 $g_x$,否则跳 $f_x$。
具体来讲,先将 $x,y$ 相互交换使得 $\text{dep}_x>\text{dep}_y$,然后令 $x$ 往上跳至与 $y$ 深度相同。若 $\text{dep}_{g_x}\ge\text{dep}_y$ 则令 $x\leftarrow g_x$;否则 $x\leftarrow f_x$。当深度相同后,令 $x,y$ 一直跳到 $x=y$。若 $g_x\ne g_y$,则令 $x\leftarrow g_x,y\leftarrow g_y$;否则 $x\leftarrow f_x,y\leftarrow f_y$。最后返回 $x$ 或 $y$。
**详细证明:**
斜二倍增的正确性是毋庸置疑的,因为 $g_x$ 肯定是 $x$ 的某个祖先,不满足条件再跳 $f_x$,一满足立马跳出循环。显然我们需要考虑的时间复杂度。
证明斜二倍增的时间复杂度的充要条件是理解斜二进制。
以下我们用 $(x)_2'$ 表示 $x$ 的斜二进制数。
斜二进制数从低到高第 $i$ 位($i\in[1,+\infty)$)的位权为 $2^i-1$。一个斜二进制数最多只能有一个位为 $2$,其他位均不超过 $1$,且低于 $2$ 的位都强制为 $0$。
定义一个斜二进制数的**最低有效位**表示最低非 $0$ 的位。
接下来,我们用一种归纳构造的方式来生成每个自然数的斜二进制分解。首先强制令 $0$ 的斜二进制为 $0$,对于已经生成的 $(x)_2'$,我们利用它构造 $(x+1)_2'$。
1. $(x)_2'$ 的最低有效位为 $2$。
设这一位为第 $i$ 位,此时这一位的值为位权乘位值等于 $2\times(2^i-1)=2^{i+1}-2$。此时加上 $1$ 得到 $2^{i+1}-1$,不妨清楚第 $i$ 位并向更高位进一。
2. $(x)_2'$ 的最低有效位不为 $2$。
显然,直接给最低位进一即可。
下面给出一张表格帮助大家理解:
| $x$ | $(x)_2'$ | $(x)_2'$ 的最低有效位 | 进位解释 ||:-:|:-:|:-:|:-:|| $0$ | $0$ | \ | 直接进一 || $1$ | $1$ | $1$ | 最低有效位不为 $2$,直接进一 || $2$ | $2$ | $1$ | 最低有效位为 $2$,向高位进一 || $3$ | $10$ | $2$ | 最低有效位不为 $2$,直接进一 || $4$ | $11$ | $1$ | 最低有效位不为 $2$,直接进一 || $5$ | $12$ | $1$ | 最低有效位为 $2$,向高位进一 || $6$ | $20$ | $2$ | 最低有效位为 $2$,向高位进一 || $7$ | $100$ | $3$ | 最低有效位不为 $2$,直接进一 || $8$ | $101$ | $1$ | 最低有效位不为 $2$,直接进一 || $9$ | $102$ | $1$ | 最低有效位为 $2$,向高位进一 || $10$ | $110$ | $2$ | 最低有效位不为 $2$,直接进一 |
我们只关心斜二进制的性质,**不关心其是否唯一**(具体可以看后面的推式子)。
此时我们设 $\text{lowbit}(x)$ 表示 $(x)_2'$ 的**最低有效位的位权**。不妨钦定 $g_x$ 表示 $x$ 的 $\text{lowbit}(\text{dep}_x)$ 级祖先,不存在默认为 $0$。
因为是 $\text{lowbit}(\text{dep}_x)$ 级祖先,那么深度显然减少了 $\text{lowbit}(\text{dep}_x)$,即 $\text{dep}_x-\text{dep}_{g_x}=\text{lowbit}(\text{dep}_x)$,在斜二进制下看就是对最低有效位减了一。
这个时候考虑如何获得 $g_x$。
设 $p=g_{f_x}$、$q=g_p$,有且仅有两种情况:
1. $(\text{dep}_{f_x})_2'$ 的最低有效位不为 $2$。
移项得 $\text{dep}_x=\text{dep}_{g_x}+\text{lowbit}(\text{dep}_x)$。此时 $\text{dep}_{f_x}+1$ 就是在最低位加 $1$ 得到 $\text{dep}_x$,不难发现 $g_x=f_x$ 是满足等式的。
因为最低位为一所以 $\text{lowbit}(\text{dep}_x)=1$。$\text{dep}_{f_x}$ 在斜二进制下加一即为最低位加一。故此时 $g_x=f_x$。
2. $(\text{dep}_{f_x})_2'$ 的最低有效位为 $2$。
此时 $\text{dep}_x=\text{dep}_{f_x}+1$ 所以 $(\text{dep}_x)_2'$ 相对 $(\text{dep}_{f_x})_2'$ 进了一位。
移项得 $\text{dep}_x-\text{lowbit}(\text{dep}_x)=\text{dep}_{g_x}$。想要减少一个最低有效位显然等同与 $(\text{dep}_{f_x})_2'$ 直接将最低有效位抹去,相当于减少两次。
因为 $(\text{dep}_{f_x})_2'$ 最低有效位为 $2$,所以 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$。此时 $(\text{dep}_x)_2'$ 最低有效位减一相当于 $(\text{dep}_p)_2'$ 的最低有效位减一即 $q$。所以 $g_x=q$。
形象来讲,我们不妨假设 $(\text{dep}_{f_x})_2'=\dots200\dots$。显然 $(\text{dep}_{x})_2'=(\text{dep}_{f_x})_2'+1$ 直接相对 $(\text{dep}_{f_x})_2'$ 进了一位。此时进位后 $(\text{dep}_{f_x})_2'$ 的最低有效位开始想当于抹除了。这和 $(\text{dep}_{f_x})_2'-\text{lowbit}(\text{dep}_{f_x})\times 2$ 是等价的。因为 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$,我们直接令 $g_x=g_p=q$ 即可。
具体来讲,$(\text{dep}_{f_x})_2'$ 的最低有效位为 $2$ 时,$\text{dep}_x-\text{lowbit}(\text{dep}_x)=\text{dep}_{f_x}-\text{lowbit}(\text{dep}_{f_x})\times 2=\text{dep}_p-\text{lowbit}_p=\text{dep}_q$。
需要注意的是边界 $f_0=g_0=\text{dep}_0=\text{lowbit}(\text{dep}_0)=0$。
现在考虑一下怎么判断 $(\text{dep}_{f_x})_2'$ 的最低有效位是否为 $2$。通过情况 $2$ 的类比,不难发现 $\text{lowbit}(\text{dep}_{f_x})=\text{lowbit}(\text{dep}_p)$ 时满足最低有效位为 $2$。简单代换得 $\text{dep}_{f_x}-\text{dep}_p=\text{dep}_p-\text{dep}_q$。
不难发现跳使深度变小,本质上是斜二进制的退位。退位的过程中我们不断使斜二进制从左到右向 $\text{lca}$ 匹配。
抽象来说,对于前面左边已经匹配好了若干位,匹配好的后一位比 $\text{lca}$ 大 $0\sim 2$,需要先减 $\text{lowbit}$ 到这一位后面全部肃清,然后再减 $\text{lowbit}$ 到比 $\text{lca}$ 只大 $1$,然后减一使最低有效位降 $1$,此时这一位匹配成功,再考虑后面。
显然肃清只需要进行一次,之后开始交互跳 $\text{lowbit}$ 匹配和跳 $1$ 退位至完毕。而肃清和总体交互跳的匹配都是 $O(\log n)$ 的。
恕笔者数学没学好,无法给出正式的归纳,只能用自己的语言将这个表述清楚。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>#define lint long long#define endl '\n'using namespace std;const int N = 5e5+5;
struct edge{ int ne,to;}e[N<<1];int cntE,pre[N];
int n,m,s;
int lb[N];int fa[N];int dep[N];
inline void add(int x,int y){ e[++cntE] = (edge){pre[x],y}; pre[x] = cntE;}
inline void dfs(int x,int father){ dep[x] = dep[father] + 1; fa[x] = father;
int p = lb[father],q = lb[p]; if(dep[father] - dep[p] != dep[p] - dep[q]) lb[x] = father; else lb[x] = q;
for(int i = pre[x]; i; i=e[i].ne){ int y = e[i].to; if(y != father) dfs(y,x); }}
inline int lca(int x,int y){ if(dep[x] < dep[y]) swap(x,y); while(dep[x] > dep[y]){ if(dep[lb[x]] >= dep[y]) x = lb[x]; else x = fa[x]; } while(x != y){ if(lb[x] != lb[y]) x = lb[x],y = lb[y]; else x = fa[x],y = fa[y]; } return x;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){ cin >> x >> y; add(x,y),add(y,x); }
dfs(s,0);
while(m--){ cin >> x >> y; if(x == y) cout << x << endl; else cout << lca(x,y) << endl; } return 0;}```::::