跳到正文

个人记录

最近公共祖先

Slclcc约 11 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

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


定义:对于树上两个结点 xx 和 yy,xx 和 yy 的祖先结点中,共同的且深度最大的节点为 xx 和 yy 的 LCA。


求取方案:

  • 暴力:从 xx 出发向上走到根节点,途中标记走过的每一个节点,yy 再向上走到根节点,途中遇到的第一个被标记过的点即为 lca⁡(x,y)\operatorname{lca}(x,y)。时间复杂度:O(n)O(n)

  • 倍增:

    • Step Ⅰ:预处理

      1. 预处理 depidep_i 表示点 ii 的深度。 depnxt=depu+1dep_{nxt} = dep_u + 1。
      2. 预处理 dpi,jdp_{i,j} 表示点 ii 向上移动 2i2^i 步到达的点编号。 dpi,j=dpdpi,j−1,j−1dp_{i,j} = dp_{dp_{i,j-1},j-1}。
    • Step Ⅱ:处理询问 lca⁡(x,y)\operatorname{lca}(x,y)。

      1. 约定深度较大的点,比如 yy。

        C++
        if (dep[x] > dep[y])
            swap(x, y);
      2. 将 yy 倍增移动到与 xx 深度相同。

        C++
        for (int i = 20; i >= 0; i--) {
          if (dep[dp[y][i]] >= dep[x]) {
            y = dp[y][i];
          }
        }
        if (x == y)
          return y;
      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];

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

C++
#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 点的距离

点 xx 到点 yy 的最短距离即点 xx 到 lca⁡(x,y)\operatorname{lca}(x,y) 的距离 + yy 到 lca⁡(x,y)\operatorname{lca}(x,y)。

即 disx−dislca(x,y)+disy−dislca(x,y)=disx+disy−2×dislca(x,y)dis_x - dis_{lca(x,y)} + dis_y - dis_{lca(x,y)} = dis_x + dis_y - 2 \times dis_{lca(x,y)}。

本题的 disi=depidis_i = dep_i。

C++
#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

本题有边权,我们单独维护一个 disidis_i 即可。

C++
#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 祖孙询问

lca⁡(x,y)==x\operatorname{lca}(x,y) == x

  • 是,输出 1。

  • 不是,判断 lca⁡(x,y)==y\operatorname{lca}(x,y) == y。

    • 是,输出 2.

    • 不是,输出 0。

C++
#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';
	}
}
原文