Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial for problem Hot & Cold #4462

Merged
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0160020
wrote the stuff
TheGamingMousse May 3, 2024
248753e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 3, 2024
872a572
did some style changes
TheGamingMousse May 4, 2024
954ebc8
Merge branch 'Editorial-for-BTS-17-Hot-&-Cold' of https://github.com/…
TheGamingMousse May 4, 2024
3a57e62
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
518b7c3
Update bts-hotcold.mdx
SansPapyrus683 May 4, 2024
58ae5f1
put everything into a class
TheGamingMousse May 4, 2024
fd3a169
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
fbc2ae3
changed some stuff
TheGamingMousse May 4, 2024
c634527
Merge branch 'master' into Editorial-for-BTS-17-Hot-&-Cold
TheGamingMousse May 4, 2024
b8d81cd
changed some stuff
TheGamingMousse May 4, 2024
48ac89a
Merge branch 'Editorial-for-BTS-17-Hot-&-Cold' of https://github.com/…
TheGamingMousse May 4, 2024
073620d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
9400b9b
Update bts-hotcold.mdx
SansPapyrus683 May 4, 2024
281997c
Update solutions/platinum/bts-hotcold.mdx
TheGamingMousse May 4, 2024
8fb12fc
Update solutions/platinum/bts-hotcold.mdx
TheGamingMousse May 4, 2024
ee21978
made constructor a bit more detailed n stuff
TheGamingMousse May 4, 2024
cce5999
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
73e0ec0
Update bts-hotcold.mdx
TheGamingMousse May 4, 2024
26859fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
c3224b5
Update bts-hotcold.mdx
SansPapyrus683 May 4, 2024
c1a1f2f
added lil comment
TheGamingMousse May 4, 2024
cda0afe
Merge branch 'Editorial-for-BTS-17-Hot-&-Cold' of https://github.com/…
TheGamingMousse May 4, 2024
4e9fbcb
added comment
TheGamingMousse May 4, 2024
3aeacd5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 4, 2024
9277b33
fixed my oopsie lol
TheGamingMousse May 5, 2024
cbd57e0
Merge branch 'Editorial-for-BTS-17-Hot-&-Cold' of https://github.com/…
TheGamingMousse May 5, 2024
0710999
Update bts-hotcold.mdx
SansPapyrus683 May 5, 2024
deaad3a
Update bts-hotcold.mdx
SansPapyrus683 May 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions solutions/platinum/bts-hotcold.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Tree {
vector<int> tout;
int timer = 0;

void tour(int u, int p) {
void tour(int u, int p) {
tin[u] = timer++;
lift[0][u] = p;
depth[u] = depth[p] + 1;
Expand All @@ -81,15 +81,6 @@ 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 @@ -99,11 +90,11 @@ class Tree {
tour(1, 0);
}

bool is_ancestor(int u, int v) const {
bool is_ancestor(int u, int v) {
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved
return tin[u] <= tin[v] && tout[v] <= tout[u];
}

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

int dist(int u, int v, int anc = -1) const {
// calculates distance from u to anc, and then anc to v
// if anc is not provided, anc is set to lca(u, v)
int dist(int u, int v, int anc = -1) {
if (anc == -1) { anc = lca(u, v); }
return depth[u] + depth[v] - 2 * depth[anc];
}
Expand Down Expand Up @@ -168,6 +161,15 @@ 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
Loading