Cod sursa(job #533145)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 13 februarie 2011 11:42:00
Problema Stramosi Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.78 kb
type    vector=array[0..250000] of longint;
var     a:vector;
        n,m,k,i,j,t,s:longint;
        f1,f2:text;
begin
  assign(f1,'stramosi.in');
  assign(f2,'stramosi.out');
  reset(f1);
  rewrite(f2);
  readln(f1,n,m);
    for i:=1 to n do
        read(f1,a[i]);
{    for i:=1 to n do
      begin
        j:=1;
        t:=a[i,1];
        while a[t,1]<>0 do
        begin
          inc(j);
          a[i,j]:=a[t,1];
          t:=a[t,1];
        end;
      end;    }
    for i:=1 to m do
      begin
        read(f1,t,s);
        j:=1;
        t:=a[t];
        while ((a[t]<>0) and (j<s)) do
        begin
          inc(j);
          t:=a[t];
        end;
        if j=s then writeln(f2,t) else writeln(f2,'0');
      end;
  close(f1);
  close(f2);
end.