LCA 笔记

· · 算法·理论

介绍了 5 种求最近公共祖先的方法。

看情况未来添加 tarjin 和 LCT 求 \text{lca} 的笔记。

斜二倍增给出详细证明,有笔误欢迎提出。

倍增

倍增方法只在两年前另一个 oj 上打过板子,所以复习一下。

设 f_{i,j} 表示 i 往上跳 2^j 次的结点。

先 \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 持平。然后一起往上跳,跳到深度最小的不想等的一对点,然后返回父结点即可。

对于怎么跳是好做的。首先我们对一个结点 x 的所有祖先结点的深度都要考虑到,这个是好做的,因为任何数字都可以拆分成不同的 2 的幂相加,只需要枚举跳 2^i(i=22\to0)即可。如果满足限制条件就跳,不满足就继续枚举。

对于边界条件 $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 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 dep[N]; int f[N][30]; inline void add(int x,int y){ e[++cntE] = (edge){pre[x],y}; pre[x] = cntE; } inline void dfs(int x,int fa){ f[x][0] = fa; dep[x] = dep[fa] + 1; for(int i = pre[x]; i; i=e[i].ne){ int y = e[i].to; if(y != fa) dfs(y,x); } } inline void init(){ for(int j = 1; (1<<j) <= n; j++){ for(int i = 1; i <= n; i++) f[i][j] = f[f[i][j-1]][j-1]; } } inline int lca(int x,int y){ if(dep[x] < dep[y]) swap(x,y); for(int i = 22; i >= 0; i--){ if(dep[f[x][i]] >= dep[y]) x = f[x][i]; } if(x == y) return x; for(int i = 22; i >= 0; i--){ if(f[x][i] != f[y][i]){ x = f[x][i]; y = f[y][i]; } } return f[x][0]; } 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),init(); while(m--){ cin >> x >> y; cout << lca(x,y) << endl; } return 0; } ``` :::: --- ### 树剖 记 $\text{top}_i$ 为 $i$ 这条链里深度最小的结点,$\text{id}_i$ 为 $i$ 的 $\texttt{dfs}$ 序。 剖分完毕后求解 $\text{lca}$,对于 $\text{lca}(x,y)$,如果 $\text{top}_i=\text{top}_j$,那么显然在一条链,直接返回 $\text{dep}_x$ 或 $\text{id}_x$ 更小者即可。 当不相等时,令 $\text{dep}_{top_x}$ 更大者往上跳,直到 $\text{top}_i=\text{top}_j$ 即可。 跳的时候直接跳到 $f_{\text{top}_x}$ 即可。 考虑时间复杂度: - 两次 $\texttt{dfs}$ 预处理都为 $O(n)$; - 单次查询 $\text{lca}$ 为 $O(\log n)$,但是很难到 $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 cnt; int id[N],top[N]; int f[N],dep[N]; int w[N],wson[N]; int n,m,s; inline void add(int x,int y){ e[++cntE] = (edge){pre[x],y}; pre[x] = cntE; } inline void dfs1(int x,int fa){ f[x] = fa,w[x] = 1; dep[x] = dep[fa] + 1; for(int i = pre[x]; i; i=e[i].ne){ int y = e[i].to; if(y != fa){ dfs1(y,x); w[x] += w[y]; if(w[wson[x]] < w[y]) wson[x] = y; } } } inline void dfs2(int x,int t){ top[x] = t,id[x] = ++cnt; if(!wson[x]) return; dfs2(wson[x],t); for(int i = pre[x]; i; i=e[i].ne){ int v = e[i].to; if(v != f[x] && v != wson[x]) dfs2(v,v); } } inline int lca(int x,int y){ while(top[x] != top[y]){ if(dep[top[x]] < dep[top[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); cin >> n >> m >> s; int x,y; for(int i = 1; i < n; i++){ cin >> x >> y; add(x,y),add(y,x); } dfs1(s,0); dfs2(s,s); while(m--){ 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]$)中 $\text{dep}$ 值最小的结点 $b_k$。 简易的证明是,设 $r$ 为 $\text{lca}(i,j)$。那么显然 $r$ 在 $[x,y]$ 这个区间内的,而且显然没有比 $r$ 深度更小的结点。因为深度更小的结点,要么从 $r$ 的祖先搜索过来,要么从 $r$ 回溯上去。这分别在这个区间的左边和右边。 此时问题转化为 $\texttt{RMQ}$ 问题了,显然是好做的。 不带修改,用 $\texttt{st}$ 表预处理即可。 考虑时间复杂度: - $\texttt{dfs}$ 预处理为 $O(n)$; - 预处理 $\texttt{st}$ 表数组 $f$ 为 $O(n\log n)$; - 单次查询 $\text{lca}$ 为 $O(1)$。 ::::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 dep[N]; int f[N<<1][30]; int cnt,a[N]; int b[N<<1]; int lg[N<<1]; inline void add(int x,int y){ e[++cntE] = (edge){pre[x],y}; pre[x] = cntE; } inline void dfs(int x,int fa){ dep[x] = dep[fa] + 1; b[++cnt] = x; a[x] = cnt; for(int i = pre[x]; i; i=e[i].ne){ int y = e[i].to; if(y != fa){ dfs(y,x); b[++cnt] = x; } } } inline void init(){ lg[0] = -1; for(int i = 1; i <= cnt; i++) lg[i] = lg[i>>1]+1; for(int i = 1; i <= cnt; i++) f[i][0] = b[i]; for(int j = 1; j <= lg[cnt]; j++){ for(int i = 1; i+(1<<j)-1 <= cnt; i++){ int x = f[i][j-1],y = f[i+(1<<(j-1))][j-1]; f[i][j] = (dep[x] < dep[y] ? x : y); } } } inline int lca(int x,int y){ int l = a[x],r = a[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); } 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),init(); while(m--){ cin >> x >> y; cout << lca(x,y) << endl; } return 0; } ``` :::: --- ### $\texttt{dfs}$ 序 设时间戳 $\text{dfn}_i$ 表示结点 $i$ 在 $\texttt{dfs}$ 序中的索引。 对于求解 $\text{lca}(x,y)$,不妨先钦定 $\text{dfn}_x<\text{dfn}_y$。 设 $l=\text{dfn}_x,r=\text{dfn}_y$,设结点 $d$ 为 $\text{lca}(l,r)$。 1. 不妨先钦定 $x$ 不为 $y$ 的祖先,此时 $\texttt{dfs}$ 序的区间 $[x,y]$ 里有 $x$ 的子树和 $d$ 子树包含 $y$ 的子结点 $y'$ 的子树。此时显然 $y'$ 的深度最小,那么 $y'$ 的父结点即为答案。在区间 $[x,y]$ 中查找深度最小的结点即可; 2. 当 $x$ 为 $y$ 的祖先结点时,显然这个区间为一条链,且 $x$ 的深度最小。那么答案应该为 $(x,y]$ 中深度最小的结点的父亲。对于情况 $(1)$,显然 $y'$ 肯定在 $(x,y]$ 这个区间,所以同样适用这个答案。 故答案为 $(x,y]$ 中深度最小的结点的父亲结点。当然 $x$ 和 $y$ 相同时要特判。 不带修改,用 $\texttt{st}$ 表预处理即可。 考虑时间复杂度: - $\texttt{dfs}$ 预处理为 $O(n)$; - 预处理 $\texttt{st}$ 表数组 $f$ 为 $O(n\log n)$; - 单次查询 $\text{lca}$ 为 $O(1)$。 实际上比欧拉序少了一半常数。 ::::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 dep[N]; int fa[N]; int f[N<<1][30]; int cnt,a[N]; int dfn[N],lg[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; a[++cnt] = x; dfn[x] = cnt; for(int i = pre[x]; i; i=e[i].ne){ int y = e[i].to; if(y != father) dfs(y,x); } } inline void init(){ lg[0] = -1; for(int i = 1; i <= n; i++) lg[i] = lg[i>>1]+1; for(int i = 1; i <= n; i++) f[i][0] = a[i]; for(int j = 1; j <= lg[n]; j++){ for(int i = 1; i+(1<<j)-1 <= n; i++){ int x = f[i][j-1],y = f[i+(1<<(j-1))][j-1]; f[i][j] = (dep[x] < dep[y] ? x : y); } } } inline int lca(int x,int y){ 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 fa[(dep[x] < dep[y] ? x : y)]; } 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),init(); while(m--){ cin >> x >> y; if(x == y) cout << x << 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; } ``` ::::