lca-再再再进阶
最近公共祖先(LCA,lowest common ancestor)
定义
对于树上两个节点
如何求取
方法一:暴力
从 lca(x , y)。时间
方法二:倍增
第一部分:预处理
- 我们知道:
2 ^ k = 2 ^ {k - 1} + 2 ^ {k - 1} ,所以我们可以拆分,就得到了状态转移:
也就是从上一级转移过来(
那么初始值就是
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);
}
- 将
y 倍增移动到与x 深度相同。
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;
}//特判
-
将
x 和y 一起向上倍增移动,但不重合。 -
跳完后,判断两个是不是相等,因为有可能
y 是x 的祖先。 -
后
x 和y 不相等,就一直跳,因为如果相等了,然后,只要跳上去但是不一定是最近的,所以我们找到最后一个不相等的,再往上跳一步就是 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)] * 2
Some 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)] * 2
2.点的距离
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)] * 2
3.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)] * 2
The End
No,No,No,一道思维水题|