Listing PERECHI.PAS
Program Perechi_stabile;
const fi='PAIRS.IN';
fo='PAIRS.OUT';
nmax=250;
type
linie=array[1..nmax] of Integer;
plinie=^linie;
tablou=array[1..nmax] of plinie;
var
mpref,fclas:tablou;
fem,barb:linie;
b,n,i,j,k,w,cuplat:Integer;
f:Text; gata:Boolean;
procedure
citire;
begin
Assign(f,fi);
Reset(f);
Readln(f,n);
for i:=1 to n do
begin
New(mpref[i]);
for j:=1 to n do
Read(f,mpref[i]^[j]);
Readln(f)
end;
Readln(f);
for i:=1 to n do
begin
New(fclas[i]);
for j:=1 to n do
begin
Read(f,k);
fclas[i]^[k]:=j
end;
Readln(f)
end;
Close(f)
end;
procedure initializari;
begin
for i:=1 to n do
begin
fem[i]:=0;
barb[i]:=0
end
end;
procedure perechi;
begin
b:=1;
while fem[b]<>0 do b:=b+1;
i:=1;
gata:=false;
repeat
w:=mpref[b]^[i];
if barb[w]<>0
then
begin
if fclas[w]^[b]<fclas[w]^[barb[w]]
then gata:=true
end
else gata:=true;
if not gata then i:=i+1
until gata;
if barb[w]<>0 then fem[barb[w]]:=0
else cuplat:=cuplat+1;
fem[b]:=w; barb[w]:=b
end;
procedure scrie;
begin
Assign(f,fo); Rewrite(f);
for i:=1 to n do Writeln(f,i,' ',fem[i]);
Close(f)
end;
Begin
citire;
initializari;
repeat perechi until cuplat=n;
scrie
End.