-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrerooting.hpp
105 lines (90 loc) · 3.62 KB
/
rerooting.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
// Rerooting
// Reference:
// - https://atcoder.jp/contests/abc222/editorial/2749
// - https://null-mn.hatenablog.com/entry/2020/04/14/124151
template <class Edge, class Subtree, class Children, Children (*rake)(Children, Children),
Children (*add_edge)(Subtree, int, Edge), Subtree (*add_vertex)(Children, int),
Children (*e)()>
struct rerooting {
int n_;
std::vector<int> par, visited;
std::vector<std::vector<std::pair<int, Edge>>> to;
// dp_subtree[i] = DP(root=i, edge (i, par[i]) is removed).
std::vector<Subtree> dp_subtree;
// dp_par[i] = DP(root=par[i], edge (i, par[i]) is removed). dp_par[root] is meaningless.
std::vector<Subtree> dp_par;
// dpall[i] = DP(root=i, all edges exist).
std::vector<Subtree> dpall;
rerooting(const std::vector<std::vector<std::pair<int, Edge>>> &to_)
: n_(to_.size()), par(n_, -1), visited(n_, 0), to(to_) {
for (int i = 0; i < n_; ++i) dp_subtree.push_back(add_vertex(e(), i));
dp_par = dpall = dp_subtree;
}
void run_connected(int root) {
if (visited.at(root)) return;
visited.at(root) = 1;
std::vector<int> visorder{root};
for (int t = 0; t < int(visorder.size()); ++t) {
int now = visorder.at(t);
for (const auto &[nxt, _] : to[now]) {
if (visited.at(nxt)) continue;
visorder.push_back(nxt);
visited.at(nxt) = 1;
par.at(nxt) = now;
}
}
for (int t = int(visorder.size()) - 1; t >= 0; --t) {
const int now = visorder.at(t);
Children ch = e();
for (const auto &[nxt, edge] : to.at(now)) {
if (nxt != par.at(now)) ch = rake(ch, add_edge(dp_subtree.at(nxt), nxt, edge));
}
dp_subtree.at(now) = add_vertex(ch, now);
}
std::vector<Children> left;
for (int now : visorder) {
const int m = to.at(now).size();
left.assign(m + 1, e());
for (int j = 0; j < m; j++) {
const auto &[nxt, edge] = to.at(now).at(j);
const Subtree &st = (nxt == par.at(now) ? dp_par.at(now) : dp_subtree.at(nxt));
left.at(j + 1) = rake(left.at(j), add_edge(st, nxt, edge));
}
dpall.at(now) = add_vertex(left.back(), now);
Children rprod = e();
for (int j = m - 1; j >= 0; --j) {
const auto &[nxt, edge] = to.at(now).at(j);
if (nxt != par.at(now)) dp_par.at(nxt) = add_vertex(rake(left.at(j), rprod), now);
const Subtree &st = (nxt == par.at(now) ? dp_par.at(now) : dp_subtree.at(nxt));
rprod = rake(add_edge(st, nxt, edge), rprod);
}
}
}
void run() {
for (int i = 0; i < n_; ++i) {
if (!visited.at(i)) run_connected(i);
}
}
const Subtree &get_subtree(int root_, int par_) const {
if (par_ < 0) return dpall.at(root_);
if (par.at(root_) == par_) return dp_subtree.at(root_);
if (par.at(par_) == root_) return dp_par.at(par_);
std::exit(1);
}
};
/* Template:
struct Subtree {};
struct Children {};
struct Edge {};
Children e() { return Children(); }
Children rake(Children x, Children y) { return Children(); }
Children add_edge(Subtree x, int ch_id, Edge edge) { return Children(); }
Subtree add_vertex(Children x, int v_id) { return Subtree(); }
vector<vector<pair<int, Edge>>> to;
rerooting<Edge, Subtree, Children, rake, add_edge, add_vertex, e> tree(to);
*/