|
| 1 | +#pragma once |
| 2 | +#include <unordered_set> |
| 3 | +#include <vector> |
| 4 | + |
| 5 | +#include "../data_structure/fast_set.hpp" |
| 6 | +#include "../sparse_table/rmq_sparse_table.hpp" |
| 7 | + |
| 8 | +// Data structure maintaining "compressed graph" of subsets of the vertices of a tree |
| 9 | +// Known as "auxiliary tree" and "virtual tree" |
| 10 | +// https://noshi91.github.io/algorithm-encyclopedia/auxiliary-tree |
| 11 | +class auxiliary_tree { |
| 12 | + |
| 13 | + int n_ = 0; |
| 14 | + int root_ = -1; |
| 15 | + |
| 16 | + // Each node is labeled by both v (given as input) and t (DFS preorder) |
| 17 | + std::vector<int> v2t; // v2t[v] = t |
| 18 | + std::vector<int> t2v; // t2v[t] = v |
| 19 | + |
| 20 | + // To get LCA of two vertices in O(1) per query |
| 21 | + std::vector<int> _rmq_pos; |
| 22 | + StaticRMQ<std::pair<int, int>> _rmq; |
| 23 | + |
| 24 | + // Auxiliary tree info |
| 25 | + // Variables starting with '_' are labeled by t, not v |
| 26 | + int _auxiliary_root = -1; // LCA of all currently activated vertices |
| 27 | + fast_set _is_active; // "t in _is_active" iff t is activated |
| 28 | + fast_set _is_semiactive; // "t in _is_semiactive" iff t is used in the current tree |
| 29 | + std::vector<int> _parent; // _parent[t] = parent of t in the current tree |
| 30 | + std::vector<std::unordered_set<int>> _child; // _child[t] = children of t in the current tree |
| 31 | + |
| 32 | + int _get_lca(int t1, int t2) const { |
| 33 | + if (t1 > t2) std::swap(t1, t2); |
| 34 | + return _rmq.get(_rmq_pos.at(t1), _rmq_pos.at(t2) + 1).second; |
| 35 | + } |
| 36 | + |
| 37 | + void _add_edge(int tpar, int tchild) { |
| 38 | + assert(tpar != tchild); |
| 39 | + assert(_parent.at(tchild) == -1); |
| 40 | + |
| 41 | + _parent.at(tchild) = tpar; |
| 42 | + _child.at(tpar).insert(tchild); |
| 43 | + } |
| 44 | + |
| 45 | + void _erase_edge(int tpar, int tchild) { |
| 46 | + assert(tpar != tchild); |
| 47 | + assert(_parent.at(tchild) == tpar); |
| 48 | + |
| 49 | + _parent.at(tchild) = -1; |
| 50 | + _child.at(tpar).erase(tchild); |
| 51 | + } |
| 52 | + |
| 53 | +public: |
| 54 | + int n() const { return n_; } |
| 55 | + |
| 56 | + int original_root() const { return root_; } |
| 57 | + |
| 58 | + int auxiliary_root() const { return _auxiliary_root == -1 ? -1 : t2v.at(_auxiliary_root); } |
| 59 | + |
| 60 | + bool is_active(int v) const { return _is_active.contains(v2t.at(v)); } |
| 61 | + |
| 62 | + bool is_semiactive(int v) const { return _is_semiactive.contains(v2t.at(v)); } |
| 63 | + |
| 64 | + int get_parent(int v) const { |
| 65 | + const int t = v2t.at(v); |
| 66 | + return _parent.at(t) == -1 ? -1 : t2v.at(_parent.at(t)); |
| 67 | + } |
| 68 | + |
| 69 | + std::vector<int> get_children(int v) const { |
| 70 | + const int t = v2t.at(v); |
| 71 | + std::vector<int> ret; |
| 72 | + ret.reserve(_child.at(t).size()); |
| 73 | + for (int c : _child.at(t)) ret.push_back(t2v.at(c)); |
| 74 | + return ret; |
| 75 | + } |
| 76 | + |
| 77 | + auxiliary_tree() = default; |
| 78 | + |
| 79 | + auxiliary_tree(const std::vector<std::vector<int>> &to, int root) |
| 80 | + : n_(to.size()), root_(root), v2t(n_, -1), _rmq_pos(n_, -1), _is_active(n_), |
| 81 | + _is_semiactive(n_), _parent(n_, -1), _child(n_) { |
| 82 | + std::vector<std::pair<int, int>> dfspath; // (depth, t[v]) |
| 83 | + t2v.reserve(n_); |
| 84 | + |
| 85 | + auto rec = [&](auto &&self, int now, int prv, int depth) -> void { |
| 86 | + const int t = t2v.size(); |
| 87 | + v2t.at(now) = t; |
| 88 | + t2v.push_back(now); |
| 89 | + |
| 90 | + _rmq_pos.at(t) = dfspath.size(); |
| 91 | + dfspath.emplace_back(depth, t); |
| 92 | + |
| 93 | + for (int nxt : to.at(now)) { |
| 94 | + if (nxt == prv) continue; |
| 95 | + self(self, nxt, now, depth + 1); |
| 96 | + dfspath.emplace_back(depth, t); |
| 97 | + } |
| 98 | + }; |
| 99 | + rec(rec, root, -1, 0); |
| 100 | + |
| 101 | + _rmq = {dfspath, std::make_pair(n_, -1)}; |
| 102 | + } |
| 103 | + |
| 104 | + void activate(int v_) { |
| 105 | + const int t = v2t.at(v_); |
| 106 | + |
| 107 | + if (_is_semiactive.contains(t)) { |
| 108 | + |
| 109 | + // Already semiactive. Nothing to do. |
| 110 | + |
| 111 | + } else if (_auxiliary_root == -1) { |
| 112 | + |
| 113 | + // Add one vertex to empty set. |
| 114 | + _auxiliary_root = t; |
| 115 | + |
| 116 | + } else if (const int next_root = _get_lca(_auxiliary_root, t); next_root != _auxiliary_root) { |
| 117 | + |
| 118 | + // New node is outside the current tree. Update root. |
| 119 | + if (next_root != t) { |
| 120 | + _is_semiactive.insert(next_root); |
| 121 | + _add_edge(next_root, t); |
| 122 | + } |
| 123 | + _add_edge(next_root, _auxiliary_root); |
| 124 | + _auxiliary_root = next_root; |
| 125 | + |
| 126 | + } else if (const int tnxt = _is_semiactive.next(t, n_); |
| 127 | + tnxt < n_ and _get_lca(t, tnxt) == t) { |
| 128 | + |
| 129 | + // New node lies on the path of the current tree. Insert new node. |
| 130 | + const int tpar = _parent.at(tnxt); |
| 131 | + assert(tpar >= 0); |
| 132 | + |
| 133 | + // tpar->tnxt => tpar->t->tnxt |
| 134 | + _erase_edge(tpar, tnxt); |
| 135 | + _add_edge(tpar, t); |
| 136 | + _add_edge(t, tnxt); |
| 137 | + |
| 138 | + } else { |
| 139 | + |
| 140 | + // New node is "under" the current tree. |
| 141 | + const int tprv = _is_semiactive.prev(t, -1); |
| 142 | + assert(tprv >= 0); |
| 143 | + const int tprvlca = _get_lca(t, tprv), tnxtlca = tnxt < n_ ? _get_lca(t, tnxt) : n_; |
| 144 | + |
| 145 | + const int t2 = (tnxt == n_ or _get_lca(tprvlca, tnxtlca) == tnxtlca) ? tprv : tnxt; |
| 146 | + const int tlca = _get_lca(t, t2); |
| 147 | + |
| 148 | + if (!_is_semiactive.contains(tlca)) { |
| 149 | + const int tc = _is_semiactive.next(tlca, n_); |
| 150 | + const int tpar = _parent.at(tc); |
| 151 | + assert(tpar >= 0); |
| 152 | + // tpar->tc => tpar->tlca->tc |
| 153 | + _is_semiactive.insert(tlca); |
| 154 | + _erase_edge(tpar, tc); |
| 155 | + _add_edge(tpar, tlca); |
| 156 | + _add_edge(tlca, tc); |
| 157 | + } |
| 158 | + |
| 159 | + _add_edge(tlca, t); |
| 160 | + } |
| 161 | + |
| 162 | + _is_semiactive.insert(t); |
| 163 | + _is_active.insert(t); |
| 164 | + } |
| 165 | + |
| 166 | + void deactivate(int v_) { |
| 167 | + const int t = v2t.at(v_); |
| 168 | + |
| 169 | + if (!_is_active.contains(t)) return; |
| 170 | + |
| 171 | + const int num_children = _child.at(t).size(); |
| 172 | + |
| 173 | + if (num_children > 1) { // (1) |
| 174 | + |
| 175 | + // Nothing to do (just deactivate it). Still semiactivated. |
| 176 | + |
| 177 | + } else if (num_children == 1) { |
| 178 | + |
| 179 | + // Delete this vertex from the current tree. |
| 180 | + const int tchild = *_child.at(t).begin(); |
| 181 | + |
| 182 | + if (_parent.at(t) == -1) { |
| 183 | + |
| 184 | + // Root changes. |
| 185 | + // t->tchild => tchild |
| 186 | + _auxiliary_root = tchild; |
| 187 | + _erase_edge(t, tchild); |
| 188 | + |
| 189 | + } else { |
| 190 | + |
| 191 | + // tpar->t->tchild => tpar->tchild |
| 192 | + const int tpar = _parent.at(t); |
| 193 | + _erase_edge(tpar, t); |
| 194 | + _erase_edge(t, tchild); |
| 195 | + _add_edge(tpar, tchild); |
| 196 | + } |
| 197 | + |
| 198 | + _is_semiactive.erase(t); |
| 199 | + |
| 200 | + } else if (num_children == 0 and _parent.at(t) == -1) { |
| 201 | + |
| 202 | + // Erase the only vertex in the current tree. |
| 203 | + _auxiliary_root = -1; |
| 204 | + _is_semiactive.erase(t); |
| 205 | + |
| 206 | + } else { |
| 207 | + |
| 208 | + assert(num_children == 0 and _parent.at(t) != -1); |
| 209 | + |
| 210 | + const int tpar = _parent.at(t); |
| 211 | + const int tparpar = _parent.at(tpar); |
| 212 | + |
| 213 | + if (!_is_active.contains(tpar) and _child.at(tpar).size() == 2) { |
| 214 | + |
| 215 | + // In only this case, parent of t is also erased. |
| 216 | + const int t2 = |
| 217 | + t ^ (*_child.at(tpar).begin()) ^ (*std::next(_child.at(tpar).begin())); |
| 218 | + if (tparpar == -1) { |
| 219 | + // t<-tpar->t2 => t2 |
| 220 | + _auxiliary_root = t2; |
| 221 | + _is_semiactive.erase(tpar); |
| 222 | + _erase_edge(tpar, t2); |
| 223 | + } else { |
| 224 | + // tparpar->tpar->t2 => tparpar->t2 |
| 225 | + _is_semiactive.erase(tpar); |
| 226 | + _erase_edge(tparpar, tpar); |
| 227 | + _erase_edge(tpar, t2); |
| 228 | + _add_edge(tparpar, t2); |
| 229 | + } |
| 230 | + } |
| 231 | + _erase_edge(tpar, t); |
| 232 | + _is_semiactive.erase(t); |
| 233 | + } |
| 234 | + |
| 235 | + _is_active.erase(t); |
| 236 | + } |
| 237 | +}; |
0 commit comments