/** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_d */ class MaxFlow { public class CapEdge { private final int from, to; private long cap; private final int rev; CapEdge(int from, int to, long cap, int rev) { this.from = from; this.to = to; this.cap = cap; this.rev = rev; } public int getFrom() {return from;} public int getTo() {return to;} public long getCap() {return cap;} public long getFlow() {return g[to][rev].cap;} } private static final long INF = Long.MAX_VALUE; private final int n; private int m; private final java.util.ArrayList edges; private final int[] count; private final CapEdge[][] g; public MaxFlow(int n) { this.n = n; this.edges = new java.util.ArrayList(); this.count = new int[n]; this.g = new CapEdge[n][]; } public int addEdge(int from, int to, long cap) { rangeCheck(from, 0, n); rangeCheck(to, 0, n); nonNegativeCheck(cap, "Capacity"); CapEdge e = new CapEdge(from, to, cap, count[to]); count[from]++; count[to]++; edges.add(e); return m++; } public CapEdge getEdge(int i) { rangeCheck(i, 0, m); return edges.get(i); } public java.util.ArrayList getEdges() { return edges; } public void changeEdge(int i, long newCap, long newFlow) { rangeCheck(i, 0, m); nonNegativeCheck(newCap, "Capacity"); if (newFlow > newCap) { throw new IllegalArgumentException( String.format("Flow %d is greater than capacity %d.", newCap, newFlow) ); } CapEdge e = edges.get(i); CapEdge er = g[e.to][e.rev]; e.cap = newCap - newFlow; er.cap = newFlow; } private void buildGraph() { for (int i = 0; i hd) { int u = que[hd++]; for (CapEdge e : g[u]) { int v = e.to; if (e.cap = 0) continue; level[v] = level[u] + 1; if (v == t) return; que[tl++] = v; } } } private long dinicDFS(int cur, int s, long f, int[] iter, int[] level) { if (cur == s) return f; long res = 0; while (iter[cur] = level[cur] || e.cap 0) { int u = stack[--ptr]; for (CapEdge e : g[u]) { int v = e.to; if (reachable[v] || e.cap = maxExclusive) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for length %d", i, maxExclusive) ); } } private void nonNegativeCheck(long cap, String attribute) { if (cap