Skip to content

Commit

Permalink
rerooting: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hitonanode committed May 5, 2024
1 parent 2290bd5 commit 1cfa596
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions tree/rerooting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,72 @@ 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[root]) return;
visited[root] = 1;
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[t];
for (const auto &edge : to[now]) {
int nxt = edge.first;
if (visited[nxt]) continue;
int now = visorder.at(t);
for (const auto &[nxt, _] : to[now]) {
if (visited.at(nxt)) continue;
visorder.push_back(nxt);
visited[nxt] = 1;
par[nxt] = now;
visited.at(nxt) = 1;
par.at(nxt) = now;
}
}

for (int t = int(visorder.size()) - 1; t >= 0; --t) {
int now = visorder[t];
const int now = visorder.at(t);
Children ch = e();
for (const auto &edge : to[now]) {
int nxt = edge.first;
if (nxt == par[now]) continue;
ch = rake(ch, add_edge(dp_subtree[nxt], nxt, edge.second));
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[now] = add_vertex(ch, now);
dp_subtree.at(now) = add_vertex(ch, now);
}

std::vector<Children> left;
for (int now : visorder) {
int m = int(to[now].size());
const int m = to.at(now).size();
left.assign(m + 1, e());
for (int j = 0; j < m; j++) {
int nxt = to[now][j].first;
const Subtree &st = (nxt == par[now] ? dp_par[now] : dp_subtree[nxt]);
left[j + 1] = rake(left[j], add_edge(st, nxt, to[now][j].second));
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[now] = add_vertex(left.back(), now);
dpall.at(now) = add_vertex(left.back(), now);

Children rprod = e();
for (int j = m - 1; j >= 0; --j) {
int nxt = to[now][j].first;
if (nxt != par[now]) dp_par[nxt] = add_vertex(rake(left[j], rprod), now);
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[now] ? dp_par[now] : dp_subtree[nxt]);
rprod = rake(add_edge(st, nxt, to[now][j].second), rprod);
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[i]) run_connected(i);
if (!visited.at(i)) run_connected(i);
}
}

Expand Down

0 comments on commit 1cfa596

Please sign in to comment.