This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "structure/dynamic-tree/link-cut-tree.hpp"Link Cut Tree とは動的木の一つで, 辺の追加や削除などの木構造の動的な変化がある場合でも効率的にクエリを処理できます。
LinkCutTree< TreeDPInfo >()
TreeDPInfo は、次の構造体と関数を持つ構造体です。
struct TreeDPInfo {
  struct Path {};
  struct Info {};
  static Path vertex(const Info& u) {}
  static Path compress(const Path& p, const Path& c) {}
};
Path: Heavy edge で繋がる頂点をまとめた結果 (Path cluster) を表す構造体Info: 頂点を表す構造体vertex(u): 頂点 u のみからなる Path cluster を生成する関数compress(p, c): Path cluster p と c (p が根に近い側にある) をマージする関数以下のコードを Splay Tree により高速化したデータ構造とみなすことができます。
Path calc_heavy(int r) {
  vector< Path > paths;
  while(not g[r].empty()) {
    paths.push_back(vertex(info[r]));
    r = g[r][0]; // (r, g[r][0]) は Heavy edge
  }
  for(int i = 1; i < (int) paths.size(); i++) {
    paths[0] = compress(paths[0], paths[i]);
  }
  return paths[0];
}
NP expose(NP t)
頂点 t から根までのパスを Heavy edge で繋げます。t を Splay tree の根にして、t 自身を返します。
void link(NP child, NP parent)
頂点 child と parent との間に辺を追加します。
child と parent は異なる連結成分child は根(根ではない場合は先に evert(child) を呼び出すこと)void cut(NP child)
頂点 child と親との間にある辺を削除します。
child は根ではないvoid evert(t)
頂点 t を根に変更します。
NP alloc(const Info &v)
Info が v の新しい頂点を作成します。
bool is_connected(NP u, NP v)
頂点 u と v が同じ連結成分に属する場合は true、そうではない場合は false を返します。
関数内部で expose(u)、expose(v) の順で呼び出すため、Splay tree の木の根が変更されます。
vector<NP> build(vector<Info> &vs)
各 Info の値が vs[i] の新しい頂点たちを作成します。
NP lca(NP u, NP v)
頂点 u と v の最小共通祖先を返します。ただし、頂点 u と v が異なる連結成分に属する場合は nullptr を返します。
関数内部で expose(u)、expose(v) の順で呼び出すため、Splay tree の木の根が変更されます。
void set_key(NP t, const Info &v)
頂点 t の Info を v に変更します。
関数内部で expose(t) を呼び出すため、Splay tree の木の根が t に変更されます。
(1) const Path &query_path(NP u)
(2) const Path &query_path(NP u, NP v)
u までのパス上の頂点を Heavy edge で繋げ、それらを compress でマージした結果を返します。u から頂点 v までのパス上の頂点を Heavy edge で繋げ、それらを compress でマージした結果を返します。副作用として、頂点 u を根に変更します。pair<NP, Path> find_first(NP u, const C &check)
根から頂点 u までのパス上の頂点を Heavy edge で繋げます。
パス上の頂点から頂点 u までの頂点を compress した結果を path とします。check(path) が true となるパス上の頂点のうち、頂点 u に最も近い頂点をその path とともに返します。true となる頂点が存在しない場合は nullptr を返します。
check は、第一引数に Path、返り値が bool の関数check の結果は単調(根からある頂点まで返り値が true で、それ以降の頂点に対しては false)頂点 v から u までのパス上に出現する頂点を並べたときに k 番目に現れる頂点を見つけたい場合、以下のように書けます。
struct TreeDPInfo {
  struct Path { int sz; };
  struct Info { int idx; };
  static Path vertex(const Info& u) { return {1}; }
  static Path compress(const Path& p, const Path& c) { return {p.sz + c.sz}; }
};
auto res = lct.find_first(vs[u], vs[v],
  [](const TreeDPInfo::Path& p) { return p.sz >= k; });
