Cod sursa(job #2510192)

Utilizator Briana_NeaguNeagu Briana Briana_Neagu Data 15 decembrie 2019 22:48:47
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.79 kb
#include <bits/stdc++.h>

using namespace std;

//varianta 6- Heavy Path + Stramosi

ifstream f("lca.in");
ofstream g("lca.out");

const int nmax = 100005;
vector <int> son[nmax];

int maxlevel = 0;

int father[nmax];
int level[nmax];
int where[nmax];
int weight[nmax];
int last[nmax];
int hpd_lvl[nmax];
int dp[nmax][18];
vector <int> link[nmax];

int cnt;
void hpd(int nod, int lvl)
{
    level[nod] = lvl;
    int found = 0;
    vector <int> now;
    for(auto el : son[nod])
    {
        hpd(el, lvl + 1);
        weight[nod] += weight[el] + 1;
        if (found == 0 || weight[found] < weight[el])
            found = el;
        now.push_back(where[el]);
    }
    if (!found)
    {
         where[nod] = cnt;
         last[cnt] = nod;
         cnt ++;
    }
    else
    {
        where[nod] = where[found];
        last[where[nod]] = nod;
    }
    for (auto chain : now)
        link[where[nod]].push_back(chain);
}

void stramosi(int chain, int lvl)
{
    hpd_lvl[chain] = lvl;
    maxlevel = max(maxlevel, lvl);
    for (auto el : link[chain])
        if (el != chain)
        {
            assert(dp[el][0] == 0);
            dp[el][0] = chain;
            stramosi(el, lvl + 1);
        }
}

void climb(int &a, int &b)
{
    if (hpd_lvl[where[a]] < hpd_lvl[where[b]])
        swap(a, b);
    int ch_a = where[a];
    int ch_b = where[b];
    int pow;
    for (pow = 1; (1 << pow) < hpd_lvl[ch_a]; ++ pow);
    while (pow >= 0)
    {
       if (hpd_lvl[ch_a] - (1 << pow) > hpd_lvl[ch_b])
            ch_a = dp[ch_a][pow];
        -- pow;
    }
    a = father[last[ch_a]];
}

int solve(int a, int b)
{
    if (hpd_lvl[where[a]] != hpd_lvl[where[b]])
        climb(a, b);

    int ch_a = where[a];
    int ch_b = where[b];
    if (ch_a == ch_b)
        if (level[a] < level[b])
            return a;
        else
            return b;
    int pow;
    assert(hpd_lvl[ch_a] == hpd_lvl[ch_b]);
    for (pow = 1; (1 << pow) < hpd_lvl[ch_a]; ++ pow);

    while (pow >= 0)
    {
        if (dp[ch_a][pow] != dp[ch_b][pow])
        {
            ch_a = dp[ch_a][pow];
            ch_b = dp[ch_b][pow];
        }
        -- pow;
    }
    a = father[last[ch_a]];
    b = father[last[ch_b]];
    assert(where[a] == where[b]);
    if (level[a] < level[b])
        return a;
    else
        return b;
}

int main()
{
    int n, m;
    f >> n >> m;
    for (int i = 1 ;i < n; ++ i)
    {
        int x;
        f >> x;
        father[i + 1] = x;
        son[x].push_back(i + 1);
    }
    father[1] = 1;
    hpd(1, 0);
    stramosi(where[1], 0);
    dp[where[1]][0] = where[1];

    for (int j = 1; (1 << j) <= maxlevel; ++ j)
      for (int chain = 0; chain < n; ++ chain)
            dp[chain][j] = dp[dp[chain][j - 1]][j - 1];

    for (int i = 1; i <= m; ++ i)
    {
        int a, b;
        f >> a >> b;
        g << solve(a, b) << '\n';
    }
}