Pagini recente » Cod sursa (job #2271673) | Cod sursa (job #2471655) | Cod sursa (job #1655786) | Cod sursa (job #50689) | Cod sursa (job #2763170)
#include <bits/stdc++.h>
#define zeros(x) x & (-x)
using namespace std;
const int MAXN = 25 * 1e4 + 65;
const int INF = 1e8;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int father[24][MAXN];
void stramosi(int y, int x){
int ans = y;
for(int i = 0; i <= 23; ++i){
if((1 << i) & x)
ans = father[i][ans];
}
fout << ans << '\n';
}
int main(){
int n, m; fin >> n >> m;
for(int i = 1; i <= n; ++i){
fin >> father[0][i];
}
for(int i = 1; i <= 21; ++i){
for(int j = 1; j <= n; j ++){
father[i][j] = father[i - 1][father[i - 1][j]];
}
}
for(int i = 1; i <= m; ++i){
int x, y; fin >> x >> y;
stramosi(x, y);
}
return 0;
}