Pagini recente » Cod sursa (job #1604298) | Cod sursa (job #705458) | Cod sursa (job #2876497) | Cod sursa (job #1902194) | Cod sursa (job #2243265)
#include <iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
vector<int> a;
int N, M, P, Q;
int main()
{
int x;
f >> N >> M;
a.reserve(N + 1);
while(N--)
{
f >> x;
a.push_back(x);
}
while(M--)
{
f >> Q >> P;
int y = a[Q - 1];
for(int i = 1; i < P && y != 0; i++)
y = a[y - 1];
g << y << '\n';
}
return 0;
}