CP-templates

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Misuki743/CP-templates

:x: test/mytest_auxiliary_tree.test.cpp

Depends on

Code

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

#include "../default/t.cpp"
#include "../tree/prufer_recover.cpp"
#include "../enumerate/enumerate_bit.cpp"
#include "../enumerate/enumerate_twelvefold.cpp"
#include "../enumerate/enumerate_label_tree.cpp"
#include "../enumerate/enumerate_unlabel_rooted_tree.cpp"
#include "../ds/RMQ.cpp"
#include "../tree/LCA.cpp"
#include "../tree/auxiliary_tree.cpp"

auto auxiliary_tree_brute_force(vvi g, vi vs, int root = 0) {

  vc<bool> in_vs(size(g), false);
  for(int v : vs) in_vs[v] = true;

  vi old_id;
  vc<pii> e;
  vc<bool> is_critical_node;
  auto dfs = [&](int v, int p, auto &self) -> int {
    vi head;
    for(int x : g[v]) {
      if (x == p) continue;
      int y = self(x, v, self);
      if (y != -1) head.eb(y);
    }

    if (!in_vs[v] and ssize(head) <= 1)
      return head.empty() ? -1 : head[0];

    int i = size(old_id);
    old_id.emplace_back(v);
    is_critical_node.emplace_back(in_vs[v]);
    for(int j : head)
      e.emplace_back(i, j);
    return i;
  };

  dfs(root, -1, dfs);

  return tuple(e, old_id, is_critical_node);
}

