# [BZOJ1305]&&[CQOI2009] dance 跳舞

2 1
NY
NY

## 样例输出 SampleOutput

1

BZOJ 1305

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf = 0x7fffffff / 27.11;
const int maxn = 4 * 55;
const int maxm = maxn * maxn;
int i, j, t, n, m, l, r, k, z, y, x;
struct edge
{
int to, fl, cp, nx;
} e[maxm];
int head[maxn], dis[maxn], used[maxn], cur[maxn];
char ch[maxn];
int num, cnt, sum, mid;
deque <int> q;
inline void ins(int u, int v, int f, int c)
{
e[cnt] = (edge)
{
v, f, c, head[u]
}; head[u] = cnt++;
e[cnt] = (edge)
{
u, 0, 0, head[v]
}; head[v] = cnt++;
}
inline bool bfs(int s, int t, int tim)
{
int i, u, v;
while (!q.empty()) q.pop_front();
dis[s] = 0; used[s] = tim;
q.push_back(s);
while (!q.empty())
{
u = q.front(); q.pop_front();
for (i = head[u]; i != -1; i = e[i].nx)
{
v = e[i].to;
if (used[v] != tim && e[i].fl > 0)
{
used[v] = tim;
dis[v] = dis[u] + 1;
q.push_back(v);
}
}
}
return (used[t] == tim);
}
int dfs(int u, int t, int flow)
{
int i, v, d, k;
if (u == t) return flow;
for (d = flow, i = cur[u]; i != -1; i = e[i].nx)
{
v = e[i].to;
if (dis[v] == dis[u] + 1 && e[i].fl > 0)
{
k = dfs(v, t, min(d, e[i].fl));
e[i].fl -= k; e[i ^ 1].fl += k;
d -= k;
if (d == 0) break;
}
}
return flow - d;
}
inline bool dinic(int s, int t, int lim)
{
int i, ans = 0;
for (i = 0; i < cnt; i++) e[i].fl = (e[i].cp == -1) ? lim : e[i].cp;
while (bfs(s, t, ++num))
{
ans += dfs(s, t, inf);
}
return (ans == lim * n);
}
int main()
{
cnt = num = 0;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) ins(0, i, 0, -1);
for (i = 1; i <= n; i++) ins(3 * n + i, 4 * n + 1, 0, -1);
for (i = 1; i <= n; i++) ins(i, n + i, k, k), ins(n * 2 + i, n * 3 + i, k, k);
for (i = 1; i <= n; i++)
{
scanf("%s", ch + 1);
for (j = 1; j <= n; j++)
{
if (ch[j] == 'Y') ins(i, 3 * n + j, 1, 1);
else ins(n + i, 2 * n + j, 1, 1);
}
}
l = 0; r = n;
while (l <= r)
{
mid = (l + r) >> 1;
if (dinic(0, 4 * n + 1, mid)) l = mid + 1 ;
else r = mid - 1;
}
printf("%d\n", l - 1);
return 0;
}``````