题意:n个男孩和n个女孩跳舞,每次跳舞恰好配成n对,每个男孩只会和一个女孩跳一支舞,有些男孩女孩相互喜欢有些则相互不喜欢,每个男孩只愿意最多和m个不喜欢的女孩跳舞,女孩也一样。给出相关信息,求最多能跳几支舞
据说学长的贪心被hack掉了...
因为每个人有喜欢和不喜欢两种,所以把每个人拆成两个点,一个代表喜欢,一个代表不喜欢。
如果男孩i和女孩j互相喜欢,则连接(i,j,1);如果不互相喜欢则连接(i',j'1)
二分答案,即每个人跳mid支舞蹈,即两个拆了的点合一起流量是mid
限流:源s和每个男孩连(s,i,mid),同理每个女孩和汇t连(j,t,mid)
不喜欢的限制:每个男孩自己连(i,i',m),同理每个女孩自己连(j',j,m)
每次跑最大流
流满即最大流为mid*n则代表mid作为答案可行(即可以跳mid支舞),否则不可行
var
n,m,l,r,mid,ll :longint;
ss,st,ans :longint;
i,j :longint;
s :string;
map :array[0..55,0..55] of char;
other,len,pre :array[0..6010] of longint;
last,que,dis :array[0..210] of longint;
function min(a,b:longint):longint;
begin
if atl) do
begin
h:=h mod 205+1;
cur:=que[h];
q:=last[cur];
while (q<>0) do
begin
p:=other[q];
if (len[q]>0) and (dis[p]=0) then
begin
dis[p]:=dis[cur]+1;
tl:=tl mod 205+1;
que[tl]:=p;
if p=st then exit(true);
end;
q:=pre[q];
end;
end;
exit(false);
end;
function dinic(x,flow:longint):longint;
var
tt,rest,p,q:longint;
begin
if x=st then exit(flow);
rest:=flow;
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if (dis[p]=dis[x]+1) and (len[q]>0) and (rest>0) then
begin
tt:=dinic(p,min(rest,len[q]));
dec(len[q],tt);
dec(rest,tt);
inc(len[q xor 1],tt);
if rest=0 then exit(flow);
end;
q:=pre[q];
end;
if rest=flow then dis[x]:=0;
exit(flow-rest);
end;
function check(x:longint):boolean;
var
i,j:longint;
ans:longint;
begin
ll:=1; ans:=0;
fillchar(last,sizeof(last),0);
for i:=1 to n do
begin
connect(ss,i*2-1,x);
connect(i*2-1,ss,0);
connect(i*2-1+2*n,st,x);
connect(st,2*n+i*2-1,0);
end;
for i:=1 to n do
begin
connect(i*2-1,i*2,m);
connect(i*2,i*2-1,0);
connect(2*n+i*2,2*n+i*2-1,m);
connect(2*n+i*2-1,2*n+i*2,0);
end;
for i:=1 to n do
for j:=1 to n do
if (map[i,j]='Y') then
begin
connect(i*2-1,2*n+j*2-1,1);
connect(2*n+j*2-1,i*2-1,0);
end else
begin
connect(i*2,2*n+j*2,1);
connect(2*n+j*2,i*2,0);
end;
while bfs do inc(ans,dinic(ss,maxlongint div 10));
if ans=mid*n then exit(true) else exit(false);
end;
begin
readln(n,m);
ss:=4*n+1; st:=ss+1;
for i:=1 to n do
begin
readln(s);
for j:=1 to n do map[i,j]:=s[j];
end;
//
l:=0; r:=n; ans:=0;
while (l<=r) do
begin
mid:=(l+r) div 2;
if check(mid) then
begin
if mid>ans then ans:=mid;
l:=mid+1;
end else r:=mid-1;
end;
writeln(ans);
end.
——by Eirlys