Pagini recente » Cod sursa (job #348128) | Cod sursa (job #1476722) | Cod sursa (job #2947680) | Cod sursa (job #2220996) | Cod sursa (job #2506721)
#include <fstream>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
int main() {
ios::sync_with_stdio(0);
in.tie(0);
out.tie(0);
int N, M, anc[250001][18];
in>>N>>M;
for(register int i = 1; i <= N; ++i) {
in>>anc[i][0];
for(register int j = 1; j <= 17; ++j)
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
while(M--) {
int node, k;
in>>node>>k;
for(int i = 0; (1 << i) <= k; ++i)
if((k >> i) & 1)
node = anc[node][i];
out<<node<<"\n";
}
return 0;
}