Cod sursa(job #1163703)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 1 aprilie 2014 16:12:57
Problema Lowest Common Ancestor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.89 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "lca.in";
const char outfile[] = "lca.out";

ofstream fout(outfile);

const int MAXN = 100005;
const int MAXLG = 25;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, M, K, RMQ[MAXLG][MAXN << 2], Euler[MAXN << 2], Level[MAXN << 2];
int First[MAXN], Lg[MAXN << 2];
Graph G;

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
}

inline void DFs(int Node, int father, int actLevel) {
    Euler[++ K] = Node;
    Level[K] = actLevel;
    First[Node] = K;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(*it == father)
            continue;
        DFs(*it, Node, actLevel + 1);
        Euler[++ K] = Node;
        Level[K] = actLevel;
    }
}

inline void buildRMQ() {
    for(int i = 2 ; i <= K ; ++ i)
        Lg[i] = Lg[i >> 1] + 1;
    for(int i = 1 ; i <= K ; ++ i)
        RMQ[0][i] = i;
    for(int i = 1 ; (1 << i) <= K ; ++ i)
        for(int j = 1 ; j + (1 << i) - 1 <= K ; ++ j) {
            int l = (1 << (i - 1));
            RMQ[i][j] = RMQ[i - 1][j];
            if(Level[RMQ[i][j]] > Level[RMQ[i - 1][j + l]])
                RMQ[i][j] = RMQ[i - 1][j + l];
        }
}

inline int LCA(int x, int y) {
    x = First[x];
    y = First[y];
    if(x > y)
        swap(x, y);
    int lg = Lg[y - x + 1];
    int Ans = RMQ[lg][x];
    if(Level[Ans] > Level[RMQ[lg][y - (1 << lg) + 1]])
        Ans = RMQ[lg][y - (1 << lg) + 1];
    return Euler[Ans];
}

int main() {
    freopen(infile, "r", stdin);
    get(N); get(M);
    for(int i = 2 ; i <= N ; ++ i) {
        int x;
        get(x);
        G[x].push_back(i);
    }
    DFs(1, 0, 1);
    buildRMQ();
    for(int i = 1 ; i <= M ; ++ i) {
        int x, y;
        get(x);
        get(y);
        fout << LCA(x, y) << '\n';
    }
    fout.close();
    return 0;
}