解题报告 P3153 [CQOI2009]跳舞
题面:P3153 [CQOI2009]跳舞
读完题觉得不像动态规划。
数据规模这么小,而且明显是匹配的问题,所以要用 AC自动机 网络流。
不涉及代价问题,仅仅是简单的匹配,所以应该不涉及费用流,甚至不需要割。
所以大概的思路就是建模后跑最大流。
那么得考虑如何建模了。
先找找对应关系:
匹配问题可以用最终网络里的总流量表示匹配数。
“不会与女孩跳两首(及以上)舞曲” 以及 “最多和
而喜欢和不喜欢可以通过拆点完成。
可是我们依然没有看到最大流的影子。。。
所以再次观察题面,题目让我们求一个舞曲数目的极限,也并不需要求出方案。所以考虑二分(因为二分就是求极限的),我们发现,舞曲数目是满足单调性的,所以果断考虑二分答案。
如果我们有了一个答案
每个人跳的舞曲数目是相同的,所以最终对于每个男孩女孩,流经他们的流量是相同的。这点可以用来限制,同样可以判断。
所以从源点向每个男孩连容量为
现在考虑,如何处理喜欢和不喜欢的限制?
显然一个人是男是女没关系,只要区别开异性就可以了。
所以我们令流量从男孩流向女孩。
把每个男孩 (
女孩 (
如何处理喜欢?
若是相互喜欢的男孩女孩,就从男孩
若是相互不喜欢的男孩女孩,就从男孩
那么怎么处理不喜欢?
从
最后跑最大流即可。
时间复杂度
其中
代码(
#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
namespace Qza {
const int inf=2147483647;
int n,k;
char graph[55];
int S,T,maxflow;
int dep[205], cur[205];
int cnt,cnt2;
int en[205], nex[42025], to[42025], W[42025], w[42025]; // 空间一定要开够。。。
queue <int> Q;
char buf[10000000], *buf_S, *buf_T;
void init(int x)
{
for(int i=0;i<=cnt;++i) {
w[i]=W[i];
}
for(int i=cnt+1;i<=cnt2;i+=4) {
w[i]=w[i+2]=x; // S->bi , gi->T;
w[i+1]=w[i+3]=0;
}
}
bool bfs(int &s,int &t)
{
memset(dep,0x3f,sizeof(dep));
dep[s]=0;
while(!Q.empty()) Q.pop();
Q.push(s);
for(int i=1;i<=(n<<2|2);++i) cur[i]=en[i];
register int x;
while(!Q.empty()) {
x=Q.front();
Q.pop();
for(int p=en[x];~p;p=nex[p]) {
if(dep[to[p]]>250 && w[p]) {
dep[to[p]]=dep[x]+1;
if(dep[to[p]]<dep[t]) Q.push(to[p]);
}
}
}
if(dep[t]<250) return true;
return false;
}
int dfs(int x, int &t,int limit)
{
if(!limit || x==t) return limit; // 不要写成 if(!limit && x==t)
int sflow=0, flow=0;
for(int &p=cur[x];~p;p=nex[p]) {
if(dep[to[p]]==dep[x]+1 && (flow=dfs(to[p],t,min(limit,w[p])))) {
sflow+=flow;
limit-=flow;
w[p]-=flow;
w[p^1]+=flow;
if(!limit) break;
}
}
return sflow;
}
void Dinic(int &s,int &t)
{
while(bfs(s,t)) {
maxflow+=dfs(s,t,inf);
}
}
bool judge(int x)
{
maxflow=0;
init(x);
Dinic(S,T);
if(maxflow==x*n) return true;
return false;
}
int read()
{
char ch=getchar(); int x=0;
while(ch<'0' || ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
return x;
}
void main()
{
cnt=-1;
memset(en,-1,sizeof(en));
n=read(); k=read();
S=n<<2|1; T=n<<2|2;
for(int i=1;i<=n;++i) {
scanf("%s",graph+1);
for(int j=1;j<=n;++j) {
if(graph[j]=='Y') {
nex[++cnt]=en[i]; to[cnt]=j+n; en[i]=cnt; W[cnt]=1;
nex[++cnt]=en[j+n]; to[cnt]=i; en[j+n]=cnt; W[cnt]=0;
}
else {
nex[++cnt]=en[i+(n<<1)]; to[cnt]=j+(n<<1)+n; en[i+(n<<1)]=cnt; W[cnt]=1;
nex[++cnt]=en[j+(n<<1)+n]; to[cnt]=i+(n<<1); en[j+(n<<1)+n]=cnt; W[cnt]=0;
}
}
nex[++cnt]=en[i]; to[cnt]=i+(n<<1); en[i]=cnt; W[cnt]=k;
nex[++cnt]=en[i+(n<<1)]; to[cnt]=i; en[i+(n<<1)]=cnt; W[cnt]=0;
nex[++cnt]=en[i+(n<<1)+n]; to[cnt]=i+n; en[i+(n<<1)+n]=cnt; W[cnt]=k;
nex[++cnt]=en[i+n]; to[cnt]=i+(n<<1)+n; en[i+n]=cnt; W[cnt]=0;
}
cnt2=cnt;
for(int i=1;i<=n;++i) {
nex[++cnt2]=en[S]; to[cnt2]=i; en[S]=cnt2;
nex[++cnt2]=en[i]; to[cnt2]=S; en[i]=cnt2;
nex[++cnt2]=en[i+n]; to[cnt2]=T; en[i+n]=cnt2;
nex[++cnt2]=en[T]; to[cnt2]=i+n; en[T]=cnt2;
}
int l=1, r=n, mid; // 从1开始不用特判。
while(l<=r) {
mid=(l+r)>>1;
if(judge(mid)) l=mid+1;
else r=mid-1;
}
printf("%d",l-1);
return ;
}
}
int main()
{
Qza::main();
return 0;
}