1305: [CQOI2009]dance跳舞

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

[ 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);
			else
				Add(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 DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值