题目情境#

算法解析:#
树上两点距离 。先用 DFS 预处理深度和倍增祖先(或树链剖分),再用 LCA 求距离。本题为 LOJ 10130(树上距离板子)。
Python代码实现#
## https://loj.ac/p/10130
# 相比树链剖分板子,只在有标注“#距离”处修改了
import sys
sys.setrecursionlimit(int(1e7))
N = 100010
fa = [0] * N
son = [0] * N
dep = [0] * N
siz = [0] * N
top = [0] * N
# 距离
dis = [0] * N
e = [[] for i in range(N)]
def dfs1(u, f):
fa[u] = f
siz[u] = 1
dep[u] = dep[f] + 1
for v in e[u]:
if v == f:
continue
# 距离
dis[v] = dis[u] + 1
dfs1(v, u)
siz[u] += siz[v]
if siz[son[u]] < siz[v]:
son[u] = v
def dfs2(u, t):
top[u] = t
if not son[u]:
return
dfs2(son[u], t)
for v in e[u]:
if v == fa[u] or v == son[u]:
continue
dfs2(v, v)
def lca(u, v):
while top[u] != top[v]:
if dep[top[u]] < dep[top[v]]:
u, v = v, u
u = fa[top[u]]
if dep[u] < dep[v]:
return u
else:
return v
n = int(input())
for i in range(1, n):
a, b = map(int, input().split())
e[a].append(b)
e[b].append(a)
dfs1(1, 0)
dfs2(1, 1)
m = int(input())
for i in range(m):
a, b = map(int, input().split())
# 距离
d = dis[a] + dis[b] - dis[lca(a, b)] * 2
print(d)pythonC++代码实现#
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N=100010;
int n,m,a,b,c;
vector<int> e[N];
int dep[N],fa[N],son[N],sz[N],dis[N];
int top[N];
void dfs1(int u,int father){
fa[u]=father,dep[u]=dep[father]+1,sz[u]=1;
for(int v:e[u]){
if(v==father) continue;
dis[v]=dis[u]+1;
dfs1(v,u);
sz[u]+=sz[v];
if(sz[son[u]]<sz[v])son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;
if(!son[u]) return;
dfs2(son[u],t);
for(int v:e[u]){
if(v==fa[u]||v==son[u])continue;
dfs2(v,v);
}
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int main(){
scanf("%d",&n);
for(int i=1; i<n; i++){
scanf("%d%d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}
dfs1(1,0);
dfs2(1,1);
scanf("%d",&m);
while(m--){
scanf("%d%d",&a,&b);
int d=dis[a]+dis[b]-dis[lca(a,b)]*2;
printf("%d\n",d);
}
return 0;
}c