Pagini recente » Cod sursa (job #1379403) | Cod sursa (job #2866952) | Cod sursa (job #1322732) | Cod sursa (job #2326403) | Cod sursa (job #31150)
Cod sursa(job #31150)
#include<stdio.h>
int a[250001],s[250001],n,m,q;
int main()
{freopen("stramosi.in","r",stdin);
freopen("stramosi.out","w",stdout);
scanf("%d %d",&n,&m);
q=1;
for(;q<=n;q++)
scanf("%d",&a[q]);
for(q=1;q<=n;q++)
s[q]=a[a[a[a[a[q]]]]];
for(;m;m--)
{scanf("%d %d",&q,&n);
for(;n/5&&q;n=n-5,q=s[q]);
if(n){ q=a[q];n--;}
if(n) q=a[q];
printf("%d\n",q);}
fclose(stdout);
return 0;}