0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 13:58:48 +00:00

H1008. 【模板】最近公共祖先

https://hydro.ac/record/61acb81d0ca64e079359e31f
This commit is contained in:
Baoshuo Ren 2021-12-05 21:04:04 +08:00
parent 620536d034
commit 4cda178d56
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -0,0 +1,62 @@
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
int n, q, u, v, fa[500005][20], dep[500005];
std::vector<int> g[500005];
void bfs(int root) {
memset(dep, 0x3f, sizeof(dep));
std::queue<int> q;
dep[0] = 0;
dep[root] = 1;
q.push(root);
while (!q.empty()) {
int t = q.front();
q.pop();
for (int v : g[t]) {
if (dep[v] > dep[t] + 1) {
dep[v] = dep[t] + 1;
q.push(v);
fa[v][0] = t;
for (int k = 1; k < 20; k++) {
fa[v][k] = fa[fa[v][k - 1]][k - 1];
}
}
}
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) std::swap(a, b);
for (int k = 19; k >= 0; k--) {
if (dep[fa[a][k]] >= dep[b]) {
a = fa[a][k];
}
}
if (a == b) return a;
for (int k = 19; k >= 0; k--) {
if (fa[a][k] != fa[b][k]) {
a = fa[a][k];
b = fa[b][k];
}
}
return fa[a][0];
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
bfs(1);
while (q--) {
scanf("%d%d", &u, &v);
printf("%d\n", lca(u, v));
}
return 0;
}