Poj1274 The Perfect Stall 2012-08-27
Program poj1274;
var n,m,s,t,flow:longint;
map:array[1..500,1..500]of longint;
cur,dis:array[1..500]of longint;
vh:array[0..500]of longint;
Procedure init;
var i,j,k,l:longint;
begin
readln(n,m);
fillchar(map,sizeof(map),0);
for i:=1 to n do
begin
read(k);
for j:=1 to k do
begin
read(l);
map[i,l+n]:=1;
end;
readln;
end;
s:=n+m+1;t:=n+m+2;
for i:=1 to n do map[s,i]:=1;
for i:=n+1 to m+n do map[i,t]:=1;
end;
Function min(a,b:longint):longint;
Begin
if a<b then min:=a else min:=b;
end;
Function aug(x,nf:longint):longint;
var i,j,d,l,minh,ins:longint;
Begin
if x=t then exit(nf);
l:=nf;
for i:=cur[x] to t do
if (map[x,i]>0)and(dis[x]=dis[i]+1) then
begin
cur[x]:=i;
d:=aug(i,min(l,map[x,i]));
dec(map[x,i],d);
inc(map[i,x],d);
dec(l,d);
if (dis[s]=m+n+2)or(l=0) then exit(nf-l);
end;
if l=nf then
begin
minh:=m*2+2;
for i:=1 to t do
if (map[x,i]>0)and(dis[i]<minh) then
begin
minh:=dis[i];
ins:=i;
end;
cur[x]:=ins;
dec(vh[dis[x]]);
if vh[dis[x]]=0 then dis[s]:=m+n+2;
dis[x]:=minh+1;
inc(vh[dis[x]]);
end;
aug:=nf-l;
end;
Procedure main;
var i,j,k:longint;
begin
fillchar(vh,sizeof(vh),0);
vh[0]:=n+m+2;
fillchar(dis,sizeof(dis),0);
for i:=1 to t do cur[i]:=1;
flow:=0;
while dis[s]<n+m+2 do inc(flow,aug(s,maxint));
writeln(flow);
end;
Begin
assign(input,'input.in');reset(input);
assign(output,'output.out');rewrite(output);
while not(eof) do
begin
init;
main;
end;
close(input);close(output);
End.