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
6 changes: 3 additions & 3 deletions src/1-ds/ds_segtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ struct seg_2d { // 1-indexed

// what: 2D segment tree with coordinate compression for sparse updates/queries.
// time: prep O(q log q), update/query O(log^2 n); memory: O(q log q)
// constraint: x is 1-indexed [1..n]; y is coordinate value; call mark_set/mark_qry first, then prep, then set/query.
// usage: seg2d_comp st(n); st.mark_set(x, y); st.mark_qry(x1, x2, y1, y2); st.prep(); st.set(x, y, v); st.query(x1, x2, y1, y2);
// constraint: x is 1-indexed [1..n]; y is coordinate value; call mark_set/mark_query first, then prep, then set/query.
// usage: seg2d_comp st(n); st.mark_set(x, y); st.mark_query(x1, x2, y1, y2); st.prep(); st.set(x, y, v); st.query(x1, x2, y1, y2);
struct seg2d_comp { // x: 1-indexed
int n;
vector<vector<ll>> a;
Expand All @@ -247,7 +247,7 @@ struct seg2d_comp { // x: 1-indexed
// goal: record y-coordinates that will be updated.
for (x += n - 1; x >= 1; x >>= 1) used[x].push_back(y);
}
void mark_qry(int x1, int x2, int y1, int y2) {
void mark_query(int x1, int x2, int y1, int y2) {
// goal: record y-coordinates needed for queries.
for (x1 += n - 1, x2 += n; x1 < x2; x1 >>= 1, x2 >>= 1) {
if (x1 & 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/2-graph/cc_bcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: find biconnected components and articulation points/bridges in an undirected graph.
// time: O(n+m); memory: O(n+m)
// constraint: 1-indexed; no self-loops; recursion depth O(n).
// usage: bcc_graph g; g.init(n); g.add(u,v); g.run(); // g.bccs, g.ap, g.ae
// usage: bcc_graph g; g.init(n); g.add_edge(u,v); g.run(); // g.bccs, g.ap, g.ae
struct bcc_graph {
int n, tim;
vector<vector<pii>> adj;
Expand All @@ -23,7 +23,7 @@ struct bcc_graph {
ed.clear();
bccs.clear();
}
void add(int u, int v) {
void add_edge(int u, int v) {
int id = sz(ed);
ed.push_back({u, v});
adj[u].push_back({v, id});
Expand Down
8 changes: 4 additions & 4 deletions src/2-graph/cc_scc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: compute SCCs in a directed graph using Kosaraju's algorithm.
// time: O(n+m); memory: O(n+m)
// constraint: directed; 1-indexed; recursion depth O(n).
// usage: scc_kosa s; s.init(n); s.add(u,v); int c=s.run();
// usage: scc_kosa s; s.init(n); s.add_edge(u,v); int c=s.run();
struct scc_kosa {
int n;
vector<vector<int>> g, rg, sccs;
Expand All @@ -18,7 +18,7 @@ struct scc_kosa {
comp.assign(n + 1, -1);
ord.clear();
}
void add(int u, int v) {
void add_edge(int u, int v) {
g[u].push_back(v);
rg[v].push_back(u);
}
Expand Down Expand Up @@ -50,7 +50,7 @@ struct scc_kosa {
// what: compute SCCs in a directed graph using Tarjan's algorithm.
// time: O(n+m); memory: O(n+m)
// constraint: directed; 1-indexed; recursion depth O(n).
// usage: scc_tarjan s; s.init(n); s.add(u,v); int c=s.run();
// usage: scc_tarjan s; s.init(n); s.add_edge(u,v); int c=s.run();
struct scc_tarjan {
int n, tim;
vector<vector<int>> g, sccs;
Expand All @@ -67,7 +67,7 @@ struct scc_tarjan {
ins.assign(n + 1, 0);
st.clear();
}
void add(int u, int v) { g[u].push_back(v); }
void add_edge(int u, int v) { g[u].push_back(v); }
void dfs(int v) {
dfn[v] = low[v] = ++tim;
st.push_back(v);
Expand Down
4 changes: 2 additions & 2 deletions src/2-graph/euler_circ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: build an Eulerian circuit in an undirected multigraph (adjacency matrix).
// time: O(n^2+m); memory: O(n^2)
// constraint: 1-indexed; all nonzero-degree nodes connected.
// usage: euler_cir g; g.init(n); g.add(u,v); if (g.can()) auto path=g.run(1);
// usage: euler_cir g; g.init(n); g.add_edge(u,v); if (g.can()) auto path=g.run(1);
struct euler_cir {
int n;
vector<vector<int>> adj;
Expand All @@ -16,7 +16,7 @@ struct euler_cir {
nxt.assign(n + 1, 1);
path.clear();
}
void add(int u, int v, int c = 1) {
void add_edge(int u, int v, int c = 1) {
// goal: add c parallel edges between u and v.
if (u == v) adj[u][u] += 2 * c;
else adj[u][v] += c, adj[v][u] += c;
Expand Down
12 changes: 6 additions & 6 deletions src/2-graph/sp_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: compute single-source shortest paths with non-negative edges (Dijkstra).
// time: O((n+m)log n); memory: O(n+m)
// constraint: directed; 1-indexed; w >= 0.
// usage: dijkstra g; g.init(n); g.add(u,v,w); auto dist=g.run(s);
// usage: dijkstra g; g.init(n); g.add_edge(u,v,w); auto dist=g.run(s);
struct dijkstra {
static const ll INF = (1LL << 62);
int n;
Expand All @@ -14,7 +14,7 @@ struct dijkstra {
n = n_;
adj.assign(n + 1, {});
}
void add(int u, int v, ll w) { adj[u].push_back({w, v}); }
void add_edge(int u, int v, ll w) { adj[u].push_back({w, v}); }
vector<ll> run(int s) {
// result: dist[i] = shortest distance from s to i.
vector<ll> dist(n + 1, INF);
Expand All @@ -39,7 +39,7 @@ struct dijkstra {
// what: compute single-source shortest paths with possible negative edges.
// time: O(nm); memory: O(n+m)
// constraint: directed; 1-indexed; detects negative cycle reachable from s.
// usage: bell_ford g; g.init(n); g.add(u,v,w); bool ok=g.run(s, dist);
// usage: bell_ford g; g.init(n); g.add_edge(u,v,w); bool ok=g.run(s, dist);
struct bell_ford {
static const ll INF = (1LL << 62);
int n;
Expand All @@ -50,7 +50,7 @@ struct bell_ford {
n = n_;
ed.clear();
}
void add(int u, int v, ll w) { ed.push_back({u, v, w}); }
void add_edge(int u, int v, ll w) { ed.push_back({u, v, w}); }
bool run(int s, vector<ll> &dist) {
// result: false if a negative cycle is reachable.
dist.assign(n + 1, INF);
Expand All @@ -74,7 +74,7 @@ struct bell_ford {
// what: compute all-pairs shortest paths with dynamic programming.
// time: O(n^3); memory: O(n^2)
// constraint: directed; 1-indexed; watch overflow on INF.
// usage: floyd g; g.init(n); g.add(u,v,w); g.run(); auto &d=g.d;
// usage: floyd g; g.init(n); g.add_edge(u,v,w); g.run(); auto &d=g.d;
struct floyd {
static const ll INF = (1LL << 62);
int n;
Expand All @@ -86,7 +86,7 @@ struct floyd {
d.assign(n + 1, vector<ll>(n + 1, INF));
for (int i = 1; i <= n; i++) d[i][i] = 0;
}
void add(int u, int v, ll w) { d[u][v] = min(d[u][v], w); }
void add_edge(int u, int v, ll w) { d[u][v] = min(d[u][v], w); }
void run() {
// goal: relax all pairs via intermediate nodes.
for (int k = 1; k <= n; k++) {
Expand Down
4 changes: 2 additions & 2 deletions src/2-graph/sp_kth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: enumerate k-th shortest walk from s to t with non-negative weights (Eppstein-style).
// time: O((n+m)log m + klog k); memory: O(n+m+heap)
// constraint: 1-indexed; w >= 0; n <= MAXN-1; recursion depth O(log m).
// usage: kth_walk g; g.init(n); g.add(u,v,w); auto v=g.run(s,e,k);
// usage: kth_walk g; g.init(n); g.add_edge(u,v,w); auto v=g.run(s,e,k);
struct kth_walk {
static const int MAXN = 303030;
static const ll INF = (ll)1e18;
Expand Down Expand Up @@ -42,7 +42,7 @@ struct kth_walk {
for (int i = 1; i <= n; i++) g[i].clear(), rg[i].clear();
hp.init();
}
void add(int u, int v, ll w) {
void add_edge(int u, int v, ll w) {
g[u].push_back({w, v});
rg[v].push_back({w, u});
}
Expand Down
4 changes: 2 additions & 2 deletions src/3-tree/lca_sparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: LCA via binary lifting for rooted tree.
// time: build O(n log n), query O(log n); memory: O(n log n)
// constraint: 1-indexed tree.
// usage: lca_sparse l; l.init(n); l.add(u,v); l.build(1); int w=l.lca(u,v);
// usage: lca_sparse l; l.init(n); l.add_edge(u,v); l.build(1); int w=l.lca(u,v);
struct lca_sparse {
int n, lg;
vector<vector<int>> adj, up;
Expand All @@ -17,7 +17,7 @@ struct lca_sparse {
up.assign(lg, vector<int>(n + 1, 0));
dep.assign(n + 1, 0);
}
void add(int u, int v) {
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
Expand Down
4 changes: 2 additions & 2 deletions src/3-tree/tree_centroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: centroid decomposition for tree, builds centroid parent/children for path queries.
// time: O(n log n); memory: O(n)
// constraint: 1-indexed tree.
// usage: cen_decomp cd; cd.init(n); cd.add(u,v); cd.build(); int p=cd.par[v];
// usage: cen_decomp cd; cd.init(n); cd.add_edge(u,v); cd.build(); int p=cd.par[v];
struct cen_decomp {
int n;
vector<vector<int>> adj, chd;
Expand All @@ -18,7 +18,7 @@ struct cen_decomp {
siz.assign(n + 1, 0);
used.assign(n + 1, 0);
}
void add(int u, int v) {
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
Expand Down
6 changes: 3 additions & 3 deletions src/3-tree/tree_hld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: heavy-light decomposition for path sum on tree (node values).
// time: build O(n), update/query O(log^2 n); memory: O(n)
// constraint: 1-indexed tree.
// usage: hld_tree h; h.init(n); h.add(u,v); h.build(1); h.set(v,x); ll s=h.qry(u,v);
// usage: hld_tree h; h.init(n); h.add_edge(u,v); h.build(1); h.set(v,x); ll s=h.query(u,v);
struct hld_tree {
seg_tree seg;

Expand All @@ -22,7 +22,7 @@ struct hld_tree {
top.assign(n + 1, 0);
in.assign(n + 1, 0);
}
void add(int u, int v) {
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
Expand Down Expand Up @@ -55,7 +55,7 @@ struct hld_tree {
seg.build(a);
}
void set(int v, ll val) { seg.set(in[v], val); }
ll qry(int a, int b) const {
ll query(int a, int b) const {
ll ret = 0;
while (top[a] != top[b]) {
if (dep[top[a]] < dep[top[b]]) swap(a, b);
Expand Down
4 changes: 2 additions & 2 deletions src/3-tree/tree_vtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: virtual tree builder for subset DP using LCA and dfs order.
// time: build O(n log n), make O(k log k); memory: O(n log n)
// constraint: 1-indexed tree.
// usage: tree_comp tc; tc.init(n); tc.add(u,v); tc.build(root); auto nodes=tc.make(vs); // use tc.vt_adj
// usage: tree_comp tc; tc.init(n); tc.add_edge(u,v); tc.build(root); auto nodes=tc.make(vs); // use tc.vt_adj
struct tree_comp {
int n, lg, tim;
vector<vector<int>> adj, up, vt_adj;
Expand All @@ -21,7 +21,7 @@ struct tree_comp {
dep.assign(n + 1, 0);
tim = 0;
}
void add(int u, int v) {
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
Expand Down
4 changes: 2 additions & 2 deletions src/3-tree/tree_xchg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// what: maximize sum w[i]*completion_time[i] under precedence "parent before child" on rooted tree.
// time: O(n log n); memory: O(n)
// constraint: 1-indexed tree; cw[i]=w[i]*t[i].
// usage: tree_xchg tx; tx.init(n, root); tx.add(u,v); // set tx.ds.w/t/cw; ll ans=tx.run();
// usage: tree_xchg tx; tx.init(n, root); tx.add_edge(u,v); // set tx.ds.w/t/cw; ll ans=tx.run();
struct tree_xchg {
struct uf {
vector<int> par;
Expand Down Expand Up @@ -42,7 +42,7 @@ struct tree_xchg {
par.assign(n + 1, 0);
ds.init(n);
}
void add(int u, int v) {
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/1-ds/test_ds_segtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void test_seg2dc_basic() {

for (auto &op : ops) {
if (op.type == 0) st.mark_set(op.x1, op.y1);
else st.mark_qry(op.x1, op.x2, op.y1, op.y2);
else st.mark_query(op.x1, op.x2, op.y1, op.y2);
}
st.prep();

Expand Down Expand Up @@ -294,7 +294,7 @@ void test_seg2dc_random() {

for (auto &op : ops) {
if (op.type == 0) st.mark_set(op.x1, op.y1);
else st.mark_qry(op.x1, op.x2, op.y1, op.y2);
else st.mark_query(op.x1, op.x2, op.y1, op.y2);
}
st.prep();

Expand Down
4 changes: 2 additions & 2 deletions tests/2-graph/test_cc_bcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void t_fix() {
vector<pii> ed = {{1, 2}, {2, 3}};
bcc_graph g;
g.init(n);
for (auto [u, v] : ed) g.add(u, v);
for (auto [u, v] : ed) g.add_edge(u, v);
g.run();
auto ap = g.ap;
auto ae = g.ae;
Expand All @@ -135,7 +135,7 @@ void t_rnd() {
}
bcc_graph g;
g.init(n);
for (auto [u, v] : ed) g.add(u, v);
for (auto [u, v] : ed) g.add_edge(u, v);
g.run();
auto ap = g.ap;
auto ae = g.ae;
Expand Down
4 changes: 2 additions & 2 deletions tests/2-graph/test_cc_scc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ void t_scc() {
scc_kosa s1;
s1.init(n);
for (int u = 1; u <= n; u++)
for (int v : g[u]) s1.add(u, v);
for (int v : g[u]) s1.add_edge(u, v);
s1.run();

scc_tarjan s2;
s2.init(n);
for (int u = 1; u <= n; u++)
for (int v : g[u]) s2.add(u, v);
for (int v : g[u]) s2.add_edge(u, v);
s2.run();

for (int i = 1; i <= n; i++)
Expand Down
2 changes: 1 addition & 1 deletion tests/2-graph/test_euler_circ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool can_na(int n, const vector<vector<int>> &cnt) {
}

void add_ed(euler_cir &g, vector<vector<int>> &cnt, int u, int v) {
g.add(u, v);
g.add_edge(u, v);
if (u == v) cnt[u][u]++;
else cnt[u][v]++, cnt[v][u]++;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/2-graph/test_sp_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void t_dijk() {
int u = rnd(1, n), v = rnd(1, n);
ll w = rnd(0, 9);
ed.push_back({u, v, w});
dj.add(u, v, w);
dj.add_edge(u, v, w);
}
auto d = floy(n, ed);
int s = rnd(1, n);
Expand All @@ -63,7 +63,7 @@ void t_bell() {
int u = rnd(1, n), v = rnd(1, n);
ll w = rnd(-5, 9);
ed.push_back({u, v, w});
bl.add(u, v, w);
bl.add_edge(u, v, w);
}
auto d = floy(n, ed);
int s = rnd(1, n);
Expand Down Expand Up @@ -98,7 +98,7 @@ void t_floy() {
int u = rnd(1, n), v = rnd(1, n);
ll w = rnd(-5, 9);
ed.push_back({u, v, w});
fl.add(u, v, w);
fl.add_edge(u, v, w);
}
auto d = floy(n, ed);
bool neg = 0;
Expand Down
6 changes: 3 additions & 3 deletions tests/2-graph/test_sp_kth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void t_fix() {
g[1].push_back({1, 2});
g[2].push_back({1, 3});
ks.init(n);
ks.add(1, 2, 1);
ks.add(2, 3, 1);
ks.add_edge(1, 2, 1);
ks.add_edge(2, 3, 1);
auto a = kth_na(n, g, 1, 3, 3);
auto b = ks.run(1, 3, 3);
assert(a == b);
Expand All @@ -53,7 +53,7 @@ void t_rnd() {
int u = rnd(1, n), v = rnd(1, n);
ll w = rnd(1, 5);
g[u].push_back({w, v});
ks.add(u, v, w);
ks.add_edge(u, v, w);
}
int s = rnd(1, n), e = rnd(1, n), k = rnd(1, 10);
auto a = kth_na(n, g, s, e, k);
Expand Down
2 changes: 1 addition & 1 deletion tests/3-tree/test_lca_sparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void t_rnd() {
}
lca_sparse l;
l.init(n);
for (int v = 2; v <= n; v++) l.add(v, par[v]);
for (int v = 2; v <= n; v++) l.add_edge(v, par[v]);
l.build(1);
for (int q = 0; q < 50; q++) {
int a = rnd(1, n), b = rnd(1, n);
Expand Down
2 changes: 1 addition & 1 deletion tests/3-tree/test_tree_centroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void check_one(const vector<vector<int>> &adj) {
cd.init(n);
for (int u = 1; u <= n; u++)
for (int v : adj[u])
if (u < v) cd.add(u, v);
if (u < v) cd.add_edge(u, v);
cd.build(1);

int root = get_root(n, cd.par);
Expand Down
Loading