Cod sursa(job #116264)

Utilizator juniorOvidiu Rosca junior Data 18 decembrie 2007 10:48:09
Problema Dusman Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.19 kb
var
	fi, fo : text;
  a : array [0..1000] of integer;
  d : array [0..1000,0..1000] of boolean; {relatiile de dusmanie}
  liber : array [1..1000] of boolean;
  n, k, m, l, c, asezari, i : integer;
  gata : boolean;
procedure back (i : integer);
var
   j, l : integer;
begin
	if i = n+1 then
  	begin
    	inc(asezari);
      if asezari = k then
      	begin
        	for l := 1 to n do
          	write(fo, a[l], ' ');
          gata := true
        end
    end
  else
  	begin
    	j := 1;
      while (j <= n) and (not gata) do
      	begin
        	if liber[j] then
          	begin
            	a[i] := j;
              if not d[a[i],a[i-1]] then
              	begin
                	liber[a[i]] := false;
              		back (i+1);
                  liber[a[i]] := true
                end	
            end;
          inc(j)
        end
    end
end;
{------------------------}
begin
	assign(fi,'dusman.in'); reset(fi);
  assign(fo,'dusman.out'); rewrite(fo);
	readln(fi,n,k,m);
  for i := 1 to n do
  	liber[i] := true;
  for i := 1 to m do
  	begin
  		readln(fi, l, c);
      d[l,c] := true; d[c,l] := true;
    end;
  back(1);
  close(fi); close(fo);
end.