USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)

题目:有n头奶牛,每头奶牛都只愿意在某些牛栏里产奶。问最多能使多少头奶牛产奶。

分析:赤裸裸的二分图最大匹配,用匈牙利算法就直接秒了。

下面附程序:

var
  n,m,i,j,ans:longint;
  s,link:array[1..200] of longint;
  a:array[1..200,1..200] of longint;
  v:array[1..200] of boolean;

function find(x:longint):boolean;
var
  p,i:longint;
begin
  find:=false;
  for i:=1 to s[x] do
    if v[a[x,i]] then
    begin
      v[a[x,i]]:=false;
      p:=link[a[x,i]];
      link[a[x,i]]:=x;
      if (p=0)or(find(p)) then exit(true);
      link[a[x,i]]:=p;
    end;
end;

begin
  readln(n,m);
  for i:=1 to n do
  begin
    read(s[i]);
    for j:=1 to s[i] do
      read(a[i,j]);
  end;
  for i:=1 to n do
  begin
    fillchar(v,sizeof(v),true);
    if find(i) then inc(ans);
  end;
  writeln(ans);
end.


你可能感兴趣的:(USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配))