Skip to content

Commit 0a00545

Browse files
dsu/kruskal_tree: tighten allocation to 2*n-1 (#242)
A Kruskal reconstruction tree over n leaves has at most 2*n-1 nodes, so allocate exactly that instead of 2*n. Document that n >= 1 is required, since 2*n-1 (and the lcp size n-1 in the suffix-array / manacher code) underflows to a huge size_t when n == 0.
1 parent e342aec commit 0a00545

4 files changed

Lines changed: 5 additions & 1 deletion

File tree

library/dsu/kruskal_tree.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#pragma once
22
//! https://mzhang2021.github.io/cp-blog/kruskal/
3+
//! requires n >= 1 (2*n-1 allocation underflows when n == 0)
34
//! @time O(n log n)
45
//! @space O(n)
56
struct kr_tree {
67
int id;
78
vi p;
89
vector<vi> g;
9-
kr_tree(int n): id(n), p(2 * n, -1), g(2 * n) {}
10+
kr_tree(int n): id(n), p(2 * n - 1, -1), g(2 * n - 1) {}
1011
int f(int u) { return p[u] < 0 ? u : p[u] = f(p[u]); }
1112
bool join(int u, int v) {
1213
if ((u = f(u)) == (v = f(v))) return 0;

library/strings/manacher/manacher.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! string b a a b a
1515
//! center 1 3 5 7
1616
//!
17+
//! requires n >= 1 (2*n-1 allocation underflows when n == 0)
1718
//! @time O(n)
1819
//! @space O(n)
1920
vi manacher(const auto& s) {

library/strings/suffix_array/suffix_array.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
//! auto [sa1, sa_inv1, lcp1] = get_sa(s_vec, 100'001);
3333
//! @endcode
3434
//!
35+
//! requires n >= 1 (lcp size n-1 underflows when n == 0)
3536
//! @time O(nlogn + max_num)
3637
//! @space O(n + max_num)
3738
auto get_sa(const auto& s, int max_num) {

library/strings/suffix_array/suffix_array_short.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! auto [sa1, sa_inv1, lcp1] = sa_short(s_vec);
99
//! @endcode
1010
//! about 2-3x slower than KACTL
11+
//! requires n >= 1 (lcp size n-1 underflows when n == 0)
1112
//! @time O(n * log^2(n))
1213
//! @space O(n)
1314
auto sa_short(const auto& s) {

0 commit comments

Comments
 (0)