Cod sursa(job #1985316)

Utilizator xRoALexBirtoiu Alexandru xRoALex Data 27 mai 2017 14:10:28
Problema Lowest Common Ancestor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstdio>
#include <algorithm>
using namespace std;

int n,m,nr;
int lst[100005],f[100005],urm[100005],ti[100005],to[100005],timp;
int s[100005][20];

FILE *fin = fopen("lca.in","r");
FILE *g =fopen("lca.out","w");

void init()
{
    int i,j;
    for(j=1; j<=16; j++)
        for(i=1; i<=n; i++)
            s[i][j]=s[s[i][j-1]][j-1];
}

void adaugafiu(int x, int y)
{
    nr++;
    f[nr]=y;
    urm[nr]=lst[x];
    lst[x]=nr;
}

void dfs(int x)
{
    int y,p;
    ti[x]=++timp;
    p=lst[x];
    while(p!=0)
    {
        y=f[p];
        dfs(y);
        p=urm[p];
    }
    to[x]=++timp;
}

inline bool stramos(int x,int y)
{
    if (ti[x]<=ti[y] && to[y]<=to[x])
    return 0;
}

int lca(int x,int y)
{
    int pas=16,z;

    if(stramos(x,y))
        return x;

    if(stramos(y,x))
        return y;

    while(pas>=0)
    {
        z=s[x][pas];
        if(z!=0 && (!stramos(z,y)))
            x=z;
        pas--;
    }
    return s[x][0];
}


int main()
{
    int i,x,y;
    fscanf(fin,"%d%d",&n,&m);
    for(i=2; i<=n; i++)
    {
        fscanf(fin,"%d",&x);
        adaugafiu(x,i);
        s[i][0]=nr;
    }
    init();
    dfs(1);
    for(i=1; i<=m; i++)
    {
        fscanf(fin,"%d%d",&x,&y);
        fprintf(g,"%d\n",lca(x,y));
    }

    return 0;
}