Luzhiled's Library

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

View the Project on GitHub ei1333/library

:warning: Disjoint-Set-Union-On-Tree
(graph/tree/disjoint-set-union-on-tree.hpp)

Depends on

Code

#pragma once

#include "../graph-template.hpp"

/**
 * @brief Disjoint-Set-Union-On-Tree
 */
template< typename T >
struct DisjointSetUnionOnTree : Graph< T > {
  using F = function< void(int) >;
  using Graph< T >::g;
  vector< int > heavy, sz, in, out, ord;
  const F expand, shrink, query;
 
  explicit DisjointSetUnionOnTree(int n, F expand, F shrink, F query)
      : Graph< T >(n), expand(move(expand)), shrink(move(shrink)), query(move(query)) {}
 
private:
  queue< int > que;
 
  int build_subtree(int idx) {
    in[idx] = ord.size();
    ord.emplace_back(idx);
    for(auto &to : g[idx]) {
      int sub = build_subtree(to);
      sz[idx] += sub;
      if(heavy[idx] == -1 || sz[heavy[idx]] < sub) {
        heavy[idx] = to;
      }
    }
    out[idx] = ord.size();
    return sz[idx];
  }
 
  void dfs(int idx, bool keep) {
    for(auto &to : g[idx]) {
      if(heavy[idx] == to) continue;
      dfs(to, false);
    }
    if(heavy[idx] != -1) {
      dfs(heavy[idx], true);
    }
    for(auto &to : g[idx]) {
      if(heavy[idx] == to) continue;
      for(int p = in[to]; p < out[to]; p++) {
        expand(ord[p]);
      }
    }
    expand(idx);
    query(idx);
    if(!keep) {
      for(int p = in[idx]; p < out[idx]; p++) shrink(ord[p]);
    }
  }
 
public:
  void build(int root = 0) {
    g = convert_rooted_tree(*this, root).g;
    sz.assign(g.size(), 1);
    heavy.assign(g.size(), -1);
    in.resize(g.size());
    out.resize(g.size());
    build_subtree(root);
    dfs(root, false);
  }
};
#line 2 "graph/tree/disjoint-set-union-on-tree.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }

  inline vector< Edge< T > > &operator[](const int &k) {
    return g[k];
  }

  inline const vector< Edge< T > > &operator[](const int &k) const {
    return g[k];
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;
#line 4 "graph/tree/disjoint-set-union-on-tree.hpp"

/**
 * @brief Disjoint-Set-Union-On-Tree
 */
template< typename T >
struct DisjointSetUnionOnTree : Graph< T > {
  using F = function< void(int) >;
  using Graph< T >::g;
  vector< int > heavy, sz, in, out, ord;
  const F expand, shrink, query;
 
  explicit DisjointSetUnionOnTree(int n, F expand, F shrink, F query)
      : Graph< T >(n), expand(move(expand)), shrink(move(shrink)), query(move(query)) {}
 
private:
  queue< int > que;
 
  int build_subtree(int idx) {
    in[idx] = ord.size();
    ord.emplace_back(idx);
    for(auto &to : g[idx]) {
      int sub = build_subtree(to);
      sz[idx] += sub;
      if(heavy[idx] == -1 || sz[heavy[idx]] < sub) {
        heavy[idx] = to;
      }
    }
    out[idx] = ord.size();
    return sz[idx];
  }
 
  void dfs(int idx, bool keep) {
    for(auto &to : g[idx]) {
      if(heavy[idx] == to) continue;
      dfs(to, false);
    }
    if(heavy[idx] != -1) {
      dfs(heavy[idx], true);
    }
    for(auto &to : g[idx]) {
      if(heavy[idx] == to) continue;
      for(int p = in[to]; p < out[to]; p++) {
        expand(ord[p]);
      }
    }
    expand(idx);
    query(idx);
    if(!keep) {
      for(int p = in[idx]; p < out[idx]; p++) shrink(ord[p]);
    }
  }
 
public:
  void build(int root = 0) {
    g = convert_rooted_tree(*this, root).g;
    sz.assign(g.size(), 1);
    heavy.assign(g.size(), -1);
    in.resize(g.size());
    out.resize(g.size());
    build_subtree(root);
    dfs(root, false);
  }
};
Back to top page