You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int INF = 0x3f3f3f3f;
|
|
const int N = 500010, M = 500010 * 2;
|
|
//邻接表
|
|
int e[M], h[N], idx, ne[M];
|
|
void add(int a, int b) {
|
|
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
|
|
int dep[N];
|
|
int f[N][22];
|
|
void bfs(int t) {
|
|
dep[t] = 1;
|
|
queue<int> q;
|
|
q.push(t);
|
|
while (q.size()) {
|
|
int u = q.front();
|
|
q.pop();
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (!dep[j]) {
|
|
dep[j] = dep[u] + 1;
|
|
q.push(j);
|
|
f[j][0] = u;
|
|
for (int k = 1; k <= 20; k++)
|
|
f[j][k] = f[f[j][k - 1]][k - 1];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
int lca(int a, int b) {
|
|
if (dep[a] < dep[b]) swap(a, b);
|
|
for (int k = 20; k >= 0; k--)
|
|
if (dep[f[a][k]] >= dep[b]) a = f[a][k];
|
|
if (a == b) return a;
|
|
for (int k = 20; k >= 0; k--)
|
|
if (f[a][k] != f[b][k])
|
|
a = f[a][k], b = f[b][k];
|
|
return f[a][0];
|
|
}
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
int n, m, s;
|
|
cin >> n >> m >> s;
|
|
int a, b;
|
|
for (int i = 0; i < n - 1; i++) {
|
|
cin >> a >> b;
|
|
add(a, b), add(b, a);
|
|
}
|
|
bfs(s);
|
|
for (int i = 1; i <= m; i++) {
|
|
cin >> a >> b;
|
|
printf("%d\n", lca(a, b));
|
|
}
|
|
return 0;
|
|
} |