Cod sursa(job #31152)

Utilizator razvi9Jurca Razvan razvi9 Data 15 martie 2007 16:30:04
Problema Stramosi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.39 kb
#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;}