luckYrat's library.

This documentation is automatically generated by competitive-verifier/competitive-verifier


:heavy_check_mark: cpp/z_test/yosupo-lca.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://judge.yosupo.jp/problem/lca"

#include "../../cpp/template/template.cpp"

#include "../../cpp/graph/tree/lowest-common-ancestor.cpp"

int main(){
  int n,q;cin>>n>>q;
  vector<vector<int>> edge(n);
  for(int i = 0; n-1 > i; i++){
    int x;cin>>x;
    edge[x].push_back(i+1);
  }
  
  LCA lca(n, edge);
  for(int i = 0; q > i; i++){
    int u,v;cin>>u>>v;
    cout << lca.compute(u,v) << endl;
  }
}
#line 1 "cpp/z_test/yosupo-lca.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/lca"

#line 2 "cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/yosupo-lca.test.cpp"

#line 1 "cpp/data-structure/segment-tree/segment-tree.cpp"
template< typename T, typename F>
struct SegmentTree{
  private:
    int n;
    vector<T> node;

    F f;
    T initer;

  public:
    SegmentTree(int n_, F f, T initer) : f(f),initer(initer) {
      n = 1;
      while(n < n_)n*=2;
      node.resize(2*n-1, initer);
    }

    SegmentTree(int n_, F f,T initer, vector<T> x) : f(f),initer(initer) {
      n = 1;
      while(n < n_)n*=2;
      node.resize(2*n-1, initer);
      set(x);
    }

    void set(vector<T> x){
      for(int i = 0; (int)x.size() > i; i++){
        update(i,x[i]);
      }
    }

    void update(int x, T val){
      x += (n-1);

      node[x] = val;

      while(x > 0){
        x = (x-1)/2;
        node[x] = f(node[2*x+1],node[2*x+2]);
      }
    }

    void add(int x, T val){
      x += (n-1);
      
      node[x] += val;
      while(x > 0){
        x = (x-1)/2;
        node[x] = f(node[2*x+1],node[2*x+2]);
      }
    }

    T getf(int a,int b, int k=0, int l=0, int r=-1){
      
      if(r < 0){
        r = n-1;
      }
      if(r < a || b < l){
        return initer;
      }
      if(a <= l && r <= b){
        return node[k];
      }

      T vl = getf(a,b,2*k+1,l,(l+r)/2);
      T vr = getf(a,b,2*k+2,(l+r)/2+1,r);
      return f(vl,vr);
    }

    void debug(){
      int nw = 1;
      int curr = 1;
      cout << "---begin---" << endl;
      for(int i = 0; 2*n-1 > i; i++){
        cout << node[i] << " ";
        if(nw == curr)cout << endl,nw *= 2, curr=1;
        else curr++;
      }
      cout << "---end---" << endl;
    }
};

template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(int N, const F &f, T initer){
  return SegmentTree<T,F>{N,f,initer};
}

template <typename T, typename F>
SegmentTree<T, F> get_segment_tree(int N, const F &f, T initer, vector<T> x){
  return SegmentTree<T,F>{N,f,initer,x};
}
#line 2 "cpp/graph/tree/lowest-common-ancestor.cpp"

struct EulerTourForLCA {
  int n;
  vector<vector<int>> edge;
  vector<pair<int, int>> timeline;
  vector<pair<int,int>> stump;
  vector<int> parent;
  EulerTourForLCA(int n, vector<vector<int>> edge):
    n(n), edge(edge), timeline(2*n, {-1, 0}), stump(n, {-1,-1}), parent(n, 0), lock(n, false) {}

