Skip to content

Commit

Permalink
✨ adds successor graph with binary lifting
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Dec 10, 2023
1 parent f2d2618 commit 7a52bf4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions algorithms/graphs/successor_graph-(struct)_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const int MAXLG2 = 31;
struct FunctionalGraph {
int n;
vi g;
vi2d bl;

FunctionalGraph(int _n)
: n(_n), g(n), bl(n, vi(MAXLG2)){};

FunctionalGraph(const vi &_g)
: n(len(_g)), g(_g), bl(n, vi(MAXLG2)){};

void build() {
for (int i = 0; i < n; i++) {
bl[i][0] = g[i];
}
for (int k = 1; k < MAXLG2; k++) {
for (int i = 0; i < n; i++) {
bl[i][k] = bl[bl[i][k - 1]][k - 1];
}
}
}

int kth_pos(int u, ll k) const {
for (ll i = 0; i < MAXLG2; i++)
if (k & (1 << i)) u = bl[u][i];

return u;
}
};
1 change: 1 addition & 0 deletions algorithms/graphs/successor_graph-(struct)_2.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\subsection{Succesor Graph (Struct Binary-Lifting)}

0 comments on commit 7a52bf4

Please sign in to comment.