Pagini recente » Cod sursa (job #1817687) | Cod sursa (job #2539197) | Cod sursa (job #439221) | Cod sursa (job #2061537) | Cod sursa (job #1385522)
#include<vector>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main() {
ifstream fin ("stramosi.in");
ofstream fout("stramosi.out");
int N,M;
fin >> N >> M;
vector<int> ancestors; ancestors.push_back(0);
vector<pair<int, int> > tree; tree.push_back(make_pair(0,0));
for(int i = 1; i <= N; ++i) {
int x; fin >> x;
ancestors.push_back(x);
}
for(int i = 1; i <= N; ++i) {
if(ancestors[i] == 0) {
tree.push_back(make_pair(0,0));
}
else {
tree.push_back(make_pair(ancestors[i], ancestors[ancestors[i]]));
}
}
/*
cout << "tree:";
for(int i = 0; i < N; ++i)
*/
for(int i = 0; i < M; ++i) {
int P,Q; fin >> Q >> P;
//--P;
int anc = Q;
// cout << "Q:" << Q << " P:" << P << ": ";
while(P > 0) {
if(P >= 2) {
anc = tree[anc].second;
P -= 2;
}
else {
anc = tree[anc].first;
--P;
}
// cout << anc << " ";
}
// cout << "\n";
fout << anc << "\n";
}
return 0;
}