求树上任意两个点的距离lca

前言:一开始看到这个题目的时候,感觉就和lca有关,但是没有想到具体的公式

d = d e p [ x ] + d e p [ y ] − 2 ∗ d e p [ l c a ( x , y ) ] d = dep[x] + dep[y] - 2*dep[lca(x,y)] d=dep[x]+dep[y]−2∗dep[lca(x,y)]

并且我们这个题目还是一个进阶版本的题,我们还会存在电缆,我们还有两种选择的可能,这也是要考虑在内的


题目地址

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = (int)5e5;
int n;
int e[N * 2], ne[N * 2], h[N], idx = 0;
int px, py;
int dep[N], fa[N][20];

inline int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c>'9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
	return f * x;
}

void add(int a, int b) {
	e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}

void dfs(int u, int father) {
	dep[u] = dep[father] + 1;
	fa[u][0] = father;
	for (int i = 1; i < 20; i++) {
		fa[u][i] = fa[fa[u][i - 1]][i - 1];
	}
	for (int i = h[u]; i; i = ne[i]) {
		int to = e[i];
		if (to == father) continue;
		dfs(to, u);
	}
}

int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	// 先跳到同一层
	for (int i = 19; i >= 0; i--) {
		if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
	}
	if (u == v) return u;
	for (int i = 19; i >= 0; i--) {
		if (fa[u][i] != fa[v][i]) {
			u = fa[u][i], v = fa[v][i];
		}
	}return fa[u][0];
}

int fun(int x, int y) {
	return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}

int main() {
	cin >> n;
	for (int i = 1; i < n; i++) {
		int x, y; 
		//cin >> x >> y;
		x = read(); y = read();
		add(x, y), add(y, x);
	}
	cin >> px >> py;
	int q; cin >> q;
	dfs(1, 0);
	while (q--) {
		int x, y; 
		//cin >> x >> y;
		x = read(); y = read();
		int d = min({ fun(x,y),fun(x,px) + fun(y,py),fun(x,py) + fun(y,px) });
		//cout << d << endl;
		printf("%d\n", d);
	}return 0;
}
相关推荐
人生在勤,不索何获-白大侠1 分钟前
day15——Java常用API(二):常见算法、正则表达式与异常处理详解
java·算法·正则表达式
Wo3Shi4七36 分钟前
双向队列
数据结构·算法·go
Wo3Shi4七40 分钟前
列表
数据结构·算法·go
Wo3Shi4七1 小时前
链表
数据结构·算法·go
Wo3Shi4七1 小时前
数组
数据结构·算法·go
CoovallyAIHub1 小时前
YOLOv13都来了,目标检测还卷得动吗?别急,还有这些新方向!
深度学习·算法·计算机视觉
转转技术团队2 小时前
边学边做:图片识别技术的学习与应用
后端·算法
一块plus2 小时前
2025 年值得一玩的最佳 Web3 游戏
算法·设计模式·程序员
前端拿破轮2 小时前
不是吧不是吧,leetcode第一题我就做不出来?😭😭😭
后端·算法·leetcode
一块plus2 小时前
什么是去中心化 AI?区块链驱动智能的初学者指南
人工智能·后端·算法