跳到正文

算法·理论

lca-再再再进阶

Luna_From_Starry约 19 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 19 分钟

版本

  1. v1当前

最近公共祖先(LCA,lowest common ancestor)

定义

对于树上两个节点xx和yy,xx和yy的祖先节点中,公共且深度最大的节点为LCA(x,y)\mathsf{LCA(x, y)} ;

如何求取

方法一:暴力

从 xx 点出发向上到根节点,并沿途标记所有点。yy 再次向上尝试走到根节点,yy 遇到的第一个已经标记的点即为 lca(x , y)。时间 O(n)O(n)

方法二:倍增

第一部分:预处理

  1. depidep_i 表示点 ii 的深度。
  2. dpi,jdp_{i,j} 表示点 ii 移动 2j2 ^ j 后到的点数。
  • 我们知道:2k=2k−1+2k−12 ^ k = 2 ^ {k - 1} + 2 ^ {k - 1},所以我们可以拆分,就得到了状态转移:
fi,j=ffi,j−1,j−1f_{i , j} = f_{f_{i , j - 1} , j - 1}

也就是从上一级转移过来(ii 是当前节点,jj 是枚举的幂次)。

那么初始值就是 fi,0=faif_{i , 0} = fa_i。

C++
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it != fa){
			init_lca(it, st);
		}
	}
}

第二部分:处理询问

  1. 约定深度较大的点,确定研究对象。
C++
if(dep[x] > dep[y]){
		swap(x, y);
	}
  1. 将 yy 倍增移动到与 xx 深度相同。
C++
for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
if(x == y){
		return x;
	}//特判
  1. 将 xx 和 yy 一起向上倍增移动,但不重合。

  2. 跳完后,判断两个是不是相等,因为有可能 yy 是 xx 的祖先。

  3. 后 xx 和 yy 不相等,就一直跳,因为如果相等了,然后,只要跳上去但是不一定是最近的,所以我们找到最后一个不相等的,再往上跳一步就是 LCA 了。

C++
for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];

The Answer Code

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
Code by cmy;
AK N = 500005;
AK M = 1000005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, m, s, dep[N],  dp[N][21];
vector<int> g[N];
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it != fa){
			init_lca(it, st);
		}
	}
}
int get_lca(int x, int y){
	if(dep[x] > dep[y]){
		swap(x, y);
	}
	for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
	if(x == y){
		return x;
	}
	for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1; i < n; i++){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	init_lca(s,0);
	while(m--){
		int a,b;
		cin >> a >> b;
		cout << get_lca(a,b) << "\n";
	}
	return 0;
}
//dep[a] + dep[b] - dep[get_lca(a,b)] * 2

Some disgusting problem

1.P3379 【模板】最近公共祖先(LCA)

【模板】

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
Code by cmy;
AK N = 500005;
AK M = 1000005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, m, s, dep[N],  dp[N][21];
vector<int> g[N];
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it != fa){
			init_lca(it, st);
		}
	}
}
int get_lca(int x, int y){
	if(dep[x] > dep[y]){
		swap(x, y);
	}
	for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
	if(x == y){
		return x;
	}
	for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1; i < n; i++){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	init_lca(s,0);
	while(m--){
		int a,b;
		cin >> a >> b;
		cout << get_lca(a,b) << "\n";
	}
	return 0;
}
//dep[a] + dep[b] - dep[get_lca(a,b)] * 2

2.点的距离

vjudge T^T

点xx到点yy的最短距离==点xx到点LCA(x,y)LCA(x, y)的最短距离 + 点yy到点LCA(x,y)LCA(x, y)的最短距离 = dis[x]−dis[lca(x,y)]+dis[y]−dis[lca(x,y)]dis[x] - dis[lca(x, y)] + dis[y] - dis[lca(x, y)]

= dis[x]+dis[y]−2∗dis[lca(x,y)]dis[x] + dis[y] - 2 * dis[lca(x, y)]

code

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
Code by cmy;
AK N = 500005;
AK M = 1000005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, m, s, dep[N],  dp[N][21];
vector<int> g[N];
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it != fa){
			init_lca(it, st);
		}
	}
}
int get_lca(int x, int y){
	if(dep[x] > dep[y]){
		swap(x, y);
	}
	for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
	if(x == y){
		return x;
	}
	for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1; i < n; i++){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	init_lca(1,0);
	cin >> m;
	while(m--){
		int a,b;
		cin >> a >> b;
		cout << dep[a] + dep[b] - dep[get_lca(a,b)] * 2 << "\n";
	}
	return 0;
}
//dep[a] + dep[b] - dep[get_lca(a,b)] * 2

3.Dis

有边权了

有边权了

有边权了

怎么办???

加一个disdis数组记录路径长度

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
Code by cmy;
AK N = 500005;
AK M = 1000005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, m, s, dep[N],  dp[N][21], dis[N];
struct node{
	int v, w;
};
vector<node> g[N];
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it.v != fa){
			dis[it.v] = dis[st] + it.w;
			init_lca(it.v, st);
		}
	}
}
int get_lca(int x, int y){
	if(dep[x] > dep[y]){
		swap(x, y);
	}
	for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
	if(x == y){
		return x;
	}
	for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i < n; i++){
		int x,y,w;
		cin >> x >> y >> w;
		g[x].push_back({y, w});
		g[y].push_back({x, w});
	}
	init_lca(1,0);
//	cin >> m;
	while(m--){
		int a,b;
		cin >> a >> b;
		cout << dis[a] + dis[b] - dis[get_lca(a,b)] * 2 << "\n";
	}
	return 0;
}
//dep[a] + dep[b] - dep[get_lca(a,b)] * 2

The End





















No,No,No,一道思维水题|                                    \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ v

4.祖孙询问

简单,如果lca(x,y)==xlca(x, y) == x说明xx是yy的祖先, 如果lca(x,y)==ylca(x, y) == y说明yy是xx的祖先

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
Code by cmy;
AK N = 500005;
AK M = 1000005;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
int n, m, s, dep[N],  dp[N][21];
vector<int> g[N];
void init_lca(int st, int fa){
	dp[st][0] = fa;
	dep[st] = dep[fa] + 1;
	for(int i = 1; (1 << i) <= dep[st]; i++){
		dp[st][i] = dp[dp[st][i - 1]][i - 1];
	}
	for(auto it : g[st]){
		if(it != fa){
			init_lca(it, st);
		}
	}
}
int get_lca(int x, int y){
	if(dep[x] > dep[y]){
		swap(x, y);
	}
	for(int i = log2(dep[y]); i >= 0; i--){
		if(dep[dp[y][i]] >= dep[x]){
			y = dp[y][i];
		}
	}
	if(x == y){
		return x;
	}
	for(int i = 20; i >= 0; i--){
		if(dp[x][i] != dp[y][i]){
			x = dp[x][i];
			y = dp[y][i];
		}
	}
	return dp[x][0];
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		int x,y;
		cin >> x >> y;
		if(y == -1){
			s = x;
			continue;
		}
		g[x].push_back(y);
		g[y].push_back(x);
	}
	cin >> m;
	init_lca(s,0);
	while(m--){
		int a,b;
		cin >> a >> b;
		int ans = get_lca(a,b);
		cout << (ans == a ? "1" : ans == b ? "2" : "0") << "\n";
	}
	return 0;
}
//dep[a] + dep[b] - dep[get_lca(a,b)] * 2

The End

原文