POJ 1274 The Perfect Stall 最大匹配

跟USACO 4.2的一题是一样的,只不过这题有多个数据罢了。

题目:有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
  while not eof do
  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]);
      readln; 
    end;
    fillchar(link,sizeof(link),0);
    ans:=0;
    for i:=1 to n do
    begin
      fillchar(v,sizeof(v),true);
      if find(i) then inc(ans);
    end;
    writeln(ans);
  end;
end.


你可能感兴趣的:(POJ 1274 The Perfect Stall 最大匹配)