Codevs 1222 信与信封问题 二分图匹配

首先要保证每一封信都能找到和它匹配的信封,即满足完美匹配

所以如果不满足完美匹配直接输出none

然后我们知道了每封信对应的信封other[i],和每个信封对应的信link[i]

枚举删边,如果我们不让信i与它的匹配信封other[i]相连即不让它两个匹配

如果此时i无法匹配说明i只能与other[i]匹配,输出i和other[i]

注意对每一个信判断完后后对相应数组的复原

var
        n,a,b,ans,t     :longint;
        i               :longint;
        map             :array[0..110,0..110] of boolean;
        vis             :array[0..110] of boolean;
        link,other      :array[0..110] of longint;
        flag            :boolean;
function find(x:longint):boolean;
var
        p:longint;
begin
   for p:=1 to n do
   begin
      if not vis[p] and not map[x,p] then
      begin
         vis[p]:=true;
         if (link[p]=0) or (find(link[p]))  then
         begin
            link[p]:=x;
            other[x]:=p;
            exit(true);
         end;
      end;
   end;
   exit(false);
end;

begin
   read(n);
   read(a,b);
   while (a<>0) and (b<>0) do
   begin
      map[a,b]:=true;
      read(a,b);
   end;

   for i:=1 to n do
   begin
      fillchar(vis,sizeof(vis),false);
      if find(i) then inc(ans);
   end;
   //
   if (ans<>n) then
   begin
      writeln('none');exit;
   end;
   //
   for i:=1 to n do
   begin
      map[i,other[i]]:=true;
      t:=other[i];
      link[other[i]]:=0;
      other[i]:=0;
      fillchar(vis,sizeof(vis),false);
      if not find(i) then
      begin
         writeln(i,' ',t);flag:=true;
      end;
      map[i,t]:=false;
      other[i]:=t;link[t]:=i;
   end;
   //
   if not flag then writeln('none');
end.
——by Eirlys

转载请注明出处=w=



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值