Cod sursa(job #556556)

Utilizator boti12botiGal Botond boti12boti Data 16 martie 2011 10:45:45
Problema Cuplaj maxim in graf bipartit Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 1.83 kb
type
     rec=record
     a,b:0..1; end;
     mat=array[1..10000,1..10000] of rec;
     vek=array[1..50000] of longint;
     vak=array[1..50000] of boolean;
var x:mat; f:text; i,n,m,a1,n1,b1,c,max,e,min,w,s,j:longint; cs,os,v:vek; jr:vak;  t:boolean;
procedure df(k:longint);
  var i:longint;
  begin
    if (k<n)and(cs[c]<>n) then begin
    for i:=1 to n do
      if ((x[k,i].a-x[k,i].b>0)or(x[i,k].b<>0))and(not jr[i]) then begin
      t:=true; inc(c); cs[c]:=i; jr[i]:=true; os[i]:=k; jr[i]:=true; end;
     j:=j+1;
      if cs[j]<>0 then df(cs[j]);
    end;  end;
begin
  assign(f,'cuplaj.in');
  reset(f);
  readln(f,n,m,e);
  for i:=1 to e do begin
    readln(f,a1,b1);
    x[a1+1,b1+n+1].a:=1;
    end;
  close(f);
  max:=0;
  for i:=2 to n+1 do
    x[1,i].a:=1;
  for i:=n+2 to m+n+1 do
    x[i,n+m+2].a:=1;
    n1:=n;
  n:=n+m+2;
  c:=1;
  repeat
  for i:=1 to c do
   cs[i]:=0;
  for i:=1 to n do
    os[i]:=0;
  for i:=1 to n do
  jr[i]:=false;
  t:=false;
  c:=1; cs[c]:=1;
  jr[1]:=true;
  j:=1;
  df(1);
  if t and (cs[c]=n) then begin
  t:=true;
  i:=n;
  w:=1;
  v[w]:=i;
  repeat
  i:=os[i];
    inc(w); v[w]:=i;
  until i=1;
  min:=1;
  {for i:=w downto 2 do
  if x[v[i],v[i-1]].a<>0 then begin if x[v[i],v[i-1]].a-x[v[i],v[i-1]].b<min then min:=x[v[i],v[i-1]].a-x[v[i],v[i-1]].b;  end
  else begin if x[v[i-1],v[i]].b<min then min:=x[v[i-1],v[i]].b; end; }
  for i:=w downto 2 do
   if x[v[i],v[i-1]].a<>0 then x[v[i],v[i-1]].b:=x[v[i],v[i-1]].b+min
   else x[v[i-1],v[i]].b:=x[v[i-1],v[i]].b-min;
    end
    else t:=false;
  until not t;
  s:=0;
  for i:=1 to n do
    s:=s+x[i,n].b;

  assign(f,'cuplaj.out');
  rewrite(f);
  writeln(f,s);
   for i:=2 to n1+m+1 do
    for j:=2 to n1+m+1 do
      if (x[i,j].b=1)and(x[i,j].a=1) then writeln(f,i-1,' ',j-n1-1);
  close(f);
end.