Pagini recente » Cod sursa (job #2645390) | Cod sursa (job #2117797) | Cod sursa (job #135634) | Cod sursa (job #334802) | Cod sursa (job #526495)
Cod sursa(job #526495)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int N, M;
int pos[250002];
vector<int> V[250002], Qs[250002];
pair<int, int> Q[300002];
vector<int> now, rem;
int x, useit, nouse;
void Dfs()
{
now.push_back(x);
for (nouse = 0; nouse < Qs[x].size(); ++nouse)
{
if (Qs[x][nouse] > now.size() - 1)
Qs[x][nouse] = 0;
else
Qs[x][nouse] = now[now.size() - 1 - Qs[x][nouse]];
}
for (useit = 0; useit < V[x].size(); ++useit)
{
rem.push_back(useit);
x = V[x][useit];
Dfs();
useit = rem.back();
rem.pop_back();
x = now.back();
}
now.pop_back();
}
int main()
{
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
fin >> N >> M;
for (int i = 1, aux; i <= N; ++i)
{
fin >> aux;
V[aux].push_back(i);
}
for (int i = 1; i <= M; ++i)
{
fin >> Q[i].first >> Q[i].second;
Qs[Q[i].first].push_back(Q[i].second);
}
Dfs();
for (int i = 1; i <= M; ++i)
{
fout << Qs[Q[i].first][pos[Q[i].first]] << '\n';
++pos[Q[i].first];
}
fin.close();
fout.close();
}