Cod sursa(job #428208)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 28 martie 2010 23:41:15
Problema Cuplaj maxim in graf bipartit Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 0.95 kb
var a:array[1..100,1..100] of boolean;
    f,g:text;
    fol:array[1..10000] of boolean;
    dr,st:array[1..100] of longint;
    m,x,j,y,i,nr,n,p:longint;
function cupleaza(nod:longint):boolean;
 var i:longint;
 begin
  if fol[nod] then cupleaza:=false
  else begin
   fol[nod]:=true; cupleaza:=false;
   for i:=1 to p do
    if a[nod,i] then
     if (dr[i]=0) or (cupleaza(dr[i])) then begin
      st[nod]:=i; dr[i]:=nod;
      cupleaza:=true;
      break;
     end;
  end;
 end;

begin
 assign(f,'cuplaj.in'); reset(f);
 assign(g,'cuplaj.out'); rewrite(g);
 read(f,n,p,m);
 for i:=1 to m do begin
  read(f,x,y);
  a[x,y]:=true;
 end;
 nr:=0;
 for i:=1 to n do
  if st[i]=0 then begin
   if cupleaza(i) then nr:=nr+1
   else begin
    for j:=1 to n do
        fol[j]:=false;
    if cupleaza(i) then
     nr:=nr+1;
   end;
  end;
 writeln(g,nr);
 for i:=1 to n do
  if st[i]<>0 then
   writeln(g,i,' ',st[i]);
 close(g);
end.