From f4d4687cdb524f3cc771bfbffb8e9cd70f4508a9 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 19 Aug 2024 15:37:22 -0300 Subject: [PATCH] feat(graphs): add binary lifting --- algorithms/graphs/binary-lifting.cpp | 14 ++++++++++++++ algorithms/graphs/binary-lifting.tex | 3 +++ 2 files changed, 17 insertions(+) create mode 100644 algorithms/graphs/binary-lifting.cpp create mode 100644 algorithms/graphs/binary-lifting.tex diff --git a/algorithms/graphs/binary-lifting.cpp b/algorithms/graphs/binary-lifting.cpp new file mode 100644 index 0000000..1a2c767 --- /dev/null +++ b/algorithms/graphs/binary-lifting.cpp @@ -0,0 +1,14 @@ +const int MAXN = 2e5, MAXLOG2 = 60; +int bl[MAXN][MAXLOG2 + 1]; + +int jump(int u, ll k) { + for (int i = 0; i <= MAXLOG2; i++) + if (k & (1LL << i)) u = bl[u][i]; + + return u; +} + +void build(int N) { + for (int i = 1; i <= MAXLOG2; i++) + for (int j = 0; j < N; j++) bl[j][i] = bl[bl[j][i - 1]][i - 1]; +} diff --git a/algorithms/graphs/binary-lifting.tex b/algorithms/graphs/binary-lifting.tex new file mode 100644 index 0000000..3179e27 --- /dev/null +++ b/algorithms/graphs/binary-lifting.tex @@ -0,0 +1,3 @@ +\subsection{Binary Lifting} + +Time: $O(N \cdot \log_2 K)$