Skip to content

Commit

Permalink
Update bts-hotcold.mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
SansPapyrus683 authored May 4, 2024
1 parent 26859fa commit c3224b5
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions solutions/platinum/bts-hotcold.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class Tree {
tout[u] = timer - 1;
}

void dfs(int u, int p) {
for (int v : adj[u]) {
if (v == p) { continue; }
dfs(v, u);
diff[u][0] += diff[v][0] + diff[v][1];
diff[u][1] += diff[v][1];
}
}

public:
Tree(int n, vector<vector<int>> &adj)
: n(n), log2dist((int)log2(n) + 1), adj(adj),
Expand All @@ -90,11 +99,11 @@ class Tree {
tour(1, 0);
}

bool is_ancestor(int u, int v) {
bool is_ancestor(int u, int v) const {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int lca(int u, int v) {
int lca(int u, int v) const {
if (is_ancestor(u, v)) { return u; }
if (is_ancestor(v, u)) { return v; }
for (int i = log2dist - 1; i >= 0; i--) {
Expand All @@ -103,7 +112,7 @@ class Tree {
return lift[0][u];
}

int dist(int u, int v, int anc = -1) {
int dist(int u, int v, int anc = -1) const {
if (anc == -1) { anc = lca(u, v); }
return depth[u] + depth[v] - 2 * depth[anc];
}
Expand Down Expand Up @@ -159,15 +168,6 @@ class Tree {
}
}

void dfs(int u, int p) {
for (int v : adj[u]) {
if (v == p) { continue; }
dfs(v, u);
diff[u][0] += diff[v][0] + diff[v][1];
diff[u][1] += diff[v][1];
}
}

vector<ll> calculate_result() {
dfs(1, 0);
vector<ll> res(n + 1);
Expand Down

0 comments on commit c3224b5

Please sign in to comment.