跳到正文

算法·理论

LCA

scguo约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年3月23日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 13 分钟

版本

  1. v1当前

最近公共最先(LCA,Lowest Common Ancestor)

定义

如果有两个点 xx 和 yy,那么点 zz 是 LCA⁡(x,y)\operatorname{LCA}(x , y) 当且仅当 zz 是 xx 的祖先且 zz 是 yy 的祖先且满足前两个条件的情况下,zz 的深度最大。

求解

  • 暴力(查询 O(n)O(n))

首先从 xx 出发一直往根走,把沿途的点都标记,然后 yy 再往根走,如果遇到了被标记的点,则最先遇到的就是 LCA⁡(x,y)\operatorname{LCA}(x , y)。

  • 倍增(预处理 O(nlog⁡n)O(n \log n),查询 O(log⁡n)O(\log n))

  • 预处理

我们定义 fi,jf_{i , j} 为当前点是 ii,然后往根节点方向跳 2j2 ^ j 步的节点。

还要预处理出每个节点的深度。

然后就是对倍增进行更新。

我们知道:2k=2k−1+2k−12 ^ k = 2 ^ {k - 1} + 2 ^ {k - 1},所以我们可以拆分,就得到了状态转移:

fi,j=ffi,j−1,j−1f_{i , j} = f_{f_{i , j - 1} , j - 1}

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

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

  • 查询

我们定义 depx≥depydep_x \ge dep_y,其中 depidep_i 是第 ii 个点的深度。

那么,我们可以从大到小枚举跳的幂次,如果跳了这么多后,depxdep_x 还是 ≥depy\ge dep_y,那么说明可以跳上去。

跳完后,判断两个是不是相等,因为有可能 yy 是 xx 的祖先。

然后,只要跳上去后 xx 和 yy 不相等,就一直跳,因为如果相等了,但是不一定是最近的,所以我们找到最后一个不相等的,再往上跳一步就是 LCA 了。

C++
void Search(int u, int fa) // 预处理
{
	dep[u] = dep[fa] + 1, f[u][0] = fa; // 处理深度,及f的初始化
	for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; // 转移
	for (int v : g[u]) if (v != fa) Search(v, u); // 下一个
	return;
}