template <typename TreeDPInfo>
struct LinkCutTree {
  using Path = typename TreeDPInfo::Path;
  using Info = typename TreeDPInfo::Info;
 private:
  struct Node {
    Node *l, *r, *p;
    Info info;
    Path sum, mus;
    bool rev;
    bool is_root() const { return not p or (p->l != this and p->r != this); }
    Node(const Info& info)
        : info(info), l(nullptr), r(nullptr), p(nullptr), rev(false) {}
  };
 public:
  using NP = Node*;
 private:
  void toggle(NP t) {
    swap(t->l, t->r);
    swap(t->sum, t->mus);
    t->rev ^= true;
  }
  void rotr(NP t) {
    NP x = t->p, y = x->p;
    push(x), push(t);
    if ((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if ((t->p = y)) {
      if (y->l == x) y->l = t;
      if (y->r == x) y->r = t;
    }
  }
  void rotl(NP t) {
    NP x = t->p, y = x->p;
    push(x), push(t);
    if ((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if ((t->p = y)) {
      if (y->l == x) y->l = t;
      if (y->r == x) y->r = t;
    }
  }
 public:
  LinkCutTree() = default;
  void push(NP t) {
    if (t->rev) {
      if (t->l) toggle(t->l);
      if (t->r) toggle(t->r);
      t->rev = false;
    }
  }
  void push_rev(NP t) {
    if (t->rev) {
      if (t->l) toggle(t->l);
      if (t->r) toggle(t->r);
      t->rev = false;
    }
  }
  void update(NP t) {
    Path key = TreeDPInfo::vertex(t->info);
    t->sum = key;
    t->mus = key;
    if (t->l) {
      t->sum = TreeDPInfo::compress(t->l->sum, t->sum);
      t->mus = TreeDPInfo::compress(t->mus, t->l->mus);
    }
    if (t->r) {
      t->sum = TreeDPInfo::compress(t->sum, t->r->sum);
      t->mus = TreeDPInfo::compress(t->r->mus, t->mus);
    }
  }
  void splay(NP t) {
    push(t);
    while (not t->is_root()) {
      NP q = t->p;
      if (q->is_root()) {
        push_rev(q), push_rev(t);
        if (q->l == t)
          rotr(t);
        else
          rotl(t);
      } else {
        NP r = q->p;
        push_rev(r), push_rev(q), push_rev(t);
        if (r->l == q) {
          if (q->l == t)
            rotr(q), rotr(t);
          else
            rotl(t), rotr(t);
        } else {
          if (q->r == t)
            rotl(q), rotl(t);
          else
            rotr(t), rotl(t);
        }
      }
    }
  }
  NP expose(NP t) {
    NP rp = nullptr;
    for (NP cur = t; cur; cur = cur->p) {
      splay(cur);
      cur->r = rp;
      update(cur);
      rp = cur;
    }
    splay(t);
    return rp;
  }
  void link(NP child, NP parent) {
    if (is_connected(child, parent)) {
      throw runtime_error(
          "child and parent must be different connected components");
    }
    if (child->l) {
      throw runtime_error("child must be root");
    }
    child->p = parent;
    parent->r = child;
    update(parent);
  }
  void cut(NP child) {
    expose(child);
    NP parent = child->l;
    if (not parent) {
      throw runtime_error("child must not be root");
    }
    child->l = nullptr;
    parent->p = nullptr;
    update(child);
  }
  void evert(NP t) {
    expose(t);
    toggle(t);
    push(t);
  }
  NP alloc(const Info& v) {
    NP t = new Node(v);
    update(t);
    return t;
  }
  bool is_connected(NP u, NP v) {
    expose(u), expose(v);
    return u == v or u->p;
  }
  vector<NP> build(vector<Info>& vs) {
    vector<NP> nodes(vs.size());
    for (int i = 0; i < (int)vs.size(); i++) {
      nodes[i] = alloc(vs[i]);
    }
    return nodes;
  }
  NP lca(NP u, NP v) {
    if (not is_connected(u, v)) return nullptr;
    expose(u);
    return expose(v);
  }
  void set_key(NP t, const Info& v) {
    expose(t);
    t->info = std::move(v);
    update(t);
  }
  const Path& query_path(NP u) {
    expose(u);
    return u->sum;
  }
  const Path& query_path(NP u, NP v) {
    evert(u);
    return query_path(v);
  }
  template <typename C>
  pair<NP, Path> find_first(NP u, const C& check) {
    expose(u);
    Path sum = TreeDPInfo::vertex(u->info);
    if (check(sum)) return {u, sum};
    u = u->l;
    while (u) {
      push(u);
      if (u->r) {
        Path nxt = TreeDPInfo::compress(u->r->sum, sum);
        if (check(nxt)) {
          u = u->r;
          continue;
        }
        sum = nxt;
      }
      Path nxt = TreeDPInfo::compress(TreeDPInfo::vertex(u->info), sum);
      if (check(nxt)) {
        splay(u);
        return {u, nxt};
      }
      sum = nxt;
      u = u->l;
    }
    return {nullptr, sum};
  }
};
/*
struct TreeDPInfo {
  struct Path {};
  struct Info {};
  static Path vertex(const Info& u) {}
  static Path compress(const Path& p, const Path& c) {}
};
*/
#line 1 "structure/dynamic-tree/link-cut-tree.hpp"
template <typename TreeDPInfo>
struct LinkCutTree {
  using Path = typename TreeDPInfo::Path;
  using Info = typename TreeDPInfo::Info;
 private:
  struct Node {
    Node *l, *r, *p;
    Info info;
    Path sum, mus;
    bool rev;
    bool is_root() const { return not p or (p->l != this and p->r != this); }
    Node(const Info& info)
        : info(info), l(nullptr), r(nullptr), p(nullptr), rev(false) {}
  };
 public:
  using NP = Node*;
 private:
  void toggle(NP t) {
    swap(t->l, t->r);
    swap(t->sum, t->mus);
    t->rev ^= true;
  }
  void rotr(NP t) {
    NP x = t->p, y = x->p;
    push(x), push(t);
    if ((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if ((t->p = y)) {
      if (y->l == x) y->l = t;
      if (y->r == x) y->r = t;
    }
  }
  void rotl(NP t) {
    NP x = t->p, y = x->p;
    push(x), push(t);
    if ((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if ((t->p = y)) {
      if (y->l == x) y->l = t;
      if (y->r == x) y->r = t;
    }
  }
 public:
  LinkCutTree() = default;
  void push(NP t) {
    if (t->rev) {
      if (t->l) toggle(t->l);
      if (t->r) toggle(t->r);
      t->rev = false;
    }
  }
  void push_rev(NP t) {
    if (t->rev) {
      if (t->l) toggle(t->l);
      if (t->r) toggle(t->r);
      t->rev = false;
    }
  }
  void update(NP t) {
    Path key = TreeDPInfo::vertex(t->info);
    t->sum = key;
    t->mus = key;
    if (t->l) {
      t->sum = TreeDPInfo::compress(t->l->sum, t->sum);
      t->mus = TreeDPInfo::compress(t->mus, t->l->mus);
    }
    if (t->r) {
      t->sum = TreeDPInfo::compress(t->sum, t->r->sum);
      t->mus = TreeDPInfo::compress(t->r->mus, t->mus);
    }
  }
  void splay(NP t) {
    push(t);
    while (not t->is_root()) {
      NP q = t->p;
      if (q->is_root()) {
        push_rev(q), push_rev(t);
        if (q->l == t)
          rotr(t);
        else
          rotl(t);
      } else {
        NP r = q->p;
        push_rev(r), push_rev(q), push_rev(t);
        if (r->l == q) {
          if (q->l == t)
            rotr(q), rotr(t);
          else
            rotl(t), rotr(t);
        } else {
          if (q->r == t)
            rotl(q), rotl(t);
          else
            rotr(t), rotl(t);
        }
      }
    }
  }
  NP expose(NP t) {
    NP rp = nullptr;
    for (NP cur = t; cur; cur = cur->p) {
      splay(cur);
      cur->r = rp;
      update(cur);
      rp = cur;
    }
    splay(t);
    return rp;
  }
  void link(NP child, NP parent) {
    if (is_connected(child, parent)) {
      throw runtime_error(
          "child and parent must be different connected components");
    }
    if (child->l) {
      throw runtime_error("child must be root");
    }
    child->p = parent;
    parent->r = child;
    update(parent);
  }
  void cut(NP child) {
    expose(child);
    NP parent = child->l;
    if (not parent) {
      throw runtime_error("child must not be root");
    }
    child->l = nullptr;
    parent->p = nullptr;
    update(child);
  }
  void evert(NP t) {
    expose(t);
    toggle(t);
    push(t);
  }
  NP alloc(const Info& v) {
    NP t = new Node(v);
    update(t);
    return t;
  }
  bool is_connected(NP u, NP v) {
    expose(u), expose(v);
    return u == v or u->p;
  }
  vector<NP> build(vector<Info>& vs) {
    vector<NP> nodes(vs.size());
    for (int i = 0; i < (int)vs.size(); i++) {
      nodes[i] = alloc(vs[i]);
    }
    return nodes;
  }
  NP lca(NP u, NP v) {
    if (not is_connected(u, v)) return nullptr;
    expose(u);
    return expose(v);
  }
  void set_key(NP t, const Info& v) {
    expose(t);
    t->info = std::move(v);
    update(t);
  }
  const Path& query_path(NP u) {
    expose(u);
    return u->sum;
  }
  const Path& query_path(NP u, NP v) {
    evert(u);
    return query_path(v);
  }
  template <typename C>
  pair<NP, Path> find_first(NP u, const C& check) {
    expose(u);
    Path sum = TreeDPInfo::vertex(u->info);
    if (check(sum)) return {u, sum};
    u = u->l;
    while (u) {
      push(u);
      if (u->r) {
        Path nxt = TreeDPInfo::compress(u->r->sum, sum);
        if (check(nxt)) {
          u = u->r;
          continue;
        }
        sum = nxt;
      }
      Path nxt = TreeDPInfo::compress(TreeDPInfo::vertex(u->info), sum);
      if (check(nxt)) {
        splay(u);
        return {u, nxt};
      }
      sum = nxt;
      u = u->l;
    }
    return {nullptr, sum};
  }
};
/*
struct TreeDPInfo {
  struct Path {};
  struct Info {};
  static Path vertex(const Info& u) {}
  static Path compress(const Path& p, const Path& c) {}
};
*/