Pagini recente » Cod sursa (job #2879867) | Cod sursa (job #1354150) | Cod sursa (job #1079528) | Cod sursa (job #2274992) | Cod sursa (job #31152)
Cod sursa(job #31152)
#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]);
while(n&&q){ q=a[q];n--;}
printf("%d\n",q);}
fclose(stdout);
return 0;}