Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ vi min_plus(const vi& convex, const vi& arbitrary) {
int n = sz(convex);
int m = sz(arbitrary);
vi res(n + m - 1, INT_MAX);
auto dnc = [&](auto&& self, int res_le, int res_ri,
auto dnc = [&](auto&& dnc, int res_le, int res_ri,
int arb_le, int arb_ri) -> void {
if (res_le >= res_ri) return;
int mid_res = (res_le + res_ri) / 2;
Expand All @@ -22,9 +22,9 @@ vi min_plus(const vi& convex, const vi& arbitrary) {
op_arb = i;
}
}
self(self, res_le, mid_res, arb_le,
dnc(dnc, res_le, mid_res, arb_le,
min(arb_ri, op_arb + 1));
self(self, mid_res + 1, res_ri, op_arb, arb_ri);
dnc(dnc, mid_res + 1, res_ri, op_arb, arb_ri);
};
dnc(dnc, 0, n + m - 1, 0, m);
return res;
Expand Down
4 changes: 2 additions & 2 deletions library/graphs/bcc_callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
void bcc(const auto& adj, auto f) {
int n = sz(adj), q = 0, s = 0;
vi t(n), st(n);
auto dfs = [&](auto&& self, int u) -> int {
auto dfs = [&](auto&& dfs, int u) -> int {
int l = t[u] = ++q;
for (int v : adj[u]) {
int siz = s, lu = 0;
l = min(l, t[v] ?: (lu = self(self, st[s++] = v)));
l = min(l, t[v] ?: (lu = dfs(dfs, st[s++] = v)));
if (lu >= t[u]) {
st[s++] = u;
f({siz + all(st) - n + s});
Expand Down
4 changes: 2 additions & 2 deletions library/graphs/euler_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
vector<pii> euler_path(auto& adj, int m, int s) {
vi vis(m);
vector<pii> path;
auto dfs = [&](auto&& self, int u, int eu) -> void {
auto dfs = [&](auto&& dfs, int u, int eu) -> void {
while (!empty(adj[u])) {
auto [v, ev] = adj[u].back();
adj[u].pop_back();
if (!vis[ev]) vis[ev] = 1, self(self, v, ev);
if (!vis[ev]) vis[ev] = 1, dfs(dfs, v, ev);
}
path.emplace_back(u, eu);
};
Expand Down
4 changes: 2 additions & 2 deletions library/graphs/hopcroft_karp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ struct hopcroft_karp {
}
}
if (!found) break;
auto dfs = [&](auto&& self, int u) -> bool {
auto dfs = [&](auto&& dfs, int u) -> bool {
for (int v : adj[u]) {
int w = to_l[v];
if (w == -1 ||
(level[u] + 1 == level[w] && self(self, w))) {
(level[u] + 1 == level[w] && dfs(dfs, w))) {
to_r[u] = v;
to_l[v] = u;
return 1;
Expand Down
4 changes: 2 additions & 2 deletions library/graphs/scc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
auto scc(const auto& adj) {
int n = sz(adj), num_sccs = 0, q = 0, s = 0;
vi scc_id(n, -1), tin(n), st(n);
auto dfs = [&](auto&& self, int u) -> int {
auto dfs = [&](auto&& dfs, int u) -> int {
int low = tin[u] = ++q;
st[s++] = u;
for (int v : adj[u])
if (scc_id[v] < 0)
low = min(low, tin[v] ?: self(self, v));
low = min(low, tin[v] ?: dfs(dfs, v));
if (tin[u] == low) {
while (scc_id[u] < 0) scc_id[st[--s]] = num_sccs;
num_sccs++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ vector<pii> extra_edges(const auto& adj, int num_sccs,
zero_in[scc_id[v]] = 0;
}
vector<bool> vis(num_sccs);
auto dfs = [&](auto&& self, int u) {
auto dfs = [&](auto&& dfs, int u) {
if (empty(scc_adj[u])) return u;
for (int v : scc_adj[u])
if (!vis[v]) {
vis[v] = 1;
int zero_out = self(self, v);
int zero_out = dfs(dfs, v);
if (zero_out != -1) return zero_out;
}
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ vi offline_incremental_scc(vector<array<int, 2>> eds,
vi ids(n, -1), joins(m, m), idx(m), vs(n), scc_id;
iota(all(idx), 0);
vector<vi> adj;
auto divide_and_conquer = [&](auto&& self, auto el,
auto er, int tl, int tr) {
auto dnc = [&](auto&& dnc, auto el, auto er, int tl,
int tr) {
adj.clear();
int mid = midpoint(tl, tr);
for (auto it = el; it != er; it++) {
Expand All @@ -42,11 +42,11 @@ vi offline_incremental_scc(vector<array<int, 2>> eds,
auto& [u, v] = eds[*it];
u = scc_id[u], v = scc_id[v];
}
self(self, el, split, tl, mid);
self(self, split, er, mid, tr);
dnc(dnc, el, split, tl, mid);
dnc(dnc, split, er, mid, tr);
};
// uses -1 as the lower bound to correctly handle
// self-edges
divide_and_conquer(divide_and_conquer, all(idx), -1, m);
dnc(dnc, all(idx), -1, m);
return joins;
}
4 changes: 2 additions & 2 deletions library/graphs/uncommon/bridges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
auto bridges(const auto& adj, int m) {
int n = sz(adj), num_ccs = 0, q = 0, s = 0;
vi br_id(n, -1), is_br(m), tin(n), st(n);
auto dfs = [&](auto&& self, int u, int p) -> int {
auto dfs = [&](auto&& dfs, int u, int p) -> int {
int low = tin[u] = ++q;
st[s++] = u;
for (auto [v, e] : adj[u])
if (e != p && br_id[v] < 0)
low = min(low, tin[v] ?: self(self, v, e));
low = min(low, tin[v] ?: dfs(dfs, v, e));
if (tin[u] == low) {
if (p != -1) is_br[p] = 1;
while (br_id[u] < 0) br_id[st[--s]] = num_ccs;
Expand Down
4 changes: 2 additions & 2 deletions library/graphs/uncommon/cuts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
auto cuts(const auto& adj, int m) {
int n = sz(adj), num_bccs = 0, q = 0, s = 0;
vi bcc_id(m, -1), is_cut(n), tin(n), st(m);
auto dfs = [&](auto&& self, int u, int p) -> int {
auto dfs = [&](auto&& dfs, int u, int p) -> int {
int low = tin[u] = ++q;
for (auto [v, e] : adj[u]) {
assert(u != v);
if (e == p) continue;
if (tin[v] < tin[u]) st[s++] = e;
int lu = -1;
low = min(low, tin[v] ?: (lu = self(self, v, e)));
low = min(low, tin[v] ?: (lu = dfs(dfs, v, e)));
if (lu >= tin[u]) {
is_cut[u] = p >= 0 || tin[u] + 1 < tin[v];
while (bcc_id[e] < 0) bcc_id[st[--s]] = num_bccs;
Expand Down
10 changes: 5 additions & 5 deletions library/trees/centroid_decomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//! @space O(n)
void centroid(auto& adj, auto f) {
vi siz(sz(adj));
auto calc_sz = [&](auto&& self, int u, int p) -> void {
auto dfs_sz = [&](auto&& dfs_sz, int u, int p) -> void {
siz[u] = 1;
for (int v : adj[u])
if (v != p) self(self, v, u), siz[u] += siz[v];
if (v != p) dfs_sz(dfs_sz, v, u), siz[u] += siz[v];
};
auto dfs = [&](auto&& self, int u, int p) -> void {
calc_sz(calc_sz, u, -1);
auto dfs = [&](auto&& dfs, int u, int p) -> void {
dfs_sz(dfs_sz, u, -1);
for (int w = -1, sz_root = siz[u];;) {
auto big_ch = ranges::find_if(adj[u], [&](int v) {
return v != w && 2 * siz[v] > sz_root;
Expand All @@ -25,7 +25,7 @@ void centroid(auto& adj, auto f) {
for (int v : adj[u]) {
iter_swap(ranges::find(adj[v], u), rbegin(adj[v]));
adj[v].pop_back();
self(self, v, u);
dfs(dfs, v, u);
}
};
dfs(dfs, 0, -1);
Expand Down
16 changes: 8 additions & 8 deletions library/trees/edge_cd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
//! @space O(n)
template<class G> void edge_cd(vector<G>& adj, auto f) {
vi siz(sz(adj));
auto find_cent = [&](auto&& self, int u, int p,
int m) -> int {
auto cent = [&](auto&& cent, int u, int p,
int m) -> int {
siz[u] = 1;
for (int v : adj[u])
if (v != p) {
int cent = self(self, v, u, m);
if (cent != -1) return cent;
int c = cent(cent, v, u, m);
if (c != -1) return c;
siz[u] += siz[v];
}
return 2 * siz[u] > m
? p >= 0 && (siz[p] = m + 1 - siz[u]),
u : -1;
};
auto dfs = [&](auto&& self, int u, int m) -> void {
auto dfs = [&](auto&& dfs, int u, int m) -> void {
if (m < 2) return;
u = find_cent(find_cent, u, -1, m);
u = cent(cent, u, -1, m);
int sum = 0;
auto it = partition(all(adj[u]), [&](int v) {
ll x = sum + siz[v];
Expand All @@ -39,9 +39,9 @@ template<class G> void edge_cd(vector<G>& adj, auto f) {
f(u, it - begin(adj[u]));
G oth(it, end(adj[u]));
adj[u].erase(it, end(adj[u]));
self(self, u, sum);
dfs(dfs, u, sum);
swap(adj[u], oth);
self(self, u, m - sum);
dfs(dfs, u, m - sum);
};
dfs(dfs, 0, sz(adj) - 1);
};
8 changes: 4 additions & 4 deletions library/trees/hld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ template<bool VALS_EDGES> struct HLD {
vi p, siz, rt, tin;
HLD(auto& adj):
n(sz(adj)), p(n), siz(n, 1), rt(n), tin(n) {
auto dfs1 = [&](auto&& self, int u) -> void {
auto dfs1 = [&](auto&& dfs1, int u) -> void {
for (int& v : adj[u]) {
iter_swap(ranges::find(adj[v], u), rbegin(adj[v]));
adj[v].pop_back();
p[v] = u;
self(self, v);
dfs1(dfs1, v);
siz[u] += siz[v];
if (siz[v] > siz[adj[u][0]]) swap(v, adj[u][0]);
}
};
dfs1(dfs1, 0);
int tim = 0;
auto dfs2 = [&](auto&& self, int u) -> void {
auto dfs2 = [&](auto&& dfs2, int u) -> void {
tin[u] = tim++;
for (int v : adj[u]) {
rt[v] = (v == adj[u][0] ? rt[u] : v);
self(self, v);
dfs2(dfs2, v);
}
};
dfs2(dfs2, 0);
Expand Down
4 changes: 2 additions & 2 deletions library/trees/lca_rmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ struct LCA {
LCA(const auto& adj):
n(sz(adj)), tin(n), siz(n, 1), d(n), p(n) {
vi order;
auto dfs = [&](auto&& self, int u) -> void {
auto dfs = [&](auto&& dfs, int u) -> void {
tin[u] = sz(order), order.push_back(u);
for (int v : adj[u])
if (v != p[u])
d[v] = d[p[v] = u] + 1, self(self, v),
d[v] = d[p[v] = u] + 1, dfs(dfs, v),
siz[u] += siz[v];
};
dfs(dfs, 0);
Expand Down
4 changes: 2 additions & 2 deletions library/trees/linear_lca.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ struct linear_lca {
linear_lca(const auto& adj):
n(sz(adj)), d(n), in(n), asc(n), head(n + 1) {
vector<pii> order;
auto dfs = [&](auto&& self, int u, int p) -> void {
auto dfs = [&](auto&& dfs, int u, int p) -> void {
order.emplace_back(u, p);
in[u] = sz(order);
for (int v : adj[u])
if (v != p) {
d[v] = 1 + d[u];
self(self, v, u);
dfs(dfs, v, u);
head[in[v]] = u;
if (lsb(in[u]) < lsb(in[v])) in[u] = in[v];
}
Expand Down
4 changes: 2 additions & 2 deletions library/trees/shallowest_decomp_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
//! @space O(n)
void shallowest(auto& adj, auto f) {
vector<vi> order(bit_width(size(adj)));
auto dfs = [&](auto&& self, int u, int p) -> int {
auto dfs = [&](auto&& dfs, int u, int p) -> int {
int once = 0, twice = 0;
for (int v : adj[u])
if (v != p) {
int dp = self(self, v, u);
int dp = dfs(dfs, v, u);
twice |= once & dp, once |= dp;
}
auto dp = (once | (bit_ceil(twice + 1u) - 1)) + 1;
Expand Down
4 changes: 2 additions & 2 deletions library/trees/tree_lift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
struct tree_lift {
vi d, p, j;
tree_lift(const auto& adj): d(sz(adj)), p(d), j(d) {
auto dfs = [&](auto&& self, int u) -> void {
auto dfs = [&](auto&& dfs, int u) -> void {
int up =
d[u] + d[j[j[u]]] == 2 * d[j[u]] ? j[j[u]] : u;
for (int v : adj[u])
if (v != p[u])
d[v] = d[p[v] = u] + 1, j[v] = up, self(self, v);
d[v] = d[p[v] = u] + 1, j[v] = up, dfs(dfs, v);
};
dfs(dfs, 0);
}
Expand Down
4 changes: 2 additions & 2 deletions library/trees/uncommon/ladder_decomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ struct ladder {
//! @space O(n log n) for jmp. Everything else is O(n)
ladder(const auto& adj):
n(sz(adj)), d(n), p(n), leaf(n), idx(n), lad(2 * n) {
auto dfs = [&](auto&& self, int u) -> void {
auto dfs = [&](auto&& dfs, int u) -> void {
leaf[u] = u;
for (int v : adj[u])
if (v != p[u]) {
d[v] = d[p[v] = u] + 1;
self(self, v);
dfs(dfs, v);
if (d[leaf[u]] < d[leaf[v]]) leaf[u] = leaf[v];
}
};
Expand Down
4 changes: 2 additions & 2 deletions library/trees/uncommon/linear_kth_par.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ template<int KAPPA = 2> struct linear_kth_par {
jmp[t] = st[max(0, s - KAPPA * (t & -t))];
t++;
};
auto dfs = [&](auto&& self, int u, int p) -> void {
auto dfs = [&](auto&& dfs, int u, int p) -> void {
int& l = leaf[u] = st[d[u]] = u;
pos[u] = t;
calc(d[u]);
for (int v : adj[u])
if (v != p) {
d[v] = 1 + d[u];
self(self, v, u);
dfs(dfs, v, u);
if (d[l] < d[leaf[v]]) l = leaf[v];
calc(d[u]);
}
Expand Down
4 changes: 2 additions & 2 deletions library/trees/uncommon/subtree_isomorphism.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
auto subtree_iso(const auto& adj) {
vi iso_id(sz(adj), -1);
map<vi, int> hashes;
auto dfs = [&](auto&& self, int u, int p) -> int {
auto dfs = [&](auto&& dfs, int u, int p) -> int {
vi ch_ids;
for (int v : adj[u])
if (v != p) ch_ids.push_back(self(self, v, u));
if (v != p) ch_ids.push_back(dfs(dfs, v, u));
ranges::sort(ch_ids);
return iso_id[u] =
hashes.try_emplace(ch_ids, sz(hashes))
Expand Down
Loading