爆炸的三个点分别是2,10,9.MLE
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int top[500005],n,m,r,p,a[500005],fa[500005],head[500005],num,s;
int dep[500005],size[500005],son[500005];
struct edge
{
int t,nxt;
}e[500005];
void add_edge(int f,int t)
{
e[++num].nxt=head[f];
e[num].t=t;head[f]=num;
}
void dfs1(int x,int u,int d)
{
dep[x]=d;fa[x]=u;size[x]=1;
int Max=0;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].t;
if(v==fa[x]) continue;
dfs1(v,x,d+1);
if(size[v]>Max) son[x]=v,Max=size[v];
size[x]+=size[v];
}
}
void dfs2(int x,int tp)
{
top[x]=tp;
if(!son[x]) return ;
else dfs2(son[x],tp);
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].t;
if(v==son[x]||v==fa[x]) 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]];
}
if(dep[x]>dep[y]) return y;
else return x;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m>>s;
for(int i=1;i<n;++i)
{
int x,y;
cin>>x>>y;
add_edge(x,y);
add_edge(y,x);
}
num=0;
dfs1(s,s,1);
dfs2(s,s);
for(int i=1;i<=m;++i)
{
int x,y;
cin>>x>>y;
cout<<LCA(x,y)<<"\n";
}
return 0;
}