Pagini recente » Cod sursa (job #1788840) | Cod sursa (job #1314487) | Cod sursa (job #7675) | Cod sursa (job #2445175) | Cod sursa (job #2923329)
#include <fstream>
using namespace std;
ifstream cin ("stramosi.in");
ofstream cout ("stramosi.out");
int dp[250002][20], n, parent[350002], LOG, m, x, y;
void bl(){
for(int i = 1; i <= n; i++){
dp[i][0] = parent[i];
for(int j = 1; j < LOG; j++)
dp[i][j] = dp[ dp[i][j-1] ][j-1];
}
}
void yes(int x, int y){
for(int j = LOG - 1; j >= 0; j--)
if(y >= (1 << j)){
x = dp[x][j];
y -= 1 << j;}
cout << x << '\n';
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> parent[i];
while ((1 << LOG) <= n) LOG++;
bl();
/* for(int i = 0; i < n; i++, cout << '\n')
for(int j = 0; j < LOG; j++)
cout << dp[i][j] << " ";
cout << endl;
*/
for(int i = 1; i <= m; i++){
cin >> x >> y;
yes(x, y);
}
}