Pagini recente » Cod sursa (job #2776674) | Monitorul de evaluare | Cod sursa (job #3303165) | Cod sursa (job #1233534) | Cod sursa (job #3323187)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, m, x, y, a[250003], str;
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i;
fin >> n >> m;
for (i = 1; i <= n; i++)
fin >> a[i];
for (i = 1; i <= m; i++)
{
fin >> x >> y;
str = a[x];
y--;
while (y != 0 && str != 0)
{
y--;
str = a[str];
}
fout << str << "\n";
}
fout.close();
return 0;
}