洛谷3379
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=,inf=1e9;
int n,m,x,y,root,tot,dep[maxn],son[maxn],size[maxn],fa[maxn],top[maxn],last[maxn];
struct edge{int to,pre;}e[maxn<<];
inline void read(int &k){
k=; int f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y){e[++tot].to=y; e[tot].pre=last[x]; last[x]=tot;}
void dfs1(int x){
size[x]=; dep[x]=dep[fa[x]]+;
for (int i=last[x],to;i;i=e[i].pre)
if ((to=e[i].to)!=fa[x]){
fa[to]=x; dfs1(to);
size[x]+=size[to];
if (size[to]>size[son[x]]) son[x]=to;
}
}
void dfs2(int x,int tp){
top[x]=tp;
if (son[x]) dfs2(son[x],tp);
for (int i=last[x],to;i;i=e[i].pre)
if ((to=e[i].to)!=fa[x]&&to!=son[x]) dfs2(to,to);
}
int lca(int x,int y){
int f1=top[x],f2=top[y];
while(f1!=f2){
if (dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
x=fa[f1]; f1=top[x];
}
return dep[x]<dep[y]?x:y;
}
int main(){
read(n); read(m); read(root);
for (int i=;i<n;i++) read(x),read(y),add(x,y),add(y,x);
dfs1(root); dfs2(root,root);
for (int i=;i<=m;i++) read(x),read(y),printf("%d\n",lca(x,y));
return ;
}