int query(int x, int y) // 查询
{
    if (dep[x] < dep[y]) swap(x, y); // 满足上述规则
    for (int i = 20; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i]; // 跳到和y深度一样
    if (x == y) return x; // 特判
    for (int i = 20; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; // 再一起跳
    return f[x][0]; // 返回答案
}
  • 树链剖分(同倍增时间)

不会。

Luogu - P3379

解法

模板题,不用讲。

Code

C++
#include <iostream> 
#include <cstdio> 
#include <algorithm> 
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstring>
#include <bitset>
#define int long long

using namespace std;

const int N = 5e5 + 5, INF = 1e18;
int n, q, s, dep[N], f[N][25];
vector<int> g[N];

void Search(int u, int fa)
{
	dep[u] = dep[fa] + 1, f[u][0] = fa;
	for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
	for (int v : g[u]) if (v != fa) Search(v, u);
	return;
}

int query(int x, int y)
{
    if (dep[x] < dep[y]) swap(x, y);
    for (int i = 20; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
    if (x == y) return x;
    for (int i = 20; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
    return f[x][0];
}

signed main()
{
	cin.tie(0)->sync_with_stdio(false);
	cin >> n >> q >> s;
	for (int i = 1, x, y; i < n; ++i)
	{
		cin >> x >> y;
		g[x].emplace_back(y), g[y].emplace_back(x);
	}
	Search(s, 0);
    for (int x, y; q--; cout << '\n')
    {
        cin >> x >> y;
        cout << query(x, y);
    }
	return 0;
}

LibreOJ - 10130

我们可以知道,树上唯一路径是从 xx 到 LCA⁡(x,y)\operatorname{LCA}(x , y) 再到 yy,所以距离公式就是:

disx+disy−2×disLCA⁡(x,y)dis_x + dis_y - 2 \times dis_{\operatorname{LCA}(x , y)}

其中 disidis_i 表示从根到 ii 的距离。

按照上述公式解就行了。

Code

C++
#include <iostream> 
#include <cstdio> 
#include <algorithm> 
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstring>
#include <bitset>
#define int long long

using namespace std;

const int N = 5e5 + 5, INF = 1e18;
int n, q, dep[N], f[N][25];
vector<int> g[N];

void Search(int u, int fa)
{
	dep[u] = dep[fa] + 1, f[u][0] = fa;
	for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
	for (int v : g[u]) if (v != fa) Search(v, u);
	return;
}

int query(int x, int y)
{
    if (dep[x] < dep[y]) swap(x, y);
    for (int i = 20; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
    if (x == y) return x;
    for (int i = 20; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
    return f[x][0];
}

signed main()
{
	cin.tie(0)->sync_with_stdio(false);
	cin >> n;
	for (int i = 1, x, y; i < n; ++i)
	{
		cin >> x >> y;
		g[x].emplace_back(y), g[y].emplace_back(x);
	}
	Search(1, 0);
    cin >> q;
    for (int x, y; q--; cout << '\n')
    {
        cin >> x >> y;
        cout << dep[x] - 1 + dep[y] - 1 - 2 * (dep[query(x, y)] - 1); // 计算公式
    }
	return 0;
}

LibreOJ - 10134

解法

改一下边权即可。其它不变。

Code

C++
#include <iostream> 
#include <cstdio> 
#include <algorithm> 
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstring>
#include <bitset>
#define int long long
#define fi first
#define se second

using namespace std;

const int N = 5e5 + 5, INF = 1e18;
int n, q, dep[N], f[N][25], dis[N];
vector<pair<int, int>> g[N];

void Search(int u, int fa)
{
	dep[u] = dep[fa] + 1, f[u][0] = fa;
	for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
	for (auto v : g[u]) if (v.fi != fa) dis[v.fi] = dis[u] + v.se, Search(v.fi, u);
	return;
}

int query(int x, int y)
{
    if (dep[x] < dep[y]) swap(x, y);
    for (int i = 20; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
    if (x == y) return x;
    for (int i = 20; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
    return f[x][0];
}

signed main()
{
	cin.tie(0)->sync_with_stdio(false);
	cin >> n >> q;
	for (int i = 1, x, y, z; i < n; ++i)
	{
		cin >> x >> y >> z;
		g[x].emplace_back(y, z), g[y].emplace_back(x, z); // 只有这里不一样
	}
	Search(1, 0);
    for (int x, y; q--; cout << '\n')
    {
        cin >> x >> y;
        cout << dis[x] + dis[y] - 2 * (dis[query(x, y)]);
    }
	return 0;
}

LibreOJ - 10135

解法

我们可以再查询当中,如果是通过特判返回的,那么,如果一开始交换了,说明 xx 是 yy 的祖先,否则 yy 是 xx 的祖先。

否则就返回 00,因为都不是。

Code

C++
#include <iostream> 
#include <cstdio> 
#include <algorithm> 
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <cstring>
#include <bitset>
#define int long long
#define fi first
#define se second

using namespace std;

const int N = 5e5 + 5, INF = 1e18;
int n, q, dep[N], f[N][25], dis[N];
vector<pair<int, int>> g[N];

void Search(int u, int fa)
{
	dep[u] = dep[fa] + 1, f[u][0] = fa;
	for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
	for (auto v : g[u]) if (v.fi != fa) dis[v.fi] = dis[u] + v.se, Search(v.fi, u);
	return;
}

int query(int x, int y)
{
    bool fl = 0;
    if (dep[x] < dep[y]) swap(x, y), fl = 1; // 标记是否交换
    for (int i = 20; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
    if (x == y)
    {
        if (fl) return 1;
        return 2;
    }
    for (int i = 20; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
    return 0;
}

signed main()
{
	cin.tie(0)->sync_with_stdio(false);
	cin >> n;
    int root = 0;
	for (int i = 1, x, y; i <= n; ++i)
	{
		cin >> x >> y;
        if (y == -1) // 说明是根
        {
            root = x;
            continue;
        }
		g[x].emplace_back(y, 1), g[y].emplace_back(x, 1);
	}
	Search(root, 0);
    cin >> q;
    for (int x, y; q--; cout << '\n')
    {
        cin >> x >> y;
        cout << query(x, y);
    }
	return 0;
}

The End.

原文