Mai intai trebuie sa te autentifici.

Cod sursa(job #3604)

Utilizator raula_sanChis Raoul raula_san Data 27 decembrie 2006 09:14:21
Problema Stramosi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include<cstdio>
#define dim 250001

long N, M, A[dim];

void read_s_w()
{
     freopen("stramosi.in", "r", stdin);
     freopen("stramosi.out", "w", stdout);
     scanf("%ld %ld", &N, &M);
     
     long i, Q, P, S, X;
     for(i=1; i<=N; ++i)
              scanf("%ld", A + i);    
     for(i=1; i<=M; ++i)
     {
              scanf("%ld %ld", &Q, &P);
              X = Q; S = 0;
              while(S != P)
              {
                      ++ S;
                      X = A[X];
                      if(!X) break;
                      }
              printf("%ld\n", S!=P?0:X);
              }
}

int main()
{
    read_s_w();
    
    fclose(stdin); fclose(stdout);
    return 0;   
}