  void build(int top){
    timer = 0;
    lock.assign(n, false);
    lock[top] = true;
    dfs(top, 0);
  }
  const pair<int,int> operator[](int index){
    return stump[index];
  }
private:
  vector<bool> lock;
  int timer = 0;
  void dfs(int vertex, int depth){
    stump[vertex].first = timer;
    timeline[timer].first = vertex;
    timeline[timer].second = depth;
    timer++;
    for(int i = 0; (int)edge[vertex].size() > i; i++){
      if(!lock[edge[vertex][i]]){
        lock[edge[vertex][i]] = true;
        parent[edge[vertex][i]] = vertex;
        dfs(edge[vertex][i], depth+1);
      }
    }
    stump[vertex].second = timer;
    timeline[timer].first = parent[vertex];
    timeline[timer].second = depth-1;
    timer++;
  }
};


struct LowestCommonAncestor {
  int n;
  vector<vector<int>> edge;
  EulerTourForLCA et;
  function<pair<int,int>(pair<int,int>, pair<int,int>)> RMQ = [](pair<int,int> a, pair<int,int> b){
    if(a.second < b.second)return a;
    return b;
  };
  //function<const pair<int,int>> RMQ = [](pair<int,int> a, pair<int,int> b){
  //};
  SegmentTree<pair<int,int>, decltype(RMQ)> st;

  LowestCommonAncestor(int n, vector<vector<int>> edge):n(n), edge(edge), et(n, edge), st(2*n, RMQ, {n+1, n+1}){
    et.build(0);
    st.set(et.timeline);
  }

  int compute(int l, int r){
    auto ls = et[l].first;
    auto rs = et[r].first;
    if(ls > rs)swap(ls, rs);
    auto mn = st.getf(ls, rs);
    return mn.first;
  }
};
using LCA = LowestCommonAncestor;

#line 6 "cpp/z_test/yosupo-lca.test.cpp"

int main(){
  int n,q;cin>>n>>q;
  vector<vector<int>> edge(n);
  for(int i = 0; n-1 > i; i++){
    int x;cin>>x;
    edge[x].push_back(i+1);
  }
  
  LCA lca(n, edge);
  for(int i = 0; q > i; i++){
    int u,v;cin>>u>>v;
    cout << lca.compute(u,v) << endl;
  }
}

Test cases

Env Name Status Elapsed Memory
g++ binary_00 :heavy_check_mark: AC 1342 ms 127 MB
g++ binary_01 :heavy_check_mark: AC 1374 ms 127 MB
g++ binary_02 :heavy_check_mark: AC 1402 ms 127 MB
g++ example_00 :heavy_check_mark: AC 6 ms 3 MB
g++ line_00 :heavy_check_mark: AC 1092 ms 164 MB
g++ line_01 :heavy_check_mark: AC 1184 ms 192 MB
g++ line_02 :heavy_check_mark: AC 709 ms 25 MB
g++ line_03 :heavy_check_mark: AC 430 ms 179 MB
g++ line_04 :heavy_check_mark: AC 556 ms 122 MB
g++ max_line_00 :heavy_check_mark: AC 1405 ms 205 MB
g++ max_line_01 :heavy_check_mark: AC 1364 ms 205 MB
g++ max_line_02 :heavy_check_mark: AC 1427 ms 205 MB
g++ max_random_00 :heavy_check_mark: AC 2107 ms 120 MB
g++ max_random_01 :heavy_check_mark: AC 1624 ms 120 MB
g++ max_random_02 :heavy_check_mark: AC 1408 ms 120 MB
g++ path_graph_root_centroid_00 :heavy_check_mark: AC 1393 ms 182 MB
g++ path_graph_root_centroid_01 :heavy_check_mark: AC 1556 ms 182 MB
g++ path_graph_root_centroid_02 :heavy_check_mark: AC 1382 ms 182 MB
g++ random_00 :heavy_check_mark: AC 1172 ms 98 MB
g++ random_01 :heavy_check_mark: AC 1258 ms 112 MB
g++ random_02 :heavy_check_mark: AC 783 ms 16 MB
g++ random_03 :heavy_check_mark: AC 360 ms 106 MB
g++ random_04 :heavy_check_mark: AC 559 ms 75 MB
Back to top page