Cod sursa(job #1227695)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 11 septembrie 2014 13:11:04
Problema Lowest Common Ancestor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.02 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";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
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; }

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

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

int n, m, level[MAXN << 2], first[MAXN], euler[MAXN << 2], k, rmq[20][MAXN << 2], Log[MAXN << 2];
Graph g;

void dfs(int node, int actlevel) {
    first[node] = ++ k;
    euler[k] = node;
    level[k] = actlevel;
    for(It it = g[node].begin(), fin = g[node].end(); it != fin ; ++ it) {
        dfs(*it, actlevel + 1);
        euler[++k] = node;
        level[k] = actlevel;
    }
}

void buildRMQ() {
    for(int i = 2 ; i <= k ; ++ i)
        Log[i] = Log[i >> 1] + 1;
    for(int i = 1 ; i <= k ; ++ i)
        rmq[0][i] = i;
    //(j, j + 2 ^ i - 1]) = (j, j + 2 ^ (i - 1) - 1), (j + 2 ^ (i - 1), j + 2 ^ i - 1);
    //rmq[i][j] = rmq[i - 1][j], rmq[i - 1][j + 2 ^ (i - 1)]
    for(int i = 1 ; (1 << i) <= k ; ++ i) {
        int aux = (1 << (i - 1));
        for(int j = 1 ; j + (1 << i) - 1 <= k ; ++ j) {
            rmq[i][j] = rmq[i - 1][j];
            if(level[rmq[i][j]] > level[rmq[i - 1][j + aux]])
                rmq[i][j] = rmq[i - 1][j + aux];
        }
    }
}

inline int lca(int x, int y) {
    int a = first[x];
    int b = first[y];
    if(a > b)
        swap(a, b);
    int l = Log[b - a + 1];
    int ans = rmq[l][a];
    if(level[ans] > level[rmq[l][b - (1 << l) + 1]])
        ans = rmq[l][b - (1 << l) + 1];
    return euler[ans];
}

int main() {
    cin.sync_with_stdio(false);
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    getInt(n);
    getInt(m);
    for(int i = 2 ; i <= n ; ++ i) {
        int x;
        getInt(x);
        g[x].push_back(i);
    }
    dfs(1, 1);
    buildRMQ();
    while(m --) {
        int x, y;
        getInt(x);
        getInt(y);
        fout << lca(x, y) << '\n';
    }

    fin.close();
    fout.close();
    return 0;
}