lca-再再再进阶

· · 算法·理论

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

定义

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

如何求取

方法一:暴力

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

方法二:倍增

第一部分:预处理

f_{i , j} = f_{f_{i , j - 1} , j - 1}

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

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

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. 约定深度较大的点,确定研究对象。
if(dep[x] > dep[y]){
        swap(x, y);
    }
  1. 将 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;
    }//特判
  1. 将 x 和 y 一起向上倍增移动,但不重合。

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

  3. 后 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

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

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

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

有边权了

有边权了

有边权了

怎么办???

加一个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,一道思维水题|

### 4.[祖孙询问 ](https://vjudge.net/problem/LibreOJ-10135) 简单,如果$lca(x, y) == x$说明$x$是$y$的祖先, 如果$lca(x, y) == y$说明$y$是$x$的祖先 ```cpp #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