#include #include using namespace std; struct edge { int to, avail, rev; }; constexpr int inf = 987'654'321; int R, C, N; vector> G; vector ptr; vector used; void add_edge(int u, int v, int c) { G[u].push_back(edge{v, c, ptr[v]++}); G[v].push_back(edge{u, 0, ptr[u]++}); } int dfs(int v, int t, int flow) { if(v == t) { return flow; } used[v] = true; for(edge &e : G[v]) { edge &x = G[e.to][e.rev]; if(!used[e.to] && e.avail > 0) { int res = dfs(e.to, t, min(e.avail, flow)); if(res > 0) { e.avail -= res; x.avail += res; return res; } } } return 0; } int max_flow(int s, int t) { int res = 0; for(;;) { used.assign(N, false); int flow = dfs(s, t, inf); if(flow == 0) { return res; } res += flow; } } int calc(int r, int c) { return r * C + c; } int main(void) { scanf("%d%d", &R, &C); vector F; for(int r=0; r> s; F.push_back(s); } ::N = R*C+2; G.assign(N, vector()); ptr.assign(N, 0); int src = R*C, dst = R*C+1; int cntw = 0, cntb = 0; for(int r=0; r