Cod sursa(job #1535427)

Utilizator ili226Vlad Ilie ili226 Data 24 noiembrie 2015 19:12:18
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.38 kb
type nd=^nod;
     nod=record
          val:word;
          next:nd
         end;
var f:text;
    j,x,y,n,m,c,i:word;
    k:longint;
    l,r:array[1..10000]of word;
    p,pp:nd;
    g:array[1..10000]of nd;
    pot,gasit:boolean;
begin
assign(f,'cuplaj1.in');
reset(f);
read(f,n,m,k);
for i:=1 to k do
 begin
  read(f,x,y);
  if (x<>0)and(y<>0)then
   begin
    new(p);
    p^.val:=y;
    p^.next:=g[x];
    g[x]:=p;
   end;
 end;
close(f); c:=0;
for i:=1 to n do
 begin
  p:=g[i];pot:=false;x:=0;
  while (p<>nil)and(not pot) do
   begin
    if r[p^.val]=0 then
     begin x:=p^.val;
           pot:=true
     end;
    p:=p^.next
   end;
  if not pot then
   begin
    p:=g[i];x:=p^.val;gasit:=false;
    while (p<>nil)and(not gasit) do
     begin
      pp:=g[r[p^.val]];
      while (pp<>nil)and(not gasit) do
       begin
        if r[pp^.val]=0 then
         begin
          gasit:=true;
          x:=p^.val;
          y:=pp^.val;
         end;
        pp:=pp^.next
       end;
      p:=p^.next
     end;
    if gasit then
     begin
      inc(c);
      l[r[x]]:=y;
      r[y]:=r[x];
      r[x]:=i;
      l[i]:=x;
     end;
   end
             else
   begin
    inc(c);
    l[i]:=x;
    r[x]:=i;
   end;
 end;
assign(f,'cuplaj.out');
rewrite(f);
writeln(f,c);
for i:=1 to n do
 if l[i]<>0 then
  writeln(f,i,' ',l[i]);
close(f);
end.