Cod sursa(job #2899068)

Utilizator bubblegumixUdrea Robert bubblegumix Data 7 mai 2022 17:46:18
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.79 kb
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define lsb(x) ((x) & (-x))
#define max(...) max({__VA_ARGS__})
#define min(...) min({__VA_ARGS__})
#define hmap unordered_map
#define var auto &
#define hset unordered_set
#define pq priority_queue
#define exists(x, v) (v.find(x) != v.end())
#define inrange(x, a, b) (x >= a && x <= b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef pair<ull, ull> p64_u;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef HOME
    ~debug() { cerr << endl; }
    eni(!= ) cerr << boolalpha << i; ris;
}
eni(== ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";

}


#else
    sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define darr(x,n) debug() <<#x<<": "<< range(x,x+n+1)
#define dmat(x,n,m) for(int i=0;i<=n;i++) if(!i) debug() <<#x << ":" << range(x[i],x[i]+m+1); else debug() << range(x[i],x[i]+m+1);
const int lim = 1e5 + 5;
int a[lim];
int up[lim][25];
int lvl[lim];
int n, q;

int main()
{
    freopen("lca.in", "r", stdin);
    freopen("lca.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        cin >> a[i];
        up[i][0] = a[i];
        lvl[i] = lvl[up[i][0]] + 1;
    }
    darr(a, n);

    for (int j = 1; j <= 20; j++)
        for (int i = 1; i <= n; i++)
            up[i][j] = up[up[i][j - 1]][j - 1];

    dmat(up, n, 20);

    while (q--)
    {
        int x, y;
        cin >> x >> y;
        if (lvl[x] < lvl[y])
            swap(x, y);

        for (int i = 20; i >= 0; i--)
            if (lvl[x] - (1 << i) >= lvl[y])
                x = up[x][i];

        if (x == y)
        {
            cout << x << "\n";
            continue;
        }
        for (int i = 20; i >= 0; i--)
            if (up[x][i] != up[y][i])
            {
                x = up[x][i];
                y = up[y][i];
            }
        cout << up[x][0] << "\n";
    }

    return 0;
}