妈蛋状态都被狗吃了,已经开始不自觉对着电脑发呆……被几道二分图的题亮瞎了双眼(这么弱可是gdkoi只剩一个月gdoi只剩100+天……!!)

 

wikioi1222信与信封问题

二分图,但是判断两个合集中的某两个点是不是只能连在一起。那么我们就在跑一边最大匹配后直接用是否可以增广来判断。如果可以增广那么这两个点是有其他方式连在一起的,否则这两个点就必须连在一起。具体做法是先去掉这两个点的边,不过那么match数组也要跟着改一下。

var
  map:array[0..200,0..200]of boolean;
  matchx,matchy:array[0..200]of longint;
  chose:array[0..200]of boolean;
  n:longint;


function dfs(x:longint):boolean;
var
  i:longint;
begin
  for i:=1 to n do
    if map[x,i] and chose[i] then begin
      chose[i]:=false;
      if (matchx[i]=0) or dfs(matchx[i]) then begin
        matchx[i]:=x;
        matchy[x]:=i;
        exit(true);
      end;
    end;
  exit(false);
end;

procedure into;
var
  j,k:longint;
begin
  readln(n);
  fillchar(map,sizeof(map),true);
  while true do begin
    readln(j,k);
    if j=0 then break;
    map[j,k]:=false;
  end;
end;


function work:boolean;
var
  ans,i,j:longint;
  flag:boolean;
begin
  for i:=1 to n do begin
    fillchar(chose,sizeof(chose),true);
    if not dfs(i) then exit(false);
  end;
  flag:=false;
  for i:=1 to n do begin
    fillchar(chose,sizeof(chose),true);
    j:=matchy[i];
    map[i,j]:=false;
    matchx[j]:=0;
    matchy[i]:=0;
    if not dfs(i) then begin
      writeln(i,' ',j);
      matchx[j]:=i;
      matchy[i]:=j;
      flag:=true;
    end;
    map[i,j]:=true;
  end;
  exit(flag);
end;

begin
  into;
  if not work then writeln('none');
  readln;
end.
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-08-08
  • 2021-04-10
  • 2022-01-23
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2021-12-07
  • 2021-06-14
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案