forked from oToToT/kiseki
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into reorganize_files
- Loading branch information
Showing
15 changed files
with
273 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,40 @@ | ||
template <typename Cap = int64_t> class Dinic { | ||
private: | ||
struct E { int to, rev; Cap cap; }; int n, st, ed; | ||
vector<vector<E>> G; vector<int> lv, idx; | ||
bool BFS() { | ||
lv.assign(n, -1); | ||
queue<int> bfs; bfs.push(st); lv[st] = 0; | ||
while (not bfs.empty()) { | ||
int u = bfs.front(); bfs.pop(); | ||
for (auto e: G[u]) { | ||
if (e.cap <= 0 or lv[e.to] != -1) continue; | ||
bfs.push(e.to); lv[e.to] = lv[u] + 1; | ||
} | ||
} | ||
return lv[ed] != -1; | ||
} | ||
Cap DFS(int u, Cap f) { | ||
if (u == ed) return f; | ||
Cap ret = 0; | ||
for(int &i = idx[u]; i < int(G[u].size()); ++i) { | ||
auto &e = G[u][i]; | ||
if (e.cap <= 0 or lv[e.to] != lv[u]+1) continue; | ||
Cap nf = DFS(e.to, min(f, e.cap)); | ||
ret += nf; e.cap -= nf; f -= nf; | ||
G[e.to][e.rev].cap += nf; | ||
if (f == 0) return ret; | ||
} | ||
if (ret == 0) lv[u] = -1; | ||
return ret; | ||
} | ||
struct E { int to, rev; Cap cap; }; int n, st, ed; | ||
vector<vector<E>> G; vector<size_t> lv, idx; | ||
bool BFS() { | ||
lv.assign(n, 0); idx.assign(n, 0); | ||
queue<int> bfs; bfs.push(st); lv[st] = 1; | ||
while (not bfs.empty()) { | ||
int u = bfs.front(); bfs.pop(); | ||
for (auto e: G[u]) if (e.cap > 0 and !lv[e.to]) | ||
bfs.push(e.to), lv[e.to] = lv[u] + 1; | ||
} | ||
return lv[ed]; | ||
} | ||
Cap DFS(int u, Cap f = numeric_limits<Cap>::max()) { | ||
if (u == ed) return f; | ||
Cap ret = 0; | ||
for (auto &i = idx[u]; i < G[u].size(); ++i) { | ||
auto &[to, rev, cap] = G[u][i]; | ||
if (cap <= 0 or lv[to] != lv[u] + 1) continue; | ||
Cap nf = DFS(to, min(f, cap)); | ||
ret += nf; cap -= nf; f -= nf; | ||
G[to][rev].cap += nf; | ||
if (f == 0) return ret; | ||
} | ||
if (ret == 0) lv[u] = 0; | ||
return ret; | ||
} | ||
public: | ||
void init(int n_) { G.assign(n = n_, vector<E>()); } | ||
void add_edge(int u, int v, Cap c) { | ||
G[u].push_back({v, int(G[v].size()), c}); | ||
G[v].push_back({u, int(G[u].size())-1, 0}); | ||
} | ||
Cap max_flow(int st_, int ed_) { | ||
st = st_, ed = ed_; Cap ret = 0, f; | ||
while (BFS()) { | ||
idx.assign(n, 0); | ||
f = DFS(st, numeric_limits<Cap>::max()); ret += f; | ||
if (f == 0) break; | ||
} | ||
return ret; | ||
} | ||
}; | ||
void init(int n_) { G.assign(n = n_, vector<E>()); } | ||
void add_edge(int u, int v, Cap c) { | ||
G[u].push_back({v, int(G[v].size()), c}); | ||
G[v].push_back({u, int(G[u].size())-1, 0}); | ||
} | ||
Cap max_flow(int st_, int ed_) { | ||
st = st_, ed = ed_; Cap ret = 0; | ||
while (BFS()) ret += DFS(st); | ||
return ret; | ||
} | ||
}; // test @ luogu P3376 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,63 @@ | ||
struct Edge { int to, cap, rev, cost; }; | ||
vector<Edge> g[kN]; | ||
int dist[kN], pv[kN], ed[kN]; | ||
bool mark[kN]; | ||
int NegativeCycle(int n) { | ||
memset(mark, false, sizeof(mark)); | ||
memset(dist, 0, sizeof(dist)); | ||
int upd = -1; | ||
for (int i = 0; i <= n; ++i) { | ||
for (int j = 0; j < n; ++j) { | ||
int idx = 0; | ||
for (auto &e : g[j]) { | ||
if(e.cap > 0 && dist[e.to] > dist[j] + e.cost){ | ||
dist[e.to] = dist[j] + e.cost; | ||
pv[e.to] = j, ed[e.to] = idx; | ||
if (i == n) { | ||
upd = j; | ||
while(!mark[upd])mark[upd]=1,upd=pv[upd]; | ||
return upd; | ||
} | ||
} | ||
idx++; | ||
} | ||
} | ||
} | ||
return -1; | ||
int vis[N], visc, fa[N], fae[N], head[N], mlc = 1; | ||
struct ep { | ||
int to, next; | ||
ll flow, cost; | ||
} e[M << 1]; | ||
void adde(int u, int v, ll fl, int cs) { | ||
e[++mlc] = {v, head[u], fl, cs}; | ||
head[u] = mlc; | ||
e[++mlc] = {u, head[v], 0, -cs}; | ||
head[v] = mlc; | ||
} | ||
void dfs(int u) { | ||
vis[u] = 1; | ||
for (int i = head[u], v; i; i = e[i].next) | ||
if (!vis[v = e[i].to] and e[i].flow) | ||
fa[v] = u, fae[v] = i, dfs(v); | ||
} | ||
int Solve(int n) { | ||
int rt = -1, ans = 0; | ||
while ((rt = NegativeCycle(n)) >= 0) { | ||
memset(mark, false, sizeof(mark)); | ||
vector<pair<int, int>> cyc; | ||
while (!mark[rt]) { | ||
cyc.emplace_back(pv[rt], ed[rt]); | ||
mark[rt] = true; | ||
rt = pv[rt]; | ||
} | ||
reverse(cyc.begin(), cyc.end()); | ||
int cap = kInf; | ||
for (auto &i : cyc) { | ||
auto &e = g[i.first][i.second]; | ||
cap = min(cap, e.cap); | ||
} | ||
for (auto &i : cyc) { | ||
auto &e = g[i.first][i.second]; | ||
e.cap -= cap; | ||
g[e.to][e.rev].cap += cap; | ||
ans += e.cost * cap; | ||
} | ||
ll phi(int x) { | ||
static ll pi[N]; | ||
if (x == -1) return 0; | ||
if (vis[x] == visc) return pi[x]; | ||
return vis[x] = visc, pi[x] = phi(fa[x]) - e[fae[x]].cost; | ||
} | ||
void pushflow(int x, ll &cost) { | ||
int v = e[x ^ 1].to, u = e[x].to; | ||
++visc; | ||
while (v != -1) vis[v] = visc, v = fa[v]; | ||
while (u != -1 && vis[u] != visc) | ||
vis[u] = visc, u = fa[u]; | ||
vector<int> cyc; | ||
int e2 = 0, pa = 2; | ||
ll f = e[x].flow; | ||
for (int i = e[x ^ 1].to; i != u; i = fa[i]) { | ||
cyc.push_back(fae[i]); | ||
if (e[fae[i]].flow < f) | ||
f = e[fae[e2 = i] ^ (pa = 0)].flow; | ||
} | ||
for (int i = e[x].to; i != u; i = fa[i]) { | ||
cyc.push_back(fae[i] ^ 1); | ||
if (e[fae[i] ^ 1].flow < f) | ||
f = e[fae[e2 = i] ^ (pa = 1)].flow; | ||
} | ||
return ans; | ||
cyc.push_back(x); | ||
for (int cyc_i : cyc) { | ||
e[cyc_i].flow -= f, e[cyc_i ^ 1].flow += f; | ||
cost += 1ll * f * e[cyc_i].cost; | ||
} | ||
if (pa == 2) return; | ||
int le = x ^ pa, l = e[le].to, o = e[le ^ 1].to; | ||
while (l != e2) { | ||
vis[o] = 0; | ||
swap(le ^= 1, fae[o]), swap(l, fa[o]), swap(l, o); | ||
} | ||
} | ||
ll simplex() { // 1-based | ||
ll cost = 0; | ||
memset(fa, -1, sizeof(fa)), dfs(1); | ||
vis[1] = visc = 2, fa[1] = -1; | ||
for (int i = 2, pre = -1; i != pre; i = (i == mlc ? 2 : i + 1)) | ||
if (e[i].flow and e[i].cost < phi(e[i ^ 1].to) - phi(e[i].to)) | ||
pushflow(pre = i, cost); | ||
return cost; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
// be careful of type | ||
Cir getCircum(P a, P b, P c){ | ||
Cir getCircum(P a, P b, P c){ // P = complex<llf> | ||
P z1 = a - b, z2 = a - c; llf D = cross(z1, z2) * 2; | ||
llf c1 = dot(a + b, z1), c2 = dot(a + c, z2); | ||
P o = (c2 * conj(z1) - c1 * conj(z2)) / D; | ||
P o = rot90(c2 * z1 - c1 * z2) / D; | ||
return { o, abs(o - a) }; | ||
} | ||
Cir minCircleCover(vector<P> &pts) { | ||
shuffle(pts.begin(), pts.end(), mt19937(114514)); | ||
Cir c = { pts[0], 0 }; | ||
for(int i = 0; i < (int)pts.size(); i++) { | ||
assert (!pts.empty()); | ||
ranges::shuffle(pts, mt19937(114514)); | ||
Cir c = { 0, 0 }; | ||
for(size_t i = 0; i < pts.size(); i++) { | ||
if (dist(pts[i], c.o) <= c.r) continue; | ||
c = { pts[i], 0 }; | ||
for (int j = 0; j < i; j++) { | ||
for (size_t j = 0; j < i; j++) { | ||
if (dist(pts[j], c.o) <= c.r) continue; | ||
c.o = (pts[i] + pts[j]) / llf(2); | ||
c.r = dist(pts[i], c.o); | ||
for (int k = 0; k < j; k++) { | ||
for (size_t k = 0; k < j; k++) { | ||
if (dist(pts[k], c.o) <= c.r) continue; | ||
c = getCircum(pts[i], pts[j], pts[k]); | ||
} | ||
} | ||
} | ||
return c; | ||
} | ||
} // test @ TIOJ 1093 & luogu P1742 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
// from 8BQube, plz ensure p is strict convex hull | ||
const llf INF = 1e18, qi = acos(-1) / 2 * 3; | ||
pair<llf, llf> solve(vector<P> &p) { | ||
#define Z(v) (p[v] - p[i]) | ||
llf mx = 0, mn = INF; | ||
int n = (int)p.size(); p.emplace_back(p[0]); | ||
pair<llf, llf> solve(const vector<P> &p) { | ||
llf mx = 0, mn = INF; int n = (int)p.size(); | ||
for (int i = 0, u = 1, r = 1, l = 1; i < n; ++i) { | ||
#define Z(v) (p[(v) % n] - p[i]) | ||
P e = Z(i + 1); | ||
while (cross(e, Z(u + 1)) > cross(e, Z(u))) | ||
u = (u + 1) % n; | ||
while (dot(e, Z(r + 1)) > dot(e, Z(r))) | ||
r = (r + 1) % n; | ||
if (!i) l = (r + 1) % n; | ||
while (dot(e, Z(l + 1)) < dot(e, Z(l))) | ||
l = (l + 1) % n; | ||
P D = p[r] - p[l]; | ||
mn = min(mn, dot(e, D) / llf(norm(e)) * cross(e, Z(u))); | ||
while (cross(e, Z(u + 1)) > cross(e, Z(u))) ++u; | ||
while (dot(e, Z(r + 1)) > dot(e, Z(r))) ++r; | ||
if (!i) l = r + 1; | ||
while (dot(e, Z(l + 1)) < dot(e, Z(l))) ++l; | ||
P D = p[r % n] - p[l % n]; | ||
llf H = cross(e, Z(u)) / llf(norm(e)); | ||
mn = min(mn, dot(e, D) * H); | ||
llf B = sqrt(norm(D)) * sqrt(norm(Z(u))); | ||
llf deg = (qi - acos(dot(D, Z(u)) / B)) / 2; | ||
mx = max(mx, B * sin(deg) * sin(deg)); | ||
} | ||
return {mn, mx}; | ||
} | ||
} // test @ UVA 819 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
// A, B are convex hull rotate to min by (X, Y) | ||
// A, B are strict convex hull rotate to min by (X, Y) | ||
vector<P> Minkowski(vector<P> A, vector<P> B) { | ||
vector<P> C(1, A[0] + B[0]), s1, s2; | ||
const int N = (int)A.size(), M = (int)B.size(); | ||
for(int i = 0; i < N; ++i) | ||
s1.pb(A[(i + 1) % N] - A[i]); | ||
for(int i = 0; i < M; i++) | ||
s2.pb(B[(i + 1) % M] - B[i]); | ||
for(int i = 0, j = 0; i < N || j < M;) | ||
if (j >= N || (i < M && cross(s1[i], s2[j]) >= 0)) | ||
C.pb(C.back() + s1[i++]); | ||
else | ||
C.pb(C.back() + s2[j++]); | ||
return hull(C), C; | ||
vector<P> sa(N), sb(M), C(N + M + 1); | ||
for (int i = 0; i < N; i++) sa[i] = A[(i+1)%N]-A[i]; | ||
for (int i = 0; i < M; i++) sb[i] = B[(i+1)%M]-B[i]; | ||
C[0] = A[0] + B[0]; | ||
for (int i = 0, j = 0; i < N || j < M; ) { | ||
P e = (j>=M || (i<N && cross(sa[i], sb[j])>=0)) | ||
? sa[i++] : sb[j++]; | ||
C[i + j] = e; | ||
} | ||
partial_sum(all(C), C.begin()); C.pop_back(); | ||
return convex_hull(C); // just to remove colinear | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// partition s = w[0] + w[1] + ... + w[k-1], | ||
// w[0] >= w[1] >= ... >= w[k-1] | ||
// each w[i] strictly smaller than all its suffix | ||
void duval(const auto &s, auto &&report) { | ||
int n = (int)s.size(), i = 0, j, k; | ||
while (i < n) { | ||
for (j = i + 1, k = i; j < n && s[k] <= s[j]; j++) | ||
k = (s[k] < s[j] ? i : k + 1); | ||
// if (i < n / 2 && j >= n / 2) { | ||
// for min cyclic shift, call duval(s + s) | ||
// then here s.substr(i, n / 2) is min cyclic shift | ||
// } | ||
for (; i <= k; i += j - k) | ||
report(i, j - k); // s.substr(l, len) | ||
} | ||
} // tested @ luogu 6114, 1368 & UVA 719 |
Oops, something went wrong.