Cod sursa(job #358581)

Utilizator prisonbreakMichael Scofield prisonbreak Data 23 octombrie 2009 19:09:49
Problema Stramosi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <cstdio>
#include <vector>
#define DIM 250002

using namespace std;

vector <int> G[DIM];
bool viz[DIM];
int i, N, M, p, k,  a[DIM], q;

int dfs( int x , int z, int c) {
    viz[x]=true;
         for (i=0; i < G[x].size(); i++)
         if ( !viz[ G[x][i] ] )
           {  
              if (z+1==c && G[x][i]) return a[G[x][i]]; 
              else return dfs( G[x][i], z+1, c);
           }
    return 0;
     
    
}
void read()
{
     scanf ("%d %d\n",&N,&M);
     scanf ("%d",&a[1]);
     for (i=2; i<=N; i++) {
         scanf ("%d",&a[i]);
         G[i].push_back(i-1);
         }
     while ( M-- ) {
           scanf ("%d %d",&q,&p);
           printf ("%d\n",dfs(q,0,p));
           memset ( viz, false, sizeof (viz) );
           }
}           
int main()
{
    freopen ("stramosi.in","r",stdin);
    freopen ("stramosi.out","w",stdout);
    read();
    return 0;
}