#1#2#3AC, 其余RE
问是什么原因???
#include<bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 5e6 + 1145;
constexpr int LM = 22;
vector<int> G[N];
int dep[N], fa[LM][N], root;
void dfs(int now, int father){
dep[now] = dep[father] + 1;
fa[now][0] = father;
for(int i = 1; i <= 20; i ++)
fa[now][i] = fa[fa[now][i - 1]][i - 1];
int len = G[now].size();
for(int i = 0; i < len; i ++){
if(G[now][i] != father){
dfs(G[now][i], now);
}
}
}
int lca(int x, int y){
if(dep[x] > dep[y])swap(x, y);
int wnt = dep[y] - dep[x];
for(int i = 20; i >= 0; i --){
if((1 << i) <= wnt){
wnt -= (1 << i);
y = fa[y][i];
}
}
if(x == y)return x;
for(int i = 20; i >= 0; i --){
if(fa[y][i] != fa[x][i]){
x = fa[x][i], y = fa[y][i];
}
}
return fa[x][0];
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);
int n, m, x, y, a, b;
cin >> n >> m >> root;
for(int i = 1; i < n ; i++){
cin >> x >> y;
G[x].push_back(y), G[y].push_back(x);
}
dfs(root, 0);
for(int i = 1; i <= m; i ++){
cin >> a >> b;
cout << lca(a, b) << endl;
}
return 0;
}