void check(vector<vector<int>> g, int root) {
  vc<pii> e;
  for(int u = 0; u < ssize(g); u++)
    for(int v : g[u])
      if (u < v)
        e.emplace_back(u, v);

  auxiliary_tree aux(e, root);
  auto check2 = [&](vi vs) {
    auto [ep, old_id, crit] = aux.induced_tree(vs);
    auto [ep2, old_id2, crit2] = auxiliary_tree_brute_force(g, vs, root);

    assert(ssize(old_id) == ssize(old_id2));
    assert(set(old_id.begin(), old_id.end()) ==
           set(old_id2.begin(), old_id2.end()));

    vi val = old_id;
    ranges::sort(val);
    vi mp(size(g), -1);
    for(int i = 0; i < ssize(val); i++)
      mp[val[i]] = i;

    {
      vi p(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        p[i] = mp[old_id[i]];
      for(auto &[u, v] : ep)
        u = p[u], v = p[v];
      vi tmp(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        tmp[p[i]] = old_id[i];
      old_id.swap(tmp);
      vc<bool> tmp2(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        tmp2[p[i]] = crit[i];
      crit.swap(tmp2);
    }

    {
      vi p(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        p[i] = mp[old_id2[i]];
      for(auto &[u, v] : ep2)
        u = p[u], v = p[v];
      vi tmp(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        tmp[p[i]] = old_id2[i];
      old_id2.swap(tmp);
      vc<bool> tmp2(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        tmp2[p[i]] = crit2[i];
      crit2.swap(tmp2);
    }

    ranges::sort(ep);
    ranges::sort(ep2);

    assert(old_id == old_id2);
    assert(ep == ep2);
    assert(crit == crit2);
  };

  mt19937 rng(clock);
  const int n = ssize(g);
  if (ssize(g) <= 15) {
    for(int msk = 1; msk < (1 << n); msk++) {
      vi vs;
      for(int i = 0; i < n; i++)
        if (msk >> i & 1)
          vs.eb(i);
      shuffle(vs.begin(), vs.end(), rng);
      check2(vs);
    }
  } else {
    for(int sz = 1; sz <= n; sz <<= 1) {
      set<int> s;
      while(ssize(s) != sz)
        s.insert(rng() % n);
      vi vs;
      for(int x : s) vs.eb(x);
      shuffle(vs.begin(), vs.end(), rng);
      check2(vs);
    }
  }
}

auto random_relabel(vector<vector<int>> g) {
  mt19937 rng(clock);
  const int n = ssize(g);
  vector<int> p(n);
  iota(p.begin(), p.end(), 0);
  shuffle(p.begin(), p.end(), rng);
  vector<vector<int>> g2(n);
  for(int u = 0; u < n; u++)
    for(int v : g[u])
      g2[p[u]].eb(p[v]);
  return g2;
}

void a_plus_b() {
  int a, b; cin >> a >> b;
  cout << a + b << '\n';
}

int main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  mt19937 rng(clock);

  for(int n = 1; n <= 6; n++) {
    enumerate_label_tree(n, [&](vector<vector<int>> g) {
      for(int r = 0; r < n; r++)
        check(g, r);
    });
  }

  for(int n = 7; n <= 8; n++) {
    enumerate_unlabel_rooted_tree(n, [&](vector<vector<int>> g) {
      for(int r = 0; r < n; r++)
        check(g, r);
      g = random_relabel(g);
      for(int r = 0; r < n; r++)
        check(g, r);
    });
  }

  //path
  for(int n = 1; n <= 100; n += 2) {
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i++)
      g[i].emplace_back(i - 1), g[i - 1].emplace_back(i);
    check(g, 0);
    check(random_relabel(g), rng() % n);
  }

  //star
  for(int n = 2; n <= 100; n += 2) {
    for(int root : {0, n / 2, n - 1}) {
      vector<vector<int>> g(n);
      for(int i = 0; i < n; i++)
        if (i != root)
          g[root].emplace_back(i), g[i].emplace_back(root);
      check(g, root);
      check(g, root == 0);
    }
  }

  //almost path
  for(int tc = 0; tc < 10; tc++) {
    int n = 5'000;
    vector<vector<int>> g(n);
    for(int v = 1; v < n; v++) {
      int x = rng() % min(v, 5);
      g[v].emplace_back(x), g[x].emplace_back(v);
    }
    for(int r : {0, n / 2, n - 1})
      check(g, r);
    g = random_relabel(g);
    for(int r : {0, n / 2, n - 1})
      check(g, r);
  }

  a_plus_b();

  return 0;
}
#line 1 "test/mytest_auxiliary_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#line 1 "default/t.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>

#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define pb push_back
#define eb emplace_back
#define clock chrono::steady_clock::now().time_since_epoch().count()

using namespace std;

template<size_t I = 0, typename... args>
ostream& print_tuple(ostream& os, const tuple<args...> tu) {
  os << get<I>(tu);
  if constexpr (I + 1 != sizeof...(args)) {
    os << ' ';
    print_tuple<I + 1>(os, tu);
  }
  return os;
}
template<typename... args>
ostream& operator<<(ostream& os, const tuple<args...> tu) {
  return print_tuple(os, tu);
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(size_t i = 0; T x : arr) {
    os << x;
    if (++i != N) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(size_t i = 0; T x : vec) {
    os << x;
    if (++i != size(vec)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const multiset<T> &s) {
  for(size_t i = 0; T x : s) {
    os << x;
    if (++i != size(s)) os << ' ';
  }
  return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
  for(size_t i = 0; pair<T1, T2> x : m) {
    os << x.first << " : " << x.second;
    if (++i != size(m)) os << ", ";
  }
  return os;
}

#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T> using vc = vector<T>;
template<typename T> using vvc = vc<vc<T>>;
template<typename T> using vvvc = vc<vvc<T>>;

using vi = vc<int>;
using vll = vc<ll>;
using vvi = vvc<int>;
using vvll = vvc<ll>;

template<typename T> using min_heap = priority_queue<T, vc<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;

template<typename R, typename F, typename... Args>
concept R_invocable = requires(F&& f, Args&&... args) {
  { std::invoke(std::forward<F>(f), std::forward<Args>(args)...) } -> std::same_as<R>;
};
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, typename F>
requires R_invocable<T, F, T, T>
void pSum(rng &&v, F f) {
  if (!v.empty())
    for(T p = *v.begin(); T &x : v | views::drop(1))
      x = p = f(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>>
void pSum(rng &&v) {
  if (!v.empty())
    for(T p = *v.begin(); T &x : v | views::drop(1))
      x = p = p + x;
}

template<ranges::forward_range rng>
void Unique(rng &v) {
  ranges::sort(v);
  v.resize(unique(v.begin(), v.end()) - v.begin());
}

template<ranges::random_access_range rng>
rng invPerm(rng p) {
  rng ret = p;
  for(int i = 0; i < ssize(p); i++)
    ret[p[i]] = i;
  return ret;
}

template<ranges::random_access_range rng>
vi argSort(rng p) {
  vi id(size(p));
  iota(id.begin(), id.end(), 0);
  ranges::sort(id, {}, [&](int i) { return pair(p[i], i); });
  return id;
}

template<ranges::random_access_range rng, class T = ranges::range_value_t<rng>, typename F>
requires invocable<F, T&>
vi argSort(rng p, F proj) {
  vi id(size(p));
  iota(id.begin(), id.end(), 0);
  ranges::sort(id, {}, [&](int i) { return pair(proj(p[i]), i); });
  return id;
}

template<bool directed>
vvi read_graph(int n, int m, int base) {
  vvi g(n);
  for(int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<bool directed>
vvi adjacency_list(int n, vc<pii> e, int base) {
  vvi g(n);
  for(auto [u, v] : e) {
    u -= base, v -= base;
    g[u].emplace_back(v);
    if constexpr (!directed)
      g[v].emplace_back(u);
  }
  return g;
}

template<class T>
void setBit(T &msk, int bit, bool x) { (msk &= ~(T(1) << bit)) |= T(x) << bit; }
template<class T> void onBit(T &msk, int bit) { setBit(msk, bit, true); }
template<class T> void offBit(T &msk, int bit) { setBit(msk, bit, false); }
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }

template<class T>
T floorDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
  if (b < 0) a *= -1, b *= -1;
  return a >= 0 ? (a + b - 1) / b : a / b;
}

template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

#line 1 "tree/prufer_recover.cpp"
//empty vector would be assumed to be n = 2
vc<pii> prufer_recover(vi prufer_code) {
  const int n = ssize(prufer_code) + 2;
  assert(prufer_code.empty() or (ranges::min(prufer_code) >= 0 and ranges::max(prufer_code) < n));
  vi d(n, 1);
  for(int x : prufer_code) d[x]++;
  min_heap<int> leaf;
  for(int v = 0; v < n; v++)
    if (d[v] == 1)
      leaf.emplace(v);
  vc<pii> edges;
  for(int x : prufer_code) {
    int v = leaf.top(); leaf.pop();
    edges.emplace_back(v, x);
    if (--d[x] == 1)
      leaf.emplace(x);
  }
  int v = leaf.top(); leaf.pop();
  edges.emplace_back(v, leaf.top());
  return edges;
}
#line 1 "enumerate/enumerate_bit.cpp"

template<typename F, typename INT>
requires invocable<F, INT>
void enumerate_subset(INT msk, F f) {
  for(INT x = msk; x > 0; x = (x - 1) & msk)
    f(x);
  f(0);
}
#line 1 "enumerate/enumerate_twelvefold.cpp"
//#include "enumerate/bit.cpp"

template<typename F>
requires invocable<F, vector<int>>
void enumerate_cartesian_power(int n, int k, F f) {
  assert(min(n, k) >= 0);
  vector<int> p(k);
  auto dfs = [&](int i, auto &self) -> void {
    if (i == k) {
      f(p);
    } else {
      for(int x = 0; x < n; x++) {
        p[i] = x;
        self(i + 1, self);
      }
    }
  };
  dfs(0, dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_permutation(int n, F f) {
  assert(n >= 0);
  vector<int> p(n);
  iota(p.begin(), p.end(), 0);
  do { f(p); } while(next_permutation(p.begin(), p.end()));
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_combination(int n, int k, F f) {
  assert(min(n, k) >= 0);
  vector<int> p;
  auto dfs = [&](auto &self) -> void {
    if (ssize(p) == k) {
      f(p);
    } else {
      for(int x = (p.empty() ? 0 : p.back() + 1); x + k - ssize(p) <= n; x++) {
        p.emplace_back(x);
        self(self);
        p.pop_back();
      }
    }
  };
  dfs(dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_set_partition(int n, F f) {
  assert(n >= 0);
  vector<int> p;
  int msk = (1 << n) - 1;
  auto dfs = [&](auto &self) -> void {
    if (msk == 0) {
      f(p);
    } else {
      int x = msk & (-msk);
      msk ^= x;
      enumerate_subset(msk, [&](int sub) {
        p.emplace_back(sub | x);
        msk ^= sub;
        self(self);
        msk ^= sub;
        p.pop_back();
      });
      msk ^= x;
    }
  };
  dfs(dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_multisubset(int n, int sum, F f) {
  assert(min(n, sum) >= 0);
  vector<int> p(n);
  auto dfs = [&](int i, auto &self) -> void {
    if (i == n) {
      if (sum == 0) f(p);
    } else {
      for(int x = sum; x >= 0; x--) {
        p[i] = x, sum -= x;
        self(i + 1, self);
        sum += x;
      }
    }
  };
  dfs(0, dfs);
}

template<typename F>
requires invocable<F, vector<int>>
void enumerate_integer_partition(int n, F f) {
  assert(n >= 0);
  vector<int> p;
  auto dfs = [&](int s, auto &self) -> void {
    if (s == 0) {
      f(p);
    } else {
      for(int x = (p.empty() ? s : min(p.back(), s)); x > 0; x--) {
        p.emplace_back(x);
        self(s - x, self);
        p.pop_back();
      }
    }
  };
  dfs(n, dfs);
}
#line 1 "enumerate/enumerate_label_tree.cpp"
//#include "tree/prufer_recover.cpp"
//#include "enumerate/enumerate_bit.cpp"
//#include "enumerate/enumerate_twelvefold.cpp"

template<typename F>
requires invocable<F, vector<vector<int>>>
void enumerate_label_tree(int n, F f) {
  assert(n > 0);
  if (n == 1) {
    f(vector<vector<int>>(1));
  } else {
    enumerate_cartesian_power(n, n - 2, [n, f](vector<int> a) {
      f(adjacency_list<false>(n, prufer_recover(a), 0));
    });
  }
}
#line 1 "enumerate/enumerate_unlabel_rooted_tree.cpp"
//number of unlabel rooted tree (1-based)
//1, 1, 2, 4, 9,
//20, 48, 115, 286, 719,
//1842, 4766, 12486, 32973, 87811,
//235381, 634847, 1721159, 4688676, 12826228,
//35221832, 97055181, 268282855, 743724984, 2067174645

//root is 0
template<typename F>
requires invocable<F, vector<vector<int>>>
void enumerate_unlabel_rooted_tree(int n, F f) {
  const int LIM = 25;
  assert(0 < n and n <= LIM);

  vector hash(1, array<int, LIM>{-1});
  array<int, LIM + 2> st;
  fill(st.begin(), st.end(), INT_MAX);
  st[0] = -1, st[1] = 0;

  auto size = [&](int id) {
    int r = 1;
    while(st[r] <= id) r++;
    return r - 1;
  };

  for(int m = 2; m <= n; m++) {
    st[m] = ssize(hash);
    array<int, LIM> h;
    int nxt = 0;
    auto dfs = [&](int pre_id, int sum, auto &self) -> void {
      if (sum == 0) {
        h[nxt++] = -1;
        hash.emplace_back(h);
        nxt--;
      } else {
        for(int x = min(pre_id, st[sum + 1] - 1); x >= 0; x--) {
          h[nxt++] = x;
          self(x, sum - size(x), self);
          nxt--;
        }
      }
    };
    dfs(INT_MAX, m - 1, dfs);
  }
  st[n + 1] = ssize(hash);

  for(int id = st[n]; id < st[n + 1]; id++) {
    vector<vector<int>> g(n);
    int nxt = 0;
    auto dfs = [&](int v, int id, auto &self) -> void {
      if (id == 0) return;
      for(int i = 0; hash[id][i] != -1; i++) {
        g[v].emplace_back(nxt), g[nxt].emplace_back(v);
        self(nxt++, hash[id][i], self);
      }
    };
    dfs(nxt++, id, dfs);
    f(g);
  }
}
#line 1 "ds/RMQ.cpp"
template<class T>
struct RMQ {
  uint64_t size;
  vector<T> base;
  vector<vector<T>> table;
  vector<uint32_t> msk;

  static const int lgw = 5;
  static const int w = 1 << lgw;
  RMQ(vector<T> _base) : size(ssize(_base)), base(_base), msk(size) {
    msk.back() = 1;
    for(int i = size - 2; i >= 0; i--) {
      msk[i] = msk[i + 1] << 1;
      while(msk[i] != 0 and base[i + countr_zero(msk[i])] >= base[i])
        msk[i] ^= 1u << countr_zero(msk[i]);
      msk[i] |= 1u;
    }

    table = vector(bit_width(size >> lgw), vector<T>(size >> lgw));
    if (!table.empty())
      for(uint64_t i = 0; i + w <= size; i += w)
        table[0][i >> lgw] = base[i + bit_width(msk[i]) - 1];
    for(int i = 1; i < ssize(table); i++)
      for(uint64_t j = 0; j < (size >> lgw); j++)
        if (j + (1 << (i - 1)) < (size >> lgw))
          table[i][j] = min(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
        else
          table[i][j] = table[i - 1][j];
  }

  T query(int l, int r) {
    if (l >= r) {
      return numeric_limits<T>::max();
    } else if (r - l <= w) {
      return base[l + bit_width(msk[l] & (~0u >> (w - (r - l)))) - 1];
    } else {
      T ret = min(query(l, l + w), query(r - w, r));
      l = (l + w) >> lgw, r >>= lgw;
      if (l == r) return ret;
      int range = bit_width((unsigned)(r - l)) - 1;
      return min({ret, table[range][l], table[range][r - (1 << range)]});
    }
  }
};
#line 1 "tree/LCA.cpp"
//#include "ds/RMQ.cpp"

struct LCA {
  vi dep, tin, tout, mp;
  RMQ<int> rmq;

  vi precomp(vc<pii> &e, int root) {
    const int n = ssize(e) + 1;

    dep = tin = tout = mp = vi(n);

    vi sz(n, 1), p(n), ord;
    {
      vi d(n);
      for(auto &[u, v] : e)
        p[u] ^= v, p[v] ^= u, d[u]++, d[v]++;

      d[root] = 0, p[root] = root;
      ord.reserve(n - 1);
      for(int i = 0; i < n; i++) {
        int v = i;
        while(d[v] == 1) {
          ord.emplace_back(v);
          sz[p[v]] += sz[v];
          d[v] = 0, d[p[v]]--, p[p[v]] ^= v;
          v = p[v];
        }
      }
    }

    vi dfn(n);
    {
      vi nxt(n, 1);
      for(int v : ord | views::reverse) {
        dfn[v] = nxt[p[v]], nxt[p[v]] += sz[v];
        nxt[v] = dfn[v] + 1;
        dep[v] = dep[p[v]] + 1;
      }
      vi().swap(ord);
      vi().swap(sz);
    }

    vi init(2 * n - 1);
    {
      vi dfn_ord = invPerm(std::move(dfn));

      int nxt = 0, pre = root;
      for(int v : dfn_ord) {
        while(pre != p[v]) {
          pre = p[pre], tout[pre] = nxt;
          init[nxt++] = pre;
        }
        tin[v] = tout[v] = nxt;
        init[nxt++] = pre = v;
      }

      while(pre != root) {
        pre = p[pre], tout[pre] = nxt;
        init[nxt++] = pre;
      }
    }

    {
      vi f(n);
      for(int x : dep) f[x]++;
      pSum(f);

      vi rank(n);
      for(int v = 0; v < n; v++) {
        rank[v] = --f[dep[v]];
        mp[rank[v]] = v;
      }
      for(int &v : init) v = rank[v];
    }

    return init;
  }

  LCA(vc<pii> e, int root = 0) : rmq(precomp(e, root)) {}

  int lca(int u, int v) {
    if (tin[u] > tin[v]) swap(u, v);
    return mp[rmq.query(tin[u], tout[v] + 1)];
  }

  int dis(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
  }

  bool is_ancestor_of(int u, int v) {
    return tin[u] <= tin[v] and tout[v] <= tout[u];
  }
};
#line 1 "tree/auxiliary_tree.cpp"
//#include "ds/RMQ.cpp"
//#include "tree/LCA.cpp"

struct auxiliary_tree {
  LCA lca;

  auxiliary_tree(vc<pii> e, int root = 0) : lca(e, root) {}

  auto induced_tree(vi vs) {
    const int P = 10;

    auto proj = [&](int v) { return lca.tin[v]; };
    if (ssize(vs) < (1 << P) / P) {
      ranges::sort(vs, {}, proj);
    } else {
      int mx = proj(ranges::max(vs, {}, proj));
      for(int i = 0; mx > 0; i++, mx >>= P){
        array<int, (1 << P) + 1> f = {};
        for(int v : vs)
          f[(proj(v) >> (i * P) & ((1 << P) - 1)) + 1]++;
        pSum(f);
        vi nxt(size(vs));
        for(int v : vs)
          nxt[f[proj(v) >> (i * P) & ((1 << P) - 1)]++] = v;
        vs.swap(nxt);
      }
    }

    vi s, old_id;
    vc<bool> is_critical_node;
    auto push_new_vertex = [&](int v, bool critical = true) {
      s.emplace_back(ssize(old_id));
      old_id.emplace_back(v);
      is_critical_node.emplace_back(critical);
    };
    
    if (int v = lca.lca(vs[0], vs.back()); v != vs[0])
      push_new_vertex(v, false);

    vc<pii> e;
    for(int v : vs) {
      if (s.empty() or lca.is_ancestor_of(old_id[s.back()], v)) {
        push_new_vertex(v);
      } else {
        int last_pop = -1;
        do {
          int j = s.back(); s.pop_back();
          if (last_pop != -1)
            e.emplace_back(j, last_pop);
          last_pop = j;
        } while(!lca.is_ancestor_of(old_id[s.back()], v));

        if (int x = lca.lca(old_id[last_pop], v); x != old_id[s.back()])
          push_new_vertex(x, false);
        e.emplace_back(s.back(), last_pop);

        push_new_vertex(v);
      }
    }

    for(int i = 1; i < ssize(s); i++)
      e.emplace_back(s[i - 1], s[i]);

    return tuple(e, old_id, is_critical_node);
  }
};
#line 12 "test/mytest_auxiliary_tree.test.cpp"

auto auxiliary_tree_brute_force(vvi g, vi vs, int root = 0) {

  vc<bool> in_vs(size(g), false);
  for(int v : vs) in_vs[v] = true;

  vi old_id;
  vc<pii> e;
  vc<bool> is_critical_node;
  auto dfs = [&](int v, int p, auto &self) -> int {
    vi head;
    for(int x : g[v]) {
      if (x == p) continue;
      int y = self(x, v, self);
      if (y != -1) head.eb(y);
    }

    if (!in_vs[v] and ssize(head) <= 1)
      return head.empty() ? -1 : head[0];

    int i = size(old_id);
    old_id.emplace_back(v);
    is_critical_node.emplace_back(in_vs[v]);
    for(int j : head)
      e.emplace_back(i, j);
    return i;
  };

  dfs(root, -1, dfs);

  return tuple(e, old_id, is_critical_node);
}

void check(vector<vector<int>> g, int root) {
  vc<pii> e;
  for(int u = 0; u < ssize(g); u++)
    for(int v : g[u])
      if (u < v)
        e.emplace_back(u, v);

  auxiliary_tree aux(e, root);
  auto check2 = [&](vi vs) {
    auto [ep, old_id, crit] = aux.induced_tree(vs);
    auto [ep2, old_id2, crit2] = auxiliary_tree_brute_force(g, vs, root);

    assert(ssize(old_id) == ssize(old_id2));
    assert(set(old_id.begin(), old_id.end()) ==
           set(old_id2.begin(), old_id2.end()));

    vi val = old_id;
    ranges::sort(val);
    vi mp(size(g), -1);
    for(int i = 0; i < ssize(val); i++)
      mp[val[i]] = i;

    {
      vi p(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        p[i] = mp[old_id[i]];
      for(auto &[u, v] : ep)
        u = p[u], v = p[v];
      vi tmp(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        tmp[p[i]] = old_id[i];
      old_id.swap(tmp);
      vc<bool> tmp2(size(old_id));
      for(int i = 0; i < ssize(p); i++)
        tmp2[p[i]] = crit[i];
      crit.swap(tmp2);
    }

    {
      vi p(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        p[i] = mp[old_id2[i]];
      for(auto &[u, v] : ep2)
        u = p[u], v = p[v];
      vi tmp(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        tmp[p[i]] = old_id2[i];
      old_id2.swap(tmp);
      vc<bool> tmp2(size(old_id2));
      for(int i = 0; i < ssize(p); i++)
        tmp2[p[i]] = crit2[i];
      crit2.swap(tmp2);
    }

    ranges::sort(ep);
    ranges::sort(ep2);

    assert(old_id == old_id2);
    assert(ep == ep2);
    assert(crit == crit2);
  };

  mt19937 rng(clock);
  const int n = ssize(g);
  if (ssize(g) <= 15) {
    for(int msk = 1; msk < (1 << n); msk++) {
      vi vs;
      for(int i = 0; i < n; i++)
        if (msk >> i & 1)
          vs.eb(i);
      shuffle(vs.begin(), vs.end(), rng);
      check2(vs);
    }
  } else {
    for(int sz = 1; sz <= n; sz <<= 1) {
      set<int> s;
      while(ssize(s) != sz)
        s.insert(rng() % n);
      vi vs;
      for(int x : s) vs.eb(x);
      shuffle(vs.begin(), vs.end(), rng);
      check2(vs);
    }
  }
}

auto random_relabel(vector<vector<int>> g) {
  mt19937 rng(clock);
  const int n = ssize(g);
  vector<int> p(n);
  iota(p.begin(), p.end(), 0);
  shuffle(p.begin(), p.end(), rng);
  vector<vector<int>> g2(n);
  for(int u = 0; u < n; u++)
    for(int v : g[u])
      g2[p[u]].eb(p[v]);
  return g2;
}

void a_plus_b() {
  int a, b; cin >> a >> b;
  cout << a + b << '\n';
}

int main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  mt19937 rng(clock);

  for(int n = 1; n <= 6; n++) {
    enumerate_label_tree(n, [&](vector<vector<int>> g) {
      for(int r = 0; r < n; r++)
        check(g, r);
    });
  }

  for(int n = 7; n <= 8; n++) {
    enumerate_unlabel_rooted_tree(n, [&](vector<vector<int>> g) {
      for(int r = 0; r < n; r++)
        check(g, r);
      g = random_relabel(g);
      for(int r = 0; r < n; r++)
        check(g, r);
    });
  }

  //path
  for(int n = 1; n <= 100; n += 2) {
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i++)
      g[i].emplace_back(i - 1), g[i - 1].emplace_back(i);
    check(g, 0);
    check(random_relabel(g), rng() % n);
  }

  //star
  for(int n = 2; n <= 100; n += 2) {
    for(int root : {0, n / 2, n - 1}) {
      vector<vector<int>> g(n);
      for(int i = 0; i < n; i++)
        if (i != root)
          g[root].emplace_back(i), g[i].emplace_back(root);
      check(g, root);
      check(g, root == 0);
    }
  }

  //almost path
  for(int tc = 0; tc < 10; tc++) {
    int n = 5'000;
    vector<vector<int>> g(n);
    for(int v = 1; v < n; v++) {
      int x = rng() % min(v, 5);
      g[v].emplace_back(x), g[x].emplace_back(v);
    }
    for(int r : {0, n / 2, n - 1})
      check(g, r);
    g = random_relabel(g);
    for(int r : {0, n / 2, n - 1})
      check(g, r);
  }

  a_plus_b();

  return 0;
}
Back to top page