poj1469 二分图匹配,匈牙利算法的模型

典型的二分图匹配,用匈牙利算法,(其实可以看作匈牙利算法的模型了)这题的数据范围值得注意。开始写的时候,把学生和课程的顺序乱了,悲剧WA~,改了之后就AC了,程序在时间和空间上都没加优化,1228K,860MS

program course;
type
  node=record
    x,y,next:integer;
  end;
var
  n,m,k,l,i,j,tot,num,c:integer;
  link:array[1..300] of integer;//link[i]=0,顶点i为未盖点,link[i]=j,边j,i为匹配边
  first:array[1..300] of integer;//first[i]表示与i相连的第一条边在g中的下标
  g:array[1..60000] of node;//存二分图的边,g[i].next表示与g[i].x相连的下一条边的下标
  used:array[1..300] of boolean;//记录第i个点有没有用过
function find(s:integer):boolean;
var
  temp:integer;
begin
  find:=true;
  temp:=first[s];
  while temp<>-1 do
  begin
    if used[g[temp].y] then
    begin
      used[g[temp].y]:=false;
      if (link[g[temp].y]=0) or (find(link[g[temp].y])) then
      begin
        link[g[temp].y]:=s;
        exit;
      end;
    end;
    temp:=g[temp].next;
  end;
  find:=false;
end;
begin
  readln(l);
  for k:=1 to l do
  begin
    fillchar(g,sizeof(g),0);
    readln(n,m);
    tot:=0;
    for i:=1 to m do
      first[i]:=-1;
    for i:=1 to n do
    begin
      read(num);
      for j:=1 to num do
      begin
        read(c);
        inc(tot);
        g[tot].x:=c;
        g[tot].y:=i;
        g[tot].next:=first[c];
        first[c]:=tot;
      end;
      readln;
    end;
    fillchar(link,sizeof(link),0);
    for i:=1 to m do
    begin
      fillchar(used,sizeof(used),true);
      find(i);
    end;
    i:=1;
    while link[i]<>0 do inc(i);
    if i<=n then writeln('NO')
      else writeln('YES');
  end;
end.

原文地址:https://www.cnblogs.com/stmq/p/2564390.html