Pagini recente » Cod sursa (job #58879) | Cod sursa (job #2270732) | Cod sursa (job #3201066) | Cod sursa (job #995809) | Cod sursa (job #533145)
Cod sursa(job #533145)
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.