Cod sursa(job #31150)

Utilizator razvi9Jurca Razvan razvi9 Data 15 martie 2007 16:29:35
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.41 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]);
  if(n){ q=a[q];n--;}
  if(n) q=a[q];
  printf("%d\n",q);}
 fclose(stdout);
 return 0;}