Cod sursa(job #1649380)

Utilizator Master011Dragos Martac Master011 Data 11 martie 2016 13:29:36
Problema Lowest Common Ancestor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.89 kb
#include<cstdio>
#include<algorithm>
using namespace std;

const int nMax = 100000 + 1;
const int lMax = 20;
int nodV[nMax], urm[nMax], lst[nMax];
int euler[nMax * 2], d[nMax * 2], mat[lMax][nMax * 2], lg[nMax * 2], firstPos[nMax];
bool viz[nMax];
int nre, n, k;

void adauga(int a, int b){
    nodV[++nre] = b;
    urm[nre] = lst[a];
    lst[a] = nre;
}

void dfs(int nod, int depth){
    int pos = lst[nod];
    int vec = nodV[pos];

    euler[++k] = nod;
    d[k] = depth;
    firstPos[nod] = k;

    while(pos){

        dfs(vec, depth + 1);
        euler[++k] = nod;
        d[k] = depth;

        pos = urm[pos];
        vec = nodV[pos];

    }
}

int lca(int a, int b){
    int x, y;
    if(firstPos[b] > firstPos[a]) {
        y = firstPos[b];
        x = firstPos[a];
    }
    else{
        y = firstPos[a];
        x = firstPos[b];
    }

    int lung = y - x + 1;
    if(d[mat[lg[lung]][y]] < d[mat[lg[lung]][x + (1 << lg[lung]) - 1]])
        return euler[mat[lg[lung]][y]];
    else
        return euler[mat[lg[lung]][x + (1 << lg[lung]) - 1]];
}

int main (){
    FILE *in = fopen("lca.in","r");
    FILE *out = fopen("lca.out","w");

    int m;
    fscanf(in,"%d%d", &n, &m);

    int a, b;
    for(int i = 1 ; i < n ; ++i){
        fscanf(in,"%d", &a);
        adauga(a, i + 1);
    }

    dfs(1, 0);

    lg[1] = 0;
    for(int i = 2 ; i <= k ; ++i ) lg[i] = lg[i / 2] + 1;
    for(int j = 1 ; j <= k ; ++j) mat[0][j] = j;

    for(int i = 1 ; i <= lg[k] ; ++i){
        for(int j = (1 << i); j <= k; ++j){
            if(d[mat[i - 1][j]] < d[mat[i - 1][j - (1 << (i - 1))]])
                mat[i][j] = mat[i - 1][j];
            else
                mat[i][j] = mat[i - 1][j - (1 << (i - 1))];
        }
    }

    for(int i = 0 ; i < m ; ++i){
        fscanf(in,"%d%d", &a, &b);
        fprintf(out,"%d\n", lca(a, b));
    }
}