Skip to content

Commit

Permalink
🧑‍💻 improves tree diamater implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Jan 25, 2024
1 parent 7d285e9 commit cabd383
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
19 changes: 19 additions & 0 deletions algorithms/graphs/tree-diameter-(dp).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const int MAXN(1'000'000);
int N;
vi G[MAXN];

int diameter, toLeaf[MAXN];
void calcDiameter(int u = 0, int p = -1) {
int d1, d2;
d1 = d2 = -1;

for (auto v : G[u]) {
if (v != p) {
calcDiameter(v, u);
d1 = max(d1, toLeaf[v]);
tie(d1, d2) = minmax({d1, d2});
}
}
toLeaf[u] = d2 + 1;
diameter = max(diameter, d1 + d2 + 2);
}
File renamed without changes.
30 changes: 0 additions & 30 deletions algorithms/graphs/tree-diameter.cpp

This file was deleted.

0 comments on commit cabd383

Please sign in to comment.