Pagini recente » Cod sursa (job #3257241) | Cod sursa (job #2981033) | Cod sursa (job #3244894) | Cod sursa (job #3289615) | Cod sursa (job #2401497)
#include <fstream>
#include <unordered_map>
#include <vector>
using namespace std;
#define swap(x, y) ( {auto _x = x; x = y; y = _x;} )
int main()
{
ifstream f{"stramosi.in"};
ofstream g{"stramosi.out"};
int n, q;
f >> n >> q;
vector<int> fathers;
int aux;
for ( int i = 0; i < n; i++ ) {
f >> aux;
aux--;
fathers.push_back(aux);
}
int om, nlea;
for ( int i = 0; i < q; i++ ) {
f >> om >> nlea;
om--;
while ( nlea > 0 && om > -1 ) {
om = fathers[om];
nlea--;
}
g << om + 1 << endl;
}
return 0;
}