Pagini recente » Cod sursa (job #2252679) | Cod sursa (job #1641898) | Cod sursa (job #1443891) | Cod sursa (job #942344) | Cod sursa (job #1032653)
#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;
}