跳到正文

休闲·娱乐

LCA模板总结 & 测试

stripe_python约 9 分钟
原文

这是 2025年11月12日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

前言

复习 CSP 时打的一些 LCA 板子。顺便给个速度。

测试结果

方法总用时 / 秒
倍增2.082.08
DFS 序 + ST 表1.291.29
树剖1.191.19
LCT2.492.49

代码

倍增

C++
namespace LCA {
	int dep[N], f[__lg(N) + 1][N];
	
	void dfs(int u, int fa) {
		dep[u] = dep[fa] + 1, f[0][u] = fa;
		for (int i = 1; i <= __lg(dep[u]); i++) {
			f[i][u] = f[i - 1][f[i - 1][u]];
		}
		for (int j = head[u]; j != 0; j = edge[j].next) {
			if (edge[j].to != fa) dfs(edge[j].to, u);
		}
	}
	void init(int rt) {dfs(rt, 0);}
	
	int lca(int x, int y) {
		if (dep[x] < dep[y]) swap(x, y);
		while (dep[x] > dep[y]) x = f[__lg(dep[x] - dep[y])][x];
		if (x == y) return x;
		for (int k = __lg(dep[x]); k >= 0; k--) {
			if (f[k][x] != f[k][y]) x = f[k][x], y = f[k][y];
		}
		return f[0][x];
	}
}

DFS 序 + ST 表

C++
template <class T>
class SphareTable {
private:
	using func_t = function<T(const T&, const T&)>;
	int n; T st[__lg(N) + 1][N]; func_t func;
	
	void init(const T* a, bool get_init) {
		if (!get_init) func = [](const T& x, const T& y) -> T {return x > y ? x : y;};
		memset(st, 0, sizeof(st));
		memcpy(st[0] + 1, a + 1, sizeof(T) * n);
		for (int i = 1; i <= __lg(n); i++) {
			for (int j = 1; j + (1 << i) - 1 <= n; j++) {
				st[i][j] = func(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
			}
		}
	}
public:
	SphareTable() {}
	SphareTable(const T* a, int _n) : n(_n) {init(a, 0);}
	SphareTable(const T* a, int _n, const func_t& f) : n(_n), func(f) {init(a, 1);}
	
	T query(int l, int r) const {
		int k = __lg(r - l + 1);
		return func(st[k][l], st[k][r - (1 << k) + 1]);
	}
};

namespace LCA {
	int dn, dfn[N], dfa[N];
	SphareTable<int> st;
	void dfs(int u, int fa) {
		dfn[u] = ++dn, dfa[dn] = fa;
		for (int j = head[u]; j != 0; j = edge[j].next) {
			if (edge[j].to != fa) dfs(edge[j].to, u);
		}
	}
	
	void init(int rt) {
		dn = 0, dfs(rt, 0);
		st = SphareTable<int>(dfa, n, [&](const int& u, const int& v) -> int {
			return dfn[u] < dfn[v] ? u : v;
		});
	}
	
	int lca(int x, int y) {
		if (x == y) return x;
		return st.query(min(dfn[u], dfn[v]) + 1, max(dfn[u], dfn[v]));
	}
}

树剖

C++
namespace LCA {
	int dep[N], sz[N], fa[N], son[N], top[N];
	
	void dfs1(int u) {
		sz[u] = 1;
		for (int j = head[u]; j != 0; j = edge[j].next) {
			int v = edge[j].to;
			if (v == fa[u]) continue;
			fa[v] = u, dep[v] = dep[u] + 1, dfs1(v), sz[u] += sz[v];
			if (sz[v] > sz[son[u]]) son[u] = v;
		}
	}
	void dfs2(int u, int t) {
		top[u] = t;
		if (son[u]) dfs2(son[u], t);
		for (int j = head[u]; j != 0; j = edge[j].next) {
			int v = edge[j].to;
			if (v != fa[u] && v != son[u]) dfs2(v, v);
		}
	}
	void init(int rt) {dfs1(rt), dfs2(rt, rt);}
	
	int lca(int u, int v) {
		while (top[u] != top[v]) {
			if (dep[top[u]] < dep[top[v]]) swap(u, v);
			u = fa[top[u]];
		}
		return dep[u] < dep[v] ? u : v;
	}
}

LCT

C++
class LinkCutTree {
#define ls (ch[0][rt])
#define rs (ch[1][rt])
public:
	int ch[2][N], fa[N], rev[N];
	inline bool get(int rt) {return ch[1][fa[rt]] == rt;}
	inline void pushup(int rt) {}
	inline void pushdown(int rt) {
		if (rev[rt]) {
			if (ls) rev[ls] ^= 1, swap(ch[0][ls], ch[1][ls]);
			if (rs) rev[rs] ^= 1, swap(ch[0][rs], ch[1][rs]);
			rev[rt] ^= 1;
		}
	}
	inline void rotate(int x) {
		int y = fa[x], z = fa[y], chk = get(x);
		fa[x] = z;
		if (!is_root(y)) ch[get(y)][z] = x;
		ch[chk][y] = ch[chk ^ 1][x], fa[ch[chk ^ 1][x]] = y;
		ch[chk ^ 1][x] = y, fa[y] = x;
		pushup(y), pushup(x);
	}
	inline void splay(int x) {
		update(x);
		for (int f = fa[x]; f = fa[x], !is_root(x); rotate(x)) {
			if (!is_root(f)) rotate(get(f) == get(x) ? f : x);
		}
	}
	
	bool is_root(int x) {
		return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
	}
	void access(int x) {
		for (int f = 0; x; f = x, x = fa[x]) {
			splay(x), ch[1][x] = f, pushup(x);
		}
	}
	void update(int x) {
		if (!is_root(x)) update(fa[x]);
		pushdown(x);
	}
	void make_root(int x) {
		access(x), splay(x), swap(ch[0][x], ch[1][x]), rev[x] ^= 1;
	}
	void link(int x, int y) {
		make_root(x), splay(x), fa[x] = y;
	}
	void cut(int x, int y) {
		make_root(x), access(y), splay(y), ch[0][x] = fa[x] = 0;
	}
	int lca(int x, int y) {
		access(x); int res = y;
		for (int i = 0; y; res = y, i = y, y = fa[i]) {
			splay(y), ch[1][y] = i;
		}
		return res;
	}
#undef ls
#undef rs
} lct;