最近公共祖先(lowest common ancestor,LCA)。
定义:对于树上两个结点 和 , 和 的祖先结点中,共同的且深度最大的节点为 和 的 LCA。
求取方案:
-
暴力:从 出发向上走到根节点,途中标记走过的每一个节点, 再向上走到根节点,途中遇到的第一个被标记过的点即为 。时间复杂度:
-
倍增:
-
Step Ⅰ:预处理
- 预处理 表示点 的深度。 。
- 预处理 表示点 向上移动 步到达的点编号。 。
-
Step Ⅱ:处理询问 。
-
约定深度较大的点,比如 。
C++ if (dep[x] > dep[y]) swap(x, y); -
将 倍增移动到与 深度相同。
C++ for (int i = 20; i >= 0; i--) { if (dep[dp[y][i]] >= dep[x]) { y = dp[y][i]; } } if (x == y) return y; -
将 和 一起向上倍增移动,但不能重合(因为一旦重合即 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];
-
-
P3379 【模板】最近公共祖先(LCA)
#include <bits/stdc++.h>
using namespace std;
int n, m, s, dep[500005], dp[500005][25];
vector<int> nbr[500005];
void PreLCA(int u, int fa) {
dep[u] = dep[fa] + 1;
dp[u][0] = fa;
for (int i = 1; (1 << i) <= dep[u]; i++)
dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto it : nbr[u]) {
if (it != fa)
PreLCA(it, u);
}
}
int LCA(int x, int y) {
if (dep[x] > dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[dp[y][i]] >= dep[x]) {
y = dp[y][i];
}
}
if (x == y)
return y;
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];
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
nbr[u].push_back(v);
nbr[v].push_back(u);
}
PreLCA(s, 0);
while (m--) {
int x, y;
cin >> x >> y;
cout << LCA(x, y) << '\n';
}
}LibreOJ-10130 点的距离
点 到点 的最短距离即点 到 的距离 + 到 。
即 。
本题的 。
#include <bits/stdc++.h>
using namespace std;
int n, m, s, dep[500005], dp[500005][25];
vector<int> nbr[500005];
void PreLCA(int u, int fa) {
dep[u] = dep[fa] + 1;
dp[u][0] = fa;
for (int i = 1; (1 << i) <= dep[u]; i++)
dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto it : nbr[u]) {
if (it != fa)
PreLCA(it, u);
}
}
int LCA(int x, int y) {
if (dep[x] > dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[dp[y][i]] >= dep[x]) {
y = dp[y][i];
}
}
if (x == y)
return y;
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];
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
nbr[u].push_back(v);
}
PreLCA(1, 0);
cin >> m;
while (m--) {
int x, y;
cin >> x >> y;
cout << dep[x] - dep[LCA(x, y)] + dep[y] - dep[LCA(x, y)] << '\n';
}
}LibreOJ - 10134 Dis
本题有边权,我们单独维护一个 即可。
#include <bits/stdc++.h>
using namespace std;
int n, m, s, dep[500005], dp[500005][25], dis[500005];
struct node {
int v, w;
};
vector<node> nbr[500005];
void PreLCA(int u, int fa) {
dep[u] = dep[fa] + 1;
dp[u][0] = fa;
for (int i = 1; (1 << i) <= dep[u]; i++)
dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto it : nbr[u]) {
if (it.v != fa)
dis[it.v] = dis[u] + it.w, PreLCA(it.v, u);
}
}
int LCA(int x, int y) {
if (dep[x] > dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[dp[y][i]] >= dep[x]) {
y = dp[y][i];
}
}
if (x == y)
return y;
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];
}
int main() {
cin >> n;
cin >> m;
for (int i = 1; i <= n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
nbr[u].push_back({v, w});
nbr[v].push_back({u, w});
}
PreLCA(1, 0);
while (m--) {
int x, y;
cin >> x >> y;
cout << dis[x] + dis[y] - 2 * dis[LCA(x, y)] << '\n';
}
}LibreOJ - 10135 祖孙询问
-
是,输出 1。
-
不是,判断 。
-
是,输出 2.
-
不是,输出 0。
-
#include <bits/stdc++.h>
using namespace std;
int n, m, s, dep[500005], dp[500005][25], dis[500005];
vector<int> nbr[500005];
void PreLCA(int u, int fa) {
dep[u] = dep[fa] + 1;
dp[u][0] = fa;
for (int i = 1; (1 << i) <= dep[u]; i++)
dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto it : nbr[u]) {
if (it != fa)
PreLCA(it, u);
}
}
int LCA(int x, int y) {
if (dep[x] > dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[dp[y][i]] >= dep[x]) {
y = dp[y][i];
}
}
if (x == y)
return y;
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];
}
int main() {
cin >> n;
int root;
for (int i = 1; i <= n; i++) {
int u, v;
cin >> u >> v;
if (v == -1) {
root = u;
continue;
}
nbr[u].push_back(v);
nbr[v].push_back(u);
}
PreLCA(root, 0);
cin >> m;
while (m--) {
int x, y;
cin >> x >> y;
cout << (LCA(x, y) == x ? 1 : LCA(x, y) == y ? 2 : 0) << '\n';
}
}