【模板】倍增LCA

C++版本:

倍增LCA

void dfs(int v, int p, int d) {//v当前节点,p父节点,d深度 
	parent[0][v] = p;
	depth[v] = d;
	for (int i = head[v]; i; i = eg[i].next) {
		int to = eg[i].v;
		if (to != p)dfs(to, v, d + 1);
	}
}
void init(int V) {
	dfs(root, -1, 0);
	maxlogv = floor(log2(V));
	for (int k = 0; k + 1 <= maxlogv; k++) {//预处理倍增 
		for (int v = 1; v <= V; v++) {
			if (parent[k][v] < 0)parent[k + 1][v] = -1;
			else parent[k + 1][v] = parent[k][parent[k][v]];
		}
	}
}
int lca(int u, int v) {
	if (depth[u] > depth[v])swap(u, v);
	for (int k = 0; k <= maxlogv; k++) {
		if ((depth[v] - depth[u]) >> k & 1) {//用倍增优化向上找的次数 
			v = parent[k][v];
		}
	}
	if (u == v)return u;
	for (int k = maxlogv; k >= 0; k--) {
		if (parent[k][u] != parent[k][v]) {
			u = parent[k][u];
			v = parent[k][v];
		}
	}
	return parent[0][u];
}
上一篇:P3242 [HNOI2015]接水果 树上莫队+分块


下一篇:st表、RMQ和LCA