【题目链接】
看了【POPOQQQ的题解】,建图比较厉害。
写网络流第一次把点开小了...orz
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 255, maxm = 10005, maxg = 55, maxq = 10000, inf = 0x3f3f3f3f; int n, m, head[maxn], cur[maxn], cnt, A[maxg][maxg], depth[maxn], bg, ed, q[maxq]; struct _edge { int v, w, next; } g[maxm << 1]; inline void add(int u, int v, int w) { g[cnt] = (_edge){v, w, head[u]}; head[u] = cnt++; } inline void insert(int u, int v, int w) { add(u, v, w); add(v, u, 0); } inline bool bfs() { for(int i = 0; i <= ed; i++) depth[i] = -1; int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1; while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(g[i].w && !~depth[g[i].v]) { depth[g[i].v] = depth[u] + 1; if(g[i].v == ed) return 1; q[t++] = g[i].v; } return 0; } inline int dfs(int x, int flow) { if(x == ed) return flow; int left = flow; for(int i = cur[x]; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] == depth[x] + 1) { int tmp = dfs(g[i].v, min(left, g[i].w)); left -= tmp; g[i].w -= tmp; g[i ^ 1].w += tmp; if(g[i].w) cur[x] = i; if(!left) return flow; } if(left == flow) depth[x] = -1; return flow - left; } inline bool check(int mid) { for(int i = 0; i <= ed; i++) head[i] = -1; cnt = 0; for(int i = 1; i <= n; i++) { insert(bg, i, mid); insert(n + n + n + i, ed, mid); insert(i, n + i, m); insert(n + n + i, n + n + n + i, m); } for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(A[i][j]) insert(i, n + n + n + j, 1); else insert(n + i, n + n + j, 1); int ans = 0; while(bfs()) { for(int i = 0; i <= ed; i++) cur[i] = head[i]; ans += dfs(bg, inf); } return n * mid == ans; } char str[maxg]; int main() { scanf("%d%d", &n, &m); bg = 0; ed = (n << 2) + 1; for(int i = 1; i <= n; i++) { scanf("%s", str + 1); for(int j = 1; j <= n; j++) A[i][j] = (str[j] == 'Y'); } int l = 0, r = n; while(l <= r) { int mid = l + r >> 1; if(check(mid)) l = mid + 1; else r = mid - 1; } printf("%d\n", r); return 0; }