JLOI2010 冠军调查 最小割

时间:2024-05-26 21:33:20
var
b,f:array[..] of longint;
s,t,i,j,n,m,x,y:longint;
l,h:array[..]of longint;
a:array[..,..]of longint;
procedure bfs;
var i,head,tail,x,y:longint;
begin
fillchar(b,sizeof(b),);
fillchar(h,sizeof(h),$7f);
h[t]:=;
head:=; tail:=; b[]:=t;
while head<=tail do
begin
x:=b[head];
for i:= to n do
if (a[i,x]>) and (h[i]>=n) then
begin
inc(tail);
b[tail]:=i;
h[i]:=h[x]+;
end;
inc(head);
end;
end;
function dfs(now,inl:longint):longint;
var i,outl:longint;
begin
if now=t then exit(inl);
dfs:=;
for i:=l[now]+ to n do
if (l[i]<n) and (a[now,i]>) and (h[now]=h[i]+) then
begin
if a[now,i]>inl then outl:=dfs(i,inl)
else outl:=dfs(i,a[now,i]);
inl:=inl-outl;
dfs:=dfs+outl;
a[now,i]:=a[now,i]-outl;
a[i,now]:=a[i,now]+outl;
if inl= then break;
inc(l[now]);
end;
end;
function dinic:longint;
var sum:longint;
begin
sum:=;
while h[s]<n do
begin
fillchar(l,sizeof(l),);
sum:=sum+dfs(s,maxlongint);
bfs;
end;
exit(sum);
end;
begin
readln(n,m);
for i:= to n do read(f[i]);
for i:= to n do
if f[i]= then
a[,i+]:= else a[i+,n+]:=;
for i:= to m do
begin
readln(x,y);
a[x+,y+]:=;
a[y+,x+]:=;
end;
s:=;
t:=n+;
n:=n+;
writeln(dinic);
end.