Skip to content

Commit

Permalink
Removed trav (and some other test/CI fixes) (kth-competitive-programm…
Browse files Browse the repository at this point in the history
…ing#173)

* checkpoint

* another checkpoint

* final checkpoint

* converted tests and fixed some tests

* fix some tests

* updated descriptions

* responded to comments

* fix topo sort

* added space before for

* final spacing nits
  • Loading branch information
Chillee authored Apr 23, 2020
1 parent 1e6e8b0 commit 0957a02
Show file tree
Hide file tree
Showing 83 changed files with 172 additions and 401 deletions.
2 changes: 1 addition & 1 deletion content/combinatorial/IntPerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

int permToInt(vi& v) {
int use = 0, i = 0, r = 0;
trav(x, v) r = r * ++i + __builtin_popcount(use & -(1 << x)),
for(int x:v) r = r * ++i + __builtin_popcount(use & -(1<<x)),
use |= 1 << x; // (note: minus, not ~!)
return r;
}
1 change: 0 additions & 1 deletion content/contest/template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
Expand Down
2 changes: 1 addition & 1 deletion content/data-structures/FenwickTree2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct FT2 {
for (; x < sz(ys); x |= x + 1) ys[x].push_back(y);
}
void init() {
trav(v, ys) sort(all(v)), ft.emplace_back(sz(v));
for (vi& v : ys) sort(all(v)), ft.emplace_back(sz(v));
}
int ind(int x, int y) {
return (int)(lower_bound(all(ys[x]), y) - ys[x].begin()); }
Expand Down
2 changes: 1 addition & 1 deletion content/geometry/3dHull.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ vector<F> hull3d(const vector<P3>& A) {
C(a, b, c); C(a, c, b); C(b, c, a);
}
}
trav(it, FS) if ((A[it.b] - A[it.a]).cross(
for (F& it : FS) if ((A[it.b] - A[it.a]).cross(
A[it.c] - A[it.a]).dot(it.q) <= 0) swap(it.c, it.b);
return FS;
};
2 changes: 1 addition & 1 deletion content/geometry/ClosestPair.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pair<P, P> closest(vector<P> v) {
sort(all(v), [](P a, P b) { return a.y < b.y; });
pair<ll, pair<P, P>> ret{LLONG_MAX, {P(), P()}};
int j = 0;
trav(p, v) {
for (P p : v) {
P d{1 + (ll)sqrt(ret.first), 0};
while (v[j].y <= p.y - d.x) S.erase(v[j++]);
auto lo = S.lower_bound(p - d), hi = S.upper_bound(p + d);
Expand Down
4 changes: 2 additions & 2 deletions content/geometry/ConvexHull.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Points on the edge of the hull between two other points are not considered part
\includegraphics[width=\textwidth]{content/geometry/ConvexHull}
\vspace{-6mm}
\end{minipage}
* Status: tested with Kattis problems convexhull
* Status: stress-tested, tested with Kattis problems convexhull
* Time: O(n \log n)
*/
#pragma once
Expand All @@ -27,7 +27,7 @@ vector<P> convexHull(vector<P> pts) {
vector<P> h(sz(pts)+1);
int s = 0, t = 0;
for (int it = 2; it--; s = --t, reverse(all(pts)))
trav(p, pts) {
for (P p : pts) {
while (t >= s + 2 && h[t-2].cross(h[t-1], p) <= 0) t--;
h[t++] = p;
}
Expand Down
4 changes: 2 additions & 2 deletions content/geometry/DelaunayTriangulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void delaunay(vector<P>& ps, F trifun) {
if (sz(ps) == 3) { int d = (ps[0].cross(ps[1], ps[2]) < 0);
trifun(0,1+d,2-d); }
vector<P3> p3;
trav(p, ps) p3.emplace_back(p.x, p.y, p.dist2());
if (sz(ps) > 3) trav(t, hull3d(p3)) if ((p3[t.b]-p3[t.a]).
for (P p : ps) p3.emplace_back(p.x, p.y, p.dist2());
if (sz(ps) > 3) for(auto t:hull3d(p3)) if ((p3[t.b]-p3[t.a]).
cross(p3[t.c]-p3[t.a]).dot(P3(0,0,1)) < 0)
trifun(t.a, t.c, t.b);
}
5 changes: 2 additions & 3 deletions content/geometry/ManhattanMST.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ vector<array<int, 3>> manhattanMST(vector<P> ps) {
sort(all(id), [&](int i, int j) {
return (ps[i]-ps[j]).x < (ps[j]-ps[i]).y;});
map<int, int> sweep;
trav(i,id) {
for (int i : id) {
for (auto it = sweep.lower_bound(-ps[i].y);
it != sweep.end(); sweep.erase(it++)) {
int j = it->second;
Expand All @@ -32,8 +32,7 @@ vector<array<int, 3>> manhattanMST(vector<P> ps) {
}
sweep[-ps[i].y] = i;
}
if (k & 1) trav(p,ps) p.x = -p.x;
else trav(p,ps) swap(p.x, p.y);
for (P& p : ps) if (k & 1) p.x = -p.x; else swap(p.x, p.y);
}
return edges;
}
5 changes: 2 additions & 3 deletions content/geometry/PolygonUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* Description: Calculates the area of the union of $n$ polygons (not necessarily
* convex). The points within each polygon must be given in CCW order.
* (Epsilon checks may optionally be added to sideOf/sgn, but shouldn't be needed.)
* Status: Submitted on ECNA 2017 Problem A, fuzz-tested
* Status: stress-tested, Submitted on ECNA 2017 Problem A
* Time: $O(N^2)$, where $N$ is the total number of points
* Usage:
*/
#pragma once

Expand Down Expand Up @@ -37,7 +36,7 @@ double polyUnion(vector<vector<P>>& poly) {
}
}
sort(all(segs));
trav(s,segs) s.first = min(max(s.first, 0.0), 1.0);
for (auto& s : segs) s.first = min(max(s.first, 0.0), 1.0);
double sum = 0;
int cnt = segs[0].second;
rep(j,1,sz(segs)) {
Expand Down
2 changes: 1 addition & 1 deletion content/geometry/PolyhedronVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
template<class V, class L>
double signedPolyVolume(const V& p, const L& trilist) {
double v = 0;
trav(i, trilist) v += p[i.a].cross(p[i.b]).dot(p[i.c]);
for (auto i : trilist) v += p[i.a].cross(p[i.b]).dot(p[i.c]);
return v / 6;
}
2 changes: 1 addition & 1 deletion content/graph/2sat.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct TwoSat {
vi val, comp, z; int time = 0;
int dfs(int i) {
int low = val[i] = ++time, x; z.push_back(i);
trav(e, gr[i]) if (!comp[e])
for(int e : gr[i]) if (!comp[e])
low = min(low, val[e] ?: dfs(e));
if (low == val[i]) do {
x = z.back(); z.pop_back();
Expand Down
4 changes: 2 additions & 2 deletions content/graph/BellmanFord.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void bellmanFord(vector<Node>& nodes, vector<Ed>& eds, int s) {
sort(all(eds), [](Ed a, Ed b) { return a.s() < b.s(); });

int lim = sz(nodes) / 2 + 2; // /3+100 with shuffled vertices
rep(i,0,lim) trav(ed, eds) {
rep(i,0,lim) for (Ed ed : eds) {
Node cur = nodes[ed.a], &dest = nodes[ed.b];
if (abs(cur.dist) == inf) continue;
ll d = cur.dist + ed.w;
Expand All @@ -29,7 +29,7 @@ void bellmanFord(vector<Node>& nodes, vector<Ed>& eds, int s) {
dest.dist = (i < lim-1 ? d : -inf);
}
}
rep(i,0,lim) trav(e, eds) {
rep(i,0,lim) for (Ed e : eds) {
if (nodes[e.a].dist == -inf)
nodes[e.b].dist = -inf;
}
Expand Down
2 changes: 1 addition & 1 deletion content/graph/BiconnectedComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int Time;
template<class F>
int dfs(int at, int par, F& f) {
int me = num[at] = ++Time, e, y, top = me;
trav(pa, ed[at]) if (pa.second != par) {
for (auto pa : ed[at]) if (pa.second != par) {
tie(y, e) = pa;
if (num[y]) {
top = min(top, num[y]);
Expand Down
4 changes: 2 additions & 2 deletions content/graph/DFSMatching.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
bool find(int j, vector<vi>& g, vi& btoa, vi& vis) {
if (btoa[j] == -1) return 1;
vis[j] = 1; int di = btoa[j];
trav(e, g[di])
for (int e : g[di])
if (!vis[e] && find(e, g, btoa, vis)) {
btoa[e] = di;
return 1;
Expand All @@ -28,7 +28,7 @@ int dfsMatching(vector<vi>& g, vi& btoa) {
vi vis;
rep(i,0,sz(g)) {
vis.assign(sz(btoa), 0);
trav(j,g[i])
for (int j : g[i])
if (find(j, g, btoa, vis)) {
btoa[j] = i;
break;
Expand Down
2 changes: 1 addition & 1 deletion content/graph/Dinic.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Dinic {
int qi = 0, qe = lvl[s] = 1;
while (qi < qe && !lvl[t]) {
int v = q[qi++];
trav(e, adj[v])
for (Edge e : adj[v])
if (!lvl[e.to] && e.c >> (30 - L))
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion content/graph/DirectedMST.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void pop(Node*& a) { a->prop(); a = merge(a->l, a->r); }
ll dmst(int n, int r, vector<Edge>& g) {
UF uf(n);
vector<Node*> heap(n);
trav(e, g) heap[e.b] = merge(heap[e.b], new Node{e});
for (Edge e : g) heap[e.b] = merge(heap[e.b], new Node{e});
ll res = 0;
vi seen(n, -1), path(n);
seen[r] = r;
Expand Down
2 changes: 1 addition & 1 deletion content/graph/EdmondsKarp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ template<class T> T edmondsKarp(vector<unordered_map<int, T>>& graph, int source

rep(i,0,ptr) {
int x = q[i];
trav(e, graph[x]) {
for (auto e : graph[x]) {
if (par[e.first] == -1 && e.second > 0) {
par[e.first] = x;
q[ptr++] = e.first;
Expand Down
2 changes: 1 addition & 1 deletion content/graph/EulerWalk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ vi eulerWalk(vector<vector<pii>>& gr, int nedges, int src=0) {
D[x]--, D[y]++;
eu[e] = 1; s.push_back(y);
}}
trav(x, D) if (x < 0 || sz(ret) != nedges+1) return {};
for (int x : D) if (x < 0 || sz(ret) != nedges+1) return {};
return {ret.rbegin(), ret.rend()};
}
2 changes: 1 addition & 1 deletion content/graph/GeneralMatching.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

vector<pii> generalMatching(int N, vector<pii>& ed) {
vector<vector<ll>> mat(N, vector<ll>(N)), A;
trav(pa, ed) {
for (pii pa : ed) {
int a = pa.first, b = pa.second, r = rand() % mod;
mat[a][b] = r, mat[b][a] = (mod - r) % mod;
}
Expand Down
2 changes: 1 addition & 1 deletion content/graph/GomoryHu.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vector<Edge> gomoryHu(int N, vector<Edge> ed) {
vi par(N);
rep(i,1,N) {
PushRelabel D(N); // Dinic also works
trav(t,ed) D.addEdge(t[0], t[1], t[2], t[2]);
for (Edge t : ed) D.addEdge(t[0], t[1], t[2], t[2]);
tree.push_back({i, par[i], D.calc(i, par[i])});
rep(j,i+1,N)
if (par[j] == par[i] && D.leftOfMinCut(j)) par[j] = i;
Expand Down
4 changes: 2 additions & 2 deletions content/graph/HLD.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ template <bool VALS_EDGES> struct HLD {
rt(N),pos(N),tree(new Node(0, N)){ dfsSz(),dfsHld();}
void dfsSz(int v = 0) {
if (par[v] != -1) adj[v].erase(find(all(adj[v]), par[v]));
trav(u, adj[v]) {
for (int& u : adj[v]) {
par[u] = v, depth[u] = depth[v] + 1;
dfsSz(u);
siz[v] += siz[u];
Expand All @@ -36,7 +36,7 @@ template <bool VALS_EDGES> struct HLD {
}
void dfsHld(int v = 0) {
pos[v] = tim++;
trav(u, adj[v]) {
for (int u : adj[v]) {
rt[u] = (u == adj[v][0] ? rt[v] : u);
dfsHld(u);
}
Expand Down
2 changes: 1 addition & 1 deletion content/graph/LCA.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct LCA {
LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
void dfs(vector<vi>& C, int v, int par) {
time[v] = T++;
trav(y, C[v]) if (y != par) {
for (int y : C[v]) if (y != par) {
path.push_back(v), ret.push_back(time[v]);
dfs(C, y, v);
}
Expand Down
10 changes: 5 additions & 5 deletions content/graph/MaximumClique.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct Maxclique {
vector<vi> C;
vi qmax, q, S, old;
void init(vv& r) {
trav(v,r) v.d = 0;
trav(v, r) trav(j, r) v.d += e[v.i][j.i];
for (auto& v : r) v.d = 0;
for (auto& v : r) for (auto j : r) v.d += e[v.i][j.i];
sort(all(r), [](auto a, auto b) { return a.d > b.d; });
int mxD = r[0].d;
rep(i,0,sz(r)) r[i].d = min(i, mxD) + 1;
Expand All @@ -33,12 +33,12 @@ struct Maxclique {
if (sz(q) + R.back().d <= sz(qmax)) return;
q.push_back(R.back().i);
vv T;
trav(v,R) if (e[R.back().i][v.i]) T.push_back({v.i});
for(auto v:R) if (e[R.back().i][v.i]) T.push_back({v.i});
if (sz(T)) {
if (S[lev]++ / ++pk < limit) init(T);
int j = 0, mxk = 1, mnk = max(sz(qmax) - sz(q) + 1, 1);
C[1].clear(), C[2].clear();
trav(v, T) {
for (auto v : T) {
int k = 1;
auto f = [&](int i) { return e[v.i][i]; };
while (any_of(all(C[k]), f)) k++;
Expand All @@ -47,7 +47,7 @@ struct Maxclique {
C[k].push_back(v.i);
}
if (j > 0) T[j - 1].d = 0;
rep(k,mnk,mxk + 1) trav(i, C[k])
rep(k,mnk,mxk + 1) for (int i : C[k])
T[j].i = i, T[j++].d = k;
expand(T, lev + 1);
} else if (sz(q) > sz(qmax)) qmax = q;
Expand Down
6 changes: 3 additions & 3 deletions content/graph/MinCostMaxFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ struct MCMF {
while (!q.empty()) {
s = q.top().second; q.pop();
seen[s] = 1; di = dist[s] + pi[s];
trav(i, ed[s]) if (!seen[i])
for (int i : ed[s]) if (!seen[i])
relax(i, cap[s][i] - flow[s][i], cost[s][i], 1);
trav(i, red[s]) if (!seen[i])
for (int i : red[s]) if (!seen[i])
relax(i, flow[i][s], -cost[i][s], 0);
}
rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
Expand All @@ -85,7 +85,7 @@ struct MCMF {
int it = N, ch = 1; ll v;
while (ch-- && it--)
rep(i,0,N) if (pi[i] != INF)
trav(to, ed[i]) if (cap[i][to])
for (int to : ed[i]) if (cap[i][to])
if ((v = pi[i] + cost[i][to]) < pi[to])
pi[to] = v, ch = 1;
assert(it >= 0); // negative cost cycle
Expand Down
4 changes: 2 additions & 2 deletions content/graph/MinimumVertexCover.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ vi cover(vector<vi>& g, int n, int m) {
vi match(m, -1);
int res = dfsMatching(g, match);
vector<bool> lfound(n, true), seen(m);
trav(it, match) if (it != -1) lfound[it] = false;
for (int it : match) if (it != -1) lfound[it] = false;
vi q, cover;
rep(i,0,n) if (lfound[i]) q.push_back(i);
while (!q.empty()) {
int i = q.back(); q.pop_back();
lfound[i] = 1;
trav(e, g[i]) if (!seen[e] && match[e] != -1) {
for (int e : g[i]) if (!seen[e] && match[e] != -1) {
seen[e] = true;
q.push_back(match[e]);
}
Expand Down
4 changes: 2 additions & 2 deletions content/graph/PushRelabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ struct PushRelabel {
int v = sz(g); H[s] = v; ec[t] = 1;
vi co(2*v); co[0] = v-1;
rep(i,0,v) cur[i] = g[i].data();
trav(e, g[s]) addFlow(e, e.c);
for (Edge& e : g[s]) addFlow(e, e.c);

for (int hi = 0;;) {
while (hs[hi].empty()) if (!hi--) return -ec[s];
int u = hs[hi].back(); hs[hi].pop_back();
while (ec[u] > 0) // discharge u
if (cur[u] == g[u].data() + sz(g[u])) {
H[u] = 1e9;
trav(e, g[u]) if (e.c && H[u] > H[e.dest]+1)
for (Edge& e : g[u]) if (e.c && H[u] > H[e.dest]+1)
H[u] = H[e.dest]+1, cur[u] = &e;
if (++co[H[u]], !--co[hi] && hi < v)
rep(i,0,v) if (hi < H[i] && H[i] < v)
Expand Down
2 changes: 1 addition & 1 deletion content/graph/SCC.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vi val, comp, z, cont;
int Time, ncomps;
template<class G, class F> int dfs(int j, G& g, F& f) {
int low = val[j] = ++Time, x; z.push_back(j);
trav(e,g[j]) if (comp[e] < 0)
for (auto e : g[j]) if (comp[e] < 0)
low = min(low, val[e] ?: dfs(e,g,f));

if (low == val[j]) {
Expand Down
4 changes: 2 additions & 2 deletions content/graph/TopoSort.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

vi topoSort(const vector<vi>& gr) {
vi indeg(sz(gr)), ret;
trav(li, gr) trav(x, li) indeg[x]++;
for (auto& li : gr) for (int x : li) indeg[x]++;
queue<int> q; // use priority queue for lexic. smallest ans.
rep(i,0,sz(gr)) if (indeg[i] == 0) q.push(-i);
while (!q.empty()) {
int i = -q.front(); // top() for priority queue
ret.push_back(i);
q.pop();
trav(x, gr[i])
for (int x : gr[i])
if (--indeg[x] == 0) q.push(-x);
}
return ret;
Expand Down
Loading

0 comments on commit 0957a02

Please sign in to comment.