Cod sursa(job #582421)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 15 aprilie 2011 12:52:14
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.37 kb
type muchie=^nod;
     nod=record n:longint; a:muchie; end;

var v:array[1..10000] of muchie;
    buf1, buf2:array[1 .. 1 shl 17] of char;
    l, r:array[1..10000] of longint;
    chk:array[1..10000] of boolean;
    nl, nr, m, i, x, y, t:longint;
    ok:boolean;
    p:muchie;
    f, g:text;

procedure cauta(a:muchie; b:longint);
var q:muchie;
  begin
  q:=a;
  while (q<> nil) and (ok=false) do
    begin
    if chk[q^.n] = false then
      begin
      if r[q^.n]=0 then
        begin
        ok:=true;
        l[b]:=q^.n; r[q^.n]:=b;
        end;
      end;
    q:=q^.a;
    end;

  q:=a;
  while (q<> nil) and (ok=false) do
    begin
    if chk[q^.n] = false then
      begin
      chk[q^.n]:=true;
      cauta(v[r[q^.n]], r[q^.n]);
      if ok=true then
        begin
        r[q^.n]:=b; l[b]:=q^.n;
        end;
      end;
    q:=q^.a;
    end;
  end;

begin
assign (f, 'cuplaj.in'); settextbuf (f, buf1); reset (f);
assign (g, 'cuplaj.out'); settextbuf (g, buf2); rewrite (g);

readln (f, nl, nr, m);
for i := 1 to m do
  begin
  readln (f, x, y);
  new(p); p^.n:=y; p^.a:=v[x]; v[x]:=p;
  end;

for i := 1 to nl do
  begin
  ok:=false;
  fillchar(chk, nl+1, false);
  cauta(v[i], i);
  if ok=true then inc(t);
  end;

writeln (g, t);
for i := 1 to nl do if l[i]<>0 then writeln (g, i, ' ', l[i]);

close (f); close (g);
end.