最近公共祖先(LCA,lowest common ancestor)
定义
对于树上两个节点和,和的祖先节点中,公共且深度最大的节点为;
如何求取
方法一:暴力
从 点出发向上到根节点,并沿途标记所有点。 再次向上尝试走到根节点, 遇到的第一个已经标记的点即为 lca(x , y)。时间
方法二:倍增
第一部分:预处理
- 表示点 的深度。
- 表示点 移动 后到的点数。
- 我们知道:,所以我们可以拆分,就得到了状态转移:
也就是从上一级转移过来( 是当前节点, 是枚举的幂次)。
那么初始值就是 。
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);
}
}
}第二部分:处理询问
- 约定深度较大的点,确定研究对象。
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;
}//特判-
将 和 一起向上倍增移动,但不重合。
-
跳完后,判断两个是不是相等,因为有可能 是 的祖先。
-
后 和 不相等,就一直跳,因为如果相等了,然后,只要跳上去但是不一定是最近的,所以我们找到最后一个不相等的,再往上跳一步就是 LCA 了。
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
#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)] * 2Some disgusting problem
1.P3379 【模板】最近公共祖先(LCA)
【模板】
#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)] * 22.点的距离
vjudge T^T
点到点的最短距离==点到点的最短距离 + 点到点的最短距离 =
=
code
#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)] * 23.Dis
有边权了
有边权了
有边权了
怎么办???
加一个数组记录路径长度
#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)] * 2The End
No,No,No,一道思维水题| v
4.祖孙询问
简单,如果说明是的祖先, 如果说明是的祖先
#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