Cod sursa(job #1032653)

Utilizator morlockRadu Tatomir morlock Data 15 noiembrie 2013 21:35:01
Problema Stramosi Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.52 kb
#include <iostream>
#include <fstream>
#define nmax 250005
using namespace std;

ifstream in("stramosi.in");
ofstream out("stramosi.out");

long N, M, t[nmax], P, Q;

long stramos( long nr, long nod )
{
    if ( !nr ) return nod;
    if ( !nod ) return 0;

    return stramos(nr-1, t[nod]);
}

int main()
{
    in >> N >> M;
    for ( long i=1; i<=N; ++i )
        in >> t[i];

    for ( long i=1; i<=M; ++i )
    {
        in >> Q >> P;
        out << stramos(P-1, t[Q]) << '\n';
    }


return 0;
}