LCA

#include<bits/stdc++.h>
using namespace std;
#define maxn 555555
int n,m,s,x,y,a,b,k;
int deep[maxn],dis[maxn],tot;
int head[maxn],dp[maxn][22];
struct node
{
    int v,to;
} edge[maxn*2];
void add(int x,int y)
{
    edge[++tot].v=y;
    edge[tot].to=head[x];
    head[x]=tot;
}
void dfs(int cur,int pre)
{
    deep[cur]=deep[pre]+1;
    dp[cur][0]=pre;
    for(int i=1; (1<<i)<=deep[cur]; i++)
        dp[cur][i]=dp[dp[cur][i-1]][i-1];
    for(int i=head[cur]; i!=-1; i=edge[i].to)
        if(edge[i].v!=pre)
            dfs(edge[i].v,cur);
}
int lca(int x,int y)
{
    if(deep[x]<deep[y])
        swap(x,y);
    for(int i=k; i>=0; i--)
        if(deep[x]-(1<<i)>=deep[y])
            x=dp[x][i];
    if(x==y)
        return x;
    for(int i=k; i>=0; i--)
        if(dp[x][i]!=dp[y][i])
        {
            x=dp[x][i];
            y=dp[y][i];
        }
    return dp[x][0];
}
int main()
{
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1; i<=n; i++)
        head[i]=-1;
    for(int i=1; i<n; i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(s,0);
    k=log(n)/log(2);
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        printf("%d\n",lca(x,y));
    }
    return 0;
}
 

posted @ 2018-10-19 21:30  hum0r0  阅读(11)  评论(0)    收藏  举报