跳到正文

题解

题解 P3386 【【模板】二分图匹配】

deluxurous约 2 分钟
原文
文章信息与版本

文章信息

发布
2017年2月10日
收录
2025年12月12日
更新
2026年10月6日
篇幅
约 2 分钟
获赞
2
题目
P3386

版本

  1. v1当前

P党水一发

标准Hungary算法

pascal
var
 match:array[0..1001]of longint; //子集B中元素是否被匹配
 used:array[0..1001]of boolean; //子集B中元素所对应的子集A元素
 a:array[0..1001,0..1001]of boolean; //邻接矩阵存边
 i,j,k,n,m,l,ans,t:longint;

function found(x:longint):boolean;  //找增广路
var
 i,j:longint;
begin
 for i:=1 to m do
  if (a[x,i])and(not used[i]) then
   begin
    used[i]:=TRUE;
    if (match[i]=0)or(found(i)) then  //点i未匹配,或者匹配了但是能引出另一条增广路径
     begin
      match[i]:=x;
      exit(TRUE);
     end;
   end;
 exit(FALSE);
end;

begin
 readln(n,m,t);
 for i:=1 to n do
  for j:=1 to m do
   a[i,j]:=FALSE;
 for i:=1 to t do
  begin
   readln(j,k);
   if (j>n)or(k>m) then continue; //应对坑人的数据
   a[j,k]:=TRUE;
  end;
 ans:=0;
 fillchar(match,sizeof(match),0);
 for i:=1 to n do
  begin
   fillchar(used,sizeof(used),FALSE);  //每次都要清零
   if found(i) then ans:=ans+1;  //找到增广路径匹配数加1
  end;
 writeln(ans);
end.

一遍过

虽然速度不是很快

原文
存档评论1 条抓取于