版本比较
LCA 笔记
正文
增加 86 行删除 16 行
1234567891080 行未改动919293949395969798997 行未改动10710810911010911111211311411554 行未改动17017117217317217417517617717814 行未改动1931941951961951971981992002018 行未改动21021121221321221421521621721884 行未改动3033043053063053073083093103117 行未改动31932032132232132332432532632785 行未改动4134144154164154174184194204218 行未改动43043143243343243443543643743843944044144044244344444544613 行未改动46046146246346246446546446646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853919 行未改动55956056156249356356456556656749856856957057157216 行未改动5895905915925235935945955965978 行未改动606607608609540610611612介绍了 $5$ 种求最近公共祖先的方法。
看情况未来添加 tarjin 和 LCT 求 $\text{lca}$ 的笔记。
斜二倍增给出详细证明,有笔误欢迎提出。
---
### 倍增
80 行未改动
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
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;} }```::::
---54 行未改动}
inline void dfs2(int x,int t){ top[x] = t,id[x] = ++cnt;
if(!wson[x]) return;
dfs2(wson[x],t);
14 行未改动
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
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;} }```::::
---84 行未改动
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
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;} }```::::
---85 行未改动
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){8 行未改动 if(x == y) cout << x << endl; else cout << lca(x,y) << endl; } return 0;} }```::::
---
### 斜二倍增
原文:<https://www.luogu.com.cn/article/x0sfn9kl>。参考原文:<https://www.luogu.com.cn/article/x0sfn9kl>。
斜二倍增求解 $\text{lca}$ 由金怀恩在 2023 年提出,继承了倍增算法大部分功能,并且将预处理时间复杂度优化到了 $O(n)$。
斜二倍增的核心为数组 $\text{lb}$($\texttt{left\_bound}$,据本人口述是历史遗留产物),为了方便下文用 $g_x$ 代替。13 行未改动斜二倍增的正确性是毋庸置疑的,因为 $g_x$ 肯定是 $x$ 的某个祖先,不满足条件再跳 $f_x$,一满足立马跳出循环。显然我们需要考虑的时间复杂度。
证明斜二倍增的时间复杂度的充要条件是理解斜二进制。
以下我们用 $(x)_2'$ 表示 $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 long19 行未改动
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); }16 行未改动
signed main(){ ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> s;
int x,y; for(int i = 1; i < n; i++){8 行未改动 if(x == y) cout << x << endl; else cout << lca(x,y) << endl; } return 0;} }```::::