1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int M = 2e6 + 10; const int inf = 0x3f3f3f3f; int n, k, s, t; int cnt = 1; int head[N]; struct edge { int u, v, c, nxt; edge(int u = 0, int v = 0, int c = 0, int nxt = 0) : u(u), v(v), c(c), nxt(nxt) {} } e[M * 2]; void ADD(int u, int v, int c) { cnt++; e[cnt] = edge(u, v, c, head[u]); head[u] = cnt; } void add_edge(int u, int v, int c) { ADD(u, v, c); ADD(v, u, 0); } int dep[N]; int now[N]; bool bfs() { memset(dep, 0, sizeof dep); memcpy(now, head, sizeof head); queue<int>q; dep[s] = 1; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; int c = e[i].c; if (dep[v] != 0 || !c) continue; dep[v] = dep[u] + 1; q.push(v); } } return dep[t] != 0; } int dfs(int u, int t, int flow) { if (u == t) return flow; int nowflow = 0; for (int i = now[u]; i; i = e[i].nxt) { now[u] = i; int v = e[i].v; int c = e[i].c; if (dep[v] != dep[u] + 1 || !c ) continue; int ff = dfs(v, t, min(flow, c)); if (ff) flow -= ff, nowflow += ff, e[i].c -= ff, e[i ^ 1].c += ff; } return nowflow; } bool maxflow() { int mxflow = 0; int nowflow; while (bfs()) { while ((nowflow = dfs(s, t, inf))) mxflow += nowflow; if (mxflow >= n) return 1; } return 0; } int re1[N]; int re2[N]; int main() { cin >> n >> k; s = n * 4 + 1, t = n * 4 + 2; for (int i = 1; i <= n; i++) { add_edge(i, n * 2 + i, k); add_edge(n * 3 + i, n + i, k); string a; cin >> a; for (int j = 0; j < n; j++) { if (a[j] == 'Y') add_edge(i, j + 1 + n, 1); else add_edge(n * 2 + i, j + 1 + n*3, 1); } } int tot = 0; for (int i = 1; i <= n; i++) add_edge(s, i, 1), re1[i] = cnt - 1, add_edge(i + n, t, 1), re2[i] = cnt - 1; while (maxflow()) { tot++; for (int i = 1; i <= n; i++) { e[re1[i]].c += e[re1[i] ^ 1].c; e[re1[i] ^ 1].c = 0; e[re2[i]].c += e[re2[i] ^ 1].c; e[re2[i] ^ 1].c = 0; } } cout << tot; }
|