Pagini recente » Cod sursa (job #2338640) | Cod sursa (job #1419989) | Cod sursa (job #1569720) | Cod sursa (job #1667645) | Cod sursa (job #2283170)
#include <bits/stdc++.h>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
const int NMax = 250005;
const int LMax = 20;
int v[NMax][LMax];
int main(){
ios::sync_with_stdio(false); in.tie(0); out.tie(0);
int n, m, p, node;
in >> n >> m;
for(int i = 1; i <= n; i++){
in >> v[i][0];
}
for(int j = 1; j < LMax; j++){
for(int i = 1; i <= n; i++){
v[i][j] = v[v[i][j - 1]][j - 1];
}
}
for(int i = 1; i <= m; i++){
in >> node >> p;
for(int i = 0; i < LMax; i++){
if((p >> i) % 2 == 1){
node = v[node][i];
}
}
out << node << "\n";
}
return 0;
}