1305: [CQOI2009]dance跳舞

来源:互联网 发布:会计事务所审计软件 编辑:程序博客网 时间:2024/04/30 20:27

1305: [CQOI2009]dance跳舞

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 2660  Solved: 1101
[Submit][Status][Discuss]

Description

一次舞会有n个男孩和n个女孩。每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞。每个男孩都不会和同一个女孩跳两首(或更多)舞曲。有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”)。每个男孩最多只愿意和k个不喜欢的女孩跳舞,而每个女孩也最多只愿意和k个不喜欢的男孩跳舞。给出每对男孩女孩是否相互喜欢的信息,舞会最多能有几首舞曲?

Input

第一行包含两个整数n和k。以下n行每行包含n个字符,其中第i行第j个字符为'Y'当且仅当男孩i和女孩j相互喜欢。

Output

仅一个数,即舞曲数目的最大值。

Sample Input

3 0
YYY
YYY
YYY

Sample Output

3

HINT

N<=50 K<=30

Source

加强数据By dwellings and liyizhen2

[Submit][Status][Discuss]

很强烈的最大流感觉啊
把每个男生or女生看成点
对于每个人多建立两个点
Ai:这个男生or女生和互相喜欢的男生or女生跳舞~
Bi:这个男生or女生和*********的男生or女生跳舞-_-

二分ans
超级源向每个男生连边,容量为ans
每个男生向Bi连边,容量为INF
每个男生向Ci连边,容量为1
女生类似向超级汇连边
中间的图就容量为1的边连一连就好啦
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<bitset>#include<algorithm>#include<cstring>#include<map>#include<stack>#include<set>#include<cmath>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int INF = ~0U>>1;const int maxn = 1E5 + 10;struct E{int to,cap,flow;E(){}E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}}edgs[maxn];int n,cnt,tot,Cnt,s,t,k,vis[maxn],L[maxn],cur[maxn],A[110],B[110],C[110];char cp[55][55];vector <int> v[maxn];queue <int> Q;void Add(int x,int y,int w){v[x].push_back(cnt); edgs[cnt++] = E(y,w,0);v[y].push_back(cnt);edgs[cnt++] = E(x,0,0);}bool BFS(){vis[s] = ++Cnt;L[s] = 1;Q.push(s);while (!Q.empty()) {int k = Q.front(); Q.pop();for (int i = 0; i < v[k].size(); i++) {E e = edgs[v[k][i]];if (e.cap == e.flow) continue;if (vis[e.to] == Cnt) continue;L[e.to] = L[k] + 1;vis[e.to] = Cnt;Q.push(e.to);}}return vis[t] == Cnt;}int Dicnic(int x,int a){if (x == t || !a) return a;int flow = 0;for (int &i = cur[x]; i < v[x].size(); i++) {E &e = edgs[v[x][i]];if (e.cap == e.flow) continue;if (L[e.to] != L[x] + 1) continue;int f = Dicnic(e.to,min(a,e.cap - e.flow));if (!f) continue;e.flow += f;a -= f;flow += f;edgs[v[x][i]^1].flow -= f;if (!a) return flow;}if (!flow) L[x] = -1;return flow;}bool Judge(int now){cnt = 0;for (int i = 1; i <= n; i++)Add(s,A[i],now);for (int i = n + 1; i <= 2*n; i++)Add(A[i],t,now);for (int i = 1; i <= n; i++) {Add(A[i],B[i],INF);Add(A[i],C[i],k);Add(B[i+n],A[i+n],INF);Add(C[i+n],A[i+n],k);}for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++) if (cp[i][j] == 'Y')Add(B[i],B[j+n],1);elseAdd(C[i],C[j+n],1);int MaxFlow = 0;while (BFS()) {for (int i = s; i <= t; i++) cur[i] = 0;MaxFlow += Dicnic(s,INF);}for (int i = s; i <= t; i++)v[i].clear();return MaxFlow == n*now;}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n >> k;t = 6*n + 1;for (int i = 1; i <= n; i++)scanf("%s",cp[i] + 1);for (int i = 1; i <= 2*n; i++) {A[i] = ++tot;B[i] = ++tot;C[i] = ++tot;}int l = 0,r = n;while (r - l > 1) {int mid = (l + r) >> 1;if (Judge(mid)) l = mid;else r = mid;}if (Judge(r)) cout << r;else cout << l;return 0;}

0 0
原创粉丝点击