summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 18:09:36 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 18:09:36 +0100
commitd52b0aee1e0d32c4c6f277e8291cedad2b4f1302 (patch)
tree03d01cb60694bf6723a7400fffb2eff93d9d2039
parente55df069a8f83b2c0c2b56c035f49e89516cdaaa (diff)
add binary lifting test
-rw-r--r--content/graph/binary_lifting.cpp6
-rw-r--r--test/graph/binary_lifting.cpp60
2 files changed, 63 insertions, 3 deletions
diff --git a/content/graph/binary_lifting.cpp b/content/graph/binary_lifting.cpp
index 0b8c218..f88b1a9 100644
--- a/content/graph/binary_lifting.cpp
+++ b/content/graph/binary_lifting.cpp
@@ -3,13 +3,13 @@ struct Lift {
Lift(vector<vector<int>> &adj, int root):
dep(adj.size()), par(adj.size()), jmp(adj.size(), root) {
- function<void(int,int,int)> dfs = [&](int u, int p, int d) {
+ auto dfs = [&](auto &self, int u, int p, int d) -> void {
dep[u] = d, par[u] = p;
jmp[u] = dep[p] + dep[jmp[jmp[p]]] == 2*dep[jmp[p]]
? jmp[jmp[p]] : p;
- for (int v: adj[u]) if (v != p) dfs(v, u, d+1);
+ for (int v: adj[u]) if (v != p) self(self, v, u, d+1);
};
- dfs(root, root, 0);
+ dfs(dfs, root, root, 0);
}
int depth(int v) { return dep[v]; }
diff --git a/test/graph/binary_lifting.cpp b/test/graph/binary_lifting.cpp
new file mode 100644
index 0000000..20318da
--- /dev/null
+++ b/test/graph/binary_lifting.cpp
@@ -0,0 +1,60 @@
+#include "../util.h"
+#include <graph/binary_lifting.cpp>
+namespace expected {
+#include <graph/hld.cpp>
+}
+
+void stress_test() {
+ ll queries = 0;
+ for (int tries = 0; tries < 200'000; tries++) {
+ int n = Random::integer<int>(2, 30);
+ Graph<NoData> g(n);
+ g.tree();
+
+ vector<vector<int>> adj(n);
+ g.forEdges([&](int a, int b){
+ adj[a].push_back(b);
+ adj[b].push_back(a);
+ });
+
+ Lift lift(adj, 0);
+
+ expected::adj = adj;
+ expected::init();
+
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j <= i; j++) {
+ auto got = lift.lca(i, j);
+ auto expected = expected::get_lca(i, j);
+ if (got != expected) cerr << "got: " << got << ", expected: " << expected << FAIL;
+ }
+ }
+ queries += n;
+ }
+ cerr << "tested random queries: " << queries << endl;
+}
+
+constexpr int N = 1'000'000;
+void performance_test() {
+ timer t;
+ Graph<NoData> g(N);
+ g.tree();
+ vector<vector<int>> adj(N);
+ g.forEdges([&](int a, int b){
+ adj[a].push_back(b);
+ adj[b].push_back(a);
+ });
+
+ hash_t hash = 0;
+ t.start();
+ Lift lift(adj, 0);
+ for (int i = 1; i < N; i++) hash += lift.lca(i-1, i);
+ t.stop();
+ if (t.time > 1000) cerr << "too slow: " << t.time << FAIL;
+ cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
+}
+
+int main() {
+ stress_test();
+ performance_test();
+}