Cod sursa(job #2565899)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 2 martie 2020 17:41:39
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 100005;
const int LMAX = 25;
vector <int> vec[NMAX];
int h[NMAX];
vector <int> poz;
int first[NMAX];
void dfs(int u, int f) {
    h[u] = h[f] + 1;
    poz.push_back(u);
    first[u] = poz.size();
    for(int v : vec[u]) {
        if(v != f) {
            dfs(v, u);
            poz.push_back(u);
        }
    }
}
struct ABC {
    int val;
    int poz;
};
ABC rmq[2 * NMAX][LMAX];
int p2[2 * NMAX];
ABC miny(ABC x, ABC y) {
    if(x.val < y.val) {
        return x;
    }
    return y;
}

int main() {
    int n, m;
    freopen("lca.in", "r", stdin);
    freopen("lca.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 2; i <= n; i++) {
        int x;
        scanf("%d", &x);
        vec[x].push_back(i);
        vec[i].push_back(x);
    }
    dfs(1, 0);
    int top = poz.size();
    p2[1] = 0;
    for(int p = 0; p < top; p++) {
        rmq[p + 1][0] = {h[poz[p]], poz[p]};
        p2[p + 2] = p2[(p + 2) / 2] + 1;
    }
    p2[top + 2] = p2[(top + 2) / 2] + 1;
    for(int p = 1; (1 << p) <= top; p++) {
        for(int i = (1 << p); i <= top; i++) {
            rmq[i][p] = miny(rmq[i][p - 1], rmq[i - (1 << (p - 1))][p - 1]);
        }
    }

    for(int i = 1; i <= m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        if(first[x] > first[y]) {
            swap(x, y);
        }
        int put = p2[first[y] - first[x] + 1];
        ABC sol = miny(rmq[first[y]][put], rmq[first[x] + (1 << put) - 1][put]);
        printf("%d\n", sol.poz);
    }
    return 0;
}