Cod sursa(job #2757648)

Utilizator llalexandruLungu Alexandru Ioan llalexandru Data 5 iunie 2021 16:14:37
Problema Lowest Common Ancestor Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.5 kb
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace chrono;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second

const long double PI = acos(-1);

template<class T>
class ChildrenTree
{
public:
   const int n;
   const int root;
   vector< vector<T> > adj;

   ChildrenTree(int n, int root) : n(n), root(root)
   {
      for (int i = 0; i < n; i++) adj.push_back(vector<T>());
   }

   void addChild(int x, T edge)
   {
      adj[x].push_back(edge);
   }
};

template<class T>
class DFSTree
{
public:
   static void run(const ChildrenTree<T>& tree, 
                   int (*getTo) (T edge), 
                   function<void(int, int)> visit = [](int x, int father) { })
   {
      vector<int> Stack;
      vector<int> Father(tree.n);
      Stack.push_back(tree.root);
      while (!Stack.empty())
      {
         int front = Stack.back();
         visit(front, Father[front]);
         Stack.pop_back();
         for (T edge : tree.adj[front])
         {
            int nxt = getTo(edge);
            Father[nxt] = front;
            Stack.push_back(nxt);
         }
      }
   }
};

template<class T>
class BinaryLifting
{
   static const int LIFT_MAX = 30;
   vector<vector<int>> dp;
   vector<int> lvl;

   void build(const ChildrenTree<T>& tree)
   {
      lvl[tree.root] = 0;
      DFSTree<int>::run(tree, [](int edge) { return edge; }, [&tree, this](int x, int father) {
         if (x != tree.root) lvl[x] = lvl[father] + 1;
         dp[x][0] = father;
         for (int i = 1; i < LIFT_MAX; i++) dp[x][i] = dp[dp[x][i - 1]][i - 1];
      });
   }
public:
   BinaryLifting(const ChildrenTree<T>& tree)
   {
      for (int i = 0; i < tree.n; i++) dp.push_back(vector<int>(LIFT_MAX));
      lvl.resize(tree.n);
      build(tree);
   }

   int run(int x, int y)
   {
      if (lvl[x] < lvl[y]) swap(x, y);
      for (int i = LIFT_MAX - 1; i >= 0 && lvl[x] > lvl[y]; i--)
         if (lvl[dp[x][i]] >= lvl[y]) x = dp[x][i];
      if (x == y) return x;
      for (int i = LIFT_MAX - 1; i >= 0; i--)
         if (dp[x][i] != dp[y][i]) x = dp[x][i], y = dp[y][i];
      return dp[x][0];
   }
};


int main()
{
   ios::sync_with_stdio(false);

   freopen("lca.in", "r", stdin);
   freopen("lca.out", "w", stdout);
   int n, q;
   cin >> n >> q;
   ChildrenTree<int> g(n, 0);
   for (int i=1; i<n; i++)
   {
      int x;
      cin >> x;
      x--;
      g.addChild(x, i);
   }
   BinaryLifting<int> bl(g);
   while (q--)
   {
      int x, y;
      cin >> x >> y; x--, y--;
      cout << bl.run(x, y) + 1 << '\n';
   }
   return 0;
}