Cod sursa(job #41230)

Utilizator Bluedrop_demonPandia Gheorghe Bluedrop_demon Data 28 martie 2007 02:11:24
Problema Stramosi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
// Problema stramosi

#include <stdio.h>
#define MAX         250001

long t[MAX];
long t2[MAX];

int main()
{
    freopen( "stramosi.in" ,"rt", stdin );
             long n, i, m;
             scanf( "%ld %ld", &n, &m );
             for( i=1; i<=n; i++ ) scanf( "%ld", &t[i] );             
             for( i=1; i<=n; i++ ) t2[i] = t[t[i]];
    
    freopen( "stramosi.out" ,"wt", stdout );     
             long p, q;
             while(m > 0)
             {
                     m--;
                     scanf( "%ld %ld", &q, &p );
                     while( (t[q]) && (p>1) )
                     {
                            p-=2;
                            q = t2[q];
                     }
                     if(p) printf("%ld\n", t[q] );
                           else printf("%ld\n", q );
             }
    fclose( stdin );
    fclose( stdout );
    return 0;
}