LCA 问题的3种不同解法
0 序
为了强化笔者对LCA的理解,故作此文。
1 DFS序求LCA
1.1 算法介绍
考虑树上的两个节点
令
当
这意味着我们只需要求出
如何保证这个方法的正确性?在
当
很显然,当
若
综上所述,若
1.2算法实现
可以通过 ST 表来查询
以下是模板题目 P3379 的代码:
// P3379 【模板】最近公共祖先(LCA)
// https://www.luogu.com.cn/problem/P3379
// 2023-11-09 21:40
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 500000 + 5;
int n, m, s;
vector<int> G[maxn];
int dfn[maxn], mi[20][maxn];
int get(int x, int y) {
return dfn[x] < dfn[y] ? x : y;
}
void dfs(int x, int f) {
dfn[x] = ++dfn[0]; mi[0][dfn[x]] = f;
for(auto to : G[x]) if(to != f) dfs(to, x);
}
int lca(int x, int y) {
if(x == y) return x;
if((x = dfn[x]) > (y = dfn[y])) swap(x, y);
int d = log2(y - x++);
return get(mi[d][x], mi[d][y - (1 << d) + 1]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("AKIOI.in", "r", stdin);
// freopen("AKIOI.out", "w", stdout);
cin >> n >> m >> s;
for(int i = 1, u, v; i < n; i++) {
cin >> u >> v;
G[u].push_back(v); G[v].push_back(u);
}
dfs(s, 0);
for(int i = 1; i <= log2(n); i++) for(int j = 1; j + (1 << i) - 1 <= n; j++)
mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << i - 1)]);
while(m--) {
int x, y;
cin >> x >> y;
cout << lca(x, y) << "\n";
}
return 0;
}
/*
编程语言
C++14 (GCC 9)
代码长度
1.25KB
用时
1.64s
内存
77.61MB
GGapa
所属题目
P3379 【模板】最近公共祖先(LCA)
评测状态
Accepted
评测分数
100
提交时间
2023-11-09 22:02:41
*/
2 树链剖分 LCA
2.1 算法介绍
对于一个父节点的所有儿子来说,在它们之间子树大小最大的便是重儿子(
考虑树上的两个节点
- 若
top_u = top_v 这说明u,v 在一条重链上,u,v 一定存在祖先关系,此时u,v 中深度较小的节点便是他们的 LCA。 - 否则,需要把深度较大的节点跳到这条重链的顶端。并重复执行操作1。
为什么不能同时将
时间复杂度
2.2 算法实现
需要进行两次递归,第一次递归求出每个非叶子节点的重儿子;第二次循环求出
以下是模板题目 P3379 的代码:
// P3379 【模板】最近公共祖先(LCA)
// https://www.luogu.com.cn/problem/P3379
// 2023-11-09 20:41
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 500000 + 5;
int n, m, s;
vector<int> G[maxn];
int si[maxn], top[maxn], hson[maxn], fa[maxn], dep[maxn];
void dfs1(int x, int f) {
si[x] = 1; dep[x] = dep[f] + 1; fa[x] = f;
for(auto to : G[x]) {
if(to == f) continue;
dfs1(to, x);
si[x] += si[to];
if(si[to] > si[hson[x]]) hson[x] = to;
}
}
void dfs2(int x, int tp) {
top[x] = tp;
if(!hson[x]) return ;
dfs2(hson[x], tp);
for(auto to : G[x]) {
if(to == fa[x] || to == hson[x]) continue;
dfs2(to, to);
}
}
int lca(int x, int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return (dep[x] < dep[y] ? x : y);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("AKIOI.in", "r", stdin);
// freopen("AKIOI.out", "w", stdout);
cin >> n >> m >> s;
for(int i = 1, u, v; i < n; i++) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(s, 0);
dfs2(s, s);
while(m--) {
int x, y;
cin >> x >> y;
cout << lca(x, y) << "\n";
}
return 0;
}
/*
编程语言
C++14 (GCC 9)
代码长度
1.37KB
用时
1.86s
内存
55.85MB
GGapa
所属题目
P3379 【模板】最近公共祖先(LCA)
评测状态
Accepted
评测分数
100
提交时间
2023-11-09 21:04:30
*/
3 倍增 LCA
3.1 算法介绍
相对于暴力算法,倍增 LCA 减少了向上跳的次数,如何减少?每次从大到小尝试跳
为了方便跳,需要记录每一个节点
考虑树上的两个节点
接着,两个节点同时往上跳,直到
时间复杂度
3.2 算法实现
以下是模板题目 P3379 的代码:
// P3379 【模板】最近公共祖先(LCA)
// https://www.luogu.com.cn/problem/P3379
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 500000 + 5;
const int maxk = 21; // log2(maxn) + 1
int fa[maxk][maxn];
int depth[maxn];
vector<int> vec[maxn];
void dfs(int x, int f)
{
fa[0][x] = f;
depth[x] = depth[f] + 1;
for (int i = 1; i < maxk; i++)
{
fa[i][x] = fa[i - 1][fa[i - 1][x]];
}
for (auto to : vec[x])
{
if (to == f)
continue;
dfs(to, x);
}
}
int LCA(int x, int y)
{
if (depth[x] < depth[y])
swap(x, y);
for (int i = maxk - 1; i >= 0; i--)
{
if (depth[fa[i][x]] >= depth[y])
x = fa[i][x];
}
if (x == y)
return x;
for (int i = maxk - 1; i >= 0; i--)
{
if (fa[i][x] != fa[i][y])
{
x = fa[i][x];
y = fa[i][y];
}
}
return fa[0][x];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
for (int i = 1, x, y; i < n; i++)
{
scanf("%d%d", &x, &y);
vec[x].push_back(y);
vec[y].push_back(x);
}
dfs(s, 0);
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
cout << LCA(a, b) << endl;
}
return 0;
}
/*
编程语言
C++14 (GCC 9) O2
代码长度
1.43KB
用时
5.53s
内存
90.44MB
GGapa
所属题目
P3379 【模板】最近公共祖先(LCA)
评测状态
Accepted
评测分数
100
提交时间
2023-07-31 15:39:45
*/
倍增的常数很大,所以说花了接近 5s。
4 各种 LCA 算法的对比
-
预处理时间复杂度均相同,常数倍增最大。
-
单次查询复杂度:DFS 序
< 树链剖分= 倍增 -
单次查询常数大小:树链剖分
< DFS 序< 倍增 -
个人认为 DFS 序代码难易程度较低。