Pagini recente » Cod sursa (job #2770602) | Cod sursa (job #441405) | Cod sursa (job #2159411) | Cod sursa (job #2831659) | Cod sursa (job #533149)
Cod sursa(job #533149)
type vector=array[0..250000] of longint;
var a:vector;
n,m,k,i,j,t,s:longint;
f1,f2:text;
procedure stramos(p:longint);
begin
if p=s then exit
else if a[t]=0 then begin t:=0; exit; end
else begin t:=a[t]; stramos(p+1); end;
end;
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');}
stramos(0);
writeln(f2,t);
end;
close(f1);
close(f2);
end.