每个人拆成两个点,一个表示接受喜欢的,一个表示不接受喜欢的,(男yes,男no,女yes,女no)
男yes->男no,容量为k;女no->女yes,容量为k
男女喜欢,则男yes->女yes,容量为1
否则男no->女no,容量为1

二分歌曲x,S->男yes,容量为x;女yes->T,容量为x
若最大流==n*x则满足要求

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(210), __(4e4 + 10), INF(2147483647); IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int n, k, w[__], fst[_], nxt[__], to[__], cnt, tmp1[_], tcnt, tmp2[__];
int S, T, lev[_], cur[_], max_flow, ans;
queue <int> Q; IL void Add(RG int u, RG int v, RG int f){
w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
} IL int Dfs(RG int u, RG int maxf){
if(u == T) return maxf;
RG int ret = 0;
for(RG int &e = cur[u]; e != -1; e = nxt[e]){
if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
RG int f = Dfs(to[e], min(w[e], maxf - ret));
ret += f; w[e ^ 1] += f; w[e] -= f;
if(ret == maxf) break;
}
return ret;
} IL bool Bfs(){
Fill(lev, 0); lev[S] = 1; Q.push(S);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(lev[to[e]] || !w[e]) continue;
lev[to[e]] = lev[u] + 1;
Q.push(to[e]);
}
}
return lev[T];
} IL bool Check(RG int x){
Copy(fst, tmp1); cnt = tcnt; Copy(w, tmp2);
for(RG int i = 1; i <= n; i++) Add(S, i, x), Add(i + 3 * n, T, x);
max_flow = 0;
while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
return max_flow == x * n;
} int main(RG int argc, RG char* argv[]){
n = Read(); k = Read(); Fill(fst, -1); T = 4 * n + 1;
for(RG int i = 1; i <= n; i++) Add(i, i + n, k), Add(i + 2 * n, i + 3 * n, k);
for(RG int i = 1; i <= n; i++)
for(RG int j = 1; j <= n; j++){
RG char c; scanf(" %c", &c);
if(c == 'Y') Add(i, j + 3 * n, 1);
else Add(i + n, j + 2 * n, 1);
}
Copy(tmp1, fst); Copy(tmp2, w); tcnt = cnt;
RG int l = 0, r = n;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}

[CQOI2009]dance跳舞的更多相关文章

  1. BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流

    1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...

  2. BZOJ 1305: [CQOI2009]dance跳舞( 最大流 )

    云神代码很短...0 ms过的...看了代码 , 大概是贪心... orz 我不会证 数据这么小乱搞就可以了吧... ←_← 这道题网络流还是可以写的... 既然限制了最多只能和 k 个不喜欢的人da ...

  3. bzoj千题计划130:bzoj1305: [CQOI2009]dance跳舞

    http://www.lydsy.com/JudgeOnline/problem.php?id=1305 每个人拆为喜欢(yes)和不喜欢(no)两个点 二分答案 1.每两个人之间只能跳一次 喜欢则 ...

  4. AC日记——[CQOI2009]DANCE跳舞 洛谷 P3153

    [CQOI2009]DANCE跳舞 思路: 二分+最大流: 代码: #include <cstdio> #include <cstring> #include <iost ...

  5. BZOJ1305 [CQOI2009]dance跳舞 【网络流】

    1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 3714  Solved: 1572 [Submit][ ...

  6. bzoj 1305: [CQOI2009]dance跳舞

    题目链接 bzoj 1305: [CQOI2009]dance跳舞 题解 男,女生拆点A1A2,B1B2,拆成两点间分别连容量为K的边,限制与不喜欢的人跳舞的数量 A1连接源点容量为x,B1连接汇点容 ...

  7. bzoj1305: [CQOI2009]dance跳舞(二分答案+网络流)

    1305: [CQOI2009]dance跳舞 题目:传送门 题解: 一眼网络流基础建模...然后就GG了 二分答案+拆点建边+最大流判断: 把男女生拆为男1,男2,女1,女2 1.男1和男2还有女1 ...

  8. [CQOI2009]dance跳舞(最大流+二分)

    [CQOI2009]dance跳舞 每个人拆成$2$个点,表示是否与喜欢的人跳舞 跳$m$首舞曲时,满足最大流为$n*m$ 二分$m$,跑最大流即可 #include<cstdio> #i ...

  9. Bzoj1305 [CQOI2009]dance跳舞

    Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2925  Solved: 1221 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时 ...

  10. 【BZOJ1305】 [CQOI2009]dance跳舞

    Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...

随机推荐

  1. [Python Study Notes]Socket模拟ssh执行cmd并记录遇到的问题

    服务器端: 流程: 1.创建servert实例 2.绑定地址和端口 3.开始监听 4.创建客户端连接实例 5.等待客户端的消息 6.......... # The_author = 'liu66' # ...

  2. Cookie禁用 获取session

    转自:http://blog.csdn.net/u010433704/article/details/40950599 Cookie与 Session,一般认为是两个独立的东西,Session采用的是 ...

  3. dig挖出DNS的秘密

    [最简单的dig用法] 最简单的dig用法,当然就是直接输入dig按回车. 1 2 3 4 5 6 $ dig   ; <<>> DiG 9.8.2rc1-RedHat-9.8 ...

  4. 编译安装python3.6后pip3无法安装模块问题处理

    编译安装python3.6之后,使用pip3命令安装第三方库效果如图所示: pip is configured with locations that require TLS/SSL, however ...

  5. Zabbix 3.2.4至3.2.7的升级方案

    1.关闭Zabbix Server 防止有新的数据提交到数据库中,也可以关闭数据库.如果更新过程中,评估告警信息可以忽略,可以先执行备份操作. 1.1.检查当前版本 /usr/local/zabbix ...

  6. Java经典编程题50道之二十二

    利用递归方法求5!. public class Example22 {    public static void main(String[] args) {        int n = 5;   ...

  7. 注册表命令 regedit32

    转自 https://zhidao.baidu.com/question/1958216489744783460.html Regedt32.exe 不支持注册表项文件 (.reg) 的导入和导出. ...

  8. Yii如何使用数据库

    1.Yii如何使用数据库 Yii通过数据库访问对象(Database Access Objects,简称DAO)来使用数据库的. DAO建立在"PHP数据对象(PDO)之上,并提供一套面向对 ...

  9. Java文件复制与读写

    函数介绍 public String readLine():每次读取文件的一行,当文件读取完毕时,返回null     public int read(byte[] b):将文件内容读取到字节数组b ...

  10. hihoCoder 树结构判定(并查集)

    思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...