Cod sursa(job #1877036)

Utilizator UrsuDanUrsu Dan UrsuDan Data 12 februarie 2017 20:58:08
Problema Lowest Common Ancestor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdio>
#include <vector>

using namespace std;

vector< vector<int> >g(100010);

int dad[100010][17];
int depth[100010];

void dfs(int node)
{
    int i;
    for(i=1; i<=16; i++)
        dad[node][i]=dad[dad[node][i-1]][i-1];
    for(i=0; i<g[node].size(); i++)
    {
        depth[g[node][i]]=depth[node]+1;
        dfs(g[node][i]);
    }
}

int stramos(int node,int p)
{
    int step;
    for(step=0; step<=16; step++)
        if(p&(1<<step))
            node=dad[node][step];
    return node;
}

void equal_depth(int &a,int &b)
{
    if(depth[a]==depth[b])
        return;
    if(depth[a]>depth[b])
        a=stramos(a,depth[a]-depth[b]);
    else
        b=stramos(b,depth[b]-depth[a]);
}

int lca(int a,int b)
{
    int step;
    equal_depth(a,b);
    if(a==b)
        return b;
    for(step=16; step>=0; step--)
    {
        if(dad[a][step]!=dad[b][step])
        {
            a=dad[a][step];
            b=dad[b][step];
        }
    }
    return dad[a][0];
}

int main()
{
    freopen("lca.in","r",stdin);
    freopen("lca.out","w",stdout);
    int n,m,i,x,a,b;
    scanf("%d%d",&n,&m);
    for(i=2; i<=n; i++)
    {
        scanf("%d",&x);
        g[x].push_back(i);
        dad[i][0]=x;
    }
    dfs(1);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        printf("%d\n",lca(a,b));
    }
    return 0;
}