Pagini recente » Cod sursa (job #2204128) | Cod sursa (job #2442491) | Cod sursa (job #359747) | Cod sursa (job #2291275) | Cod sursa (job #2321587)
#include <iostream>
#include <fstream>
#define NMAX 250000
using namespace std;
ifstream fi("stramosi.in");
ofstream fo("stramosi.out");
int N, M;
int table[20][NMAX];
int main()
{
fi >> N >> M;
for(int i = 1; i <= N; ++i)
{
fi >> table[1][i];
}
for(int i = 2; (1 << i) <= N; ++i)
for(int j = 1; j <= N; ++j)
table[i][j] = table[i-1][table[i-1][j]];
for(int i = 1; i <= M; ++i)
{
int node, ancestor;
fi >> node >> ancestor;
int currentAncestor = node;
for(int j = 0; j < 21; ++j)
{
if((1 << j) & ancestor)
currentAncestor = table[j+1][currentAncestor];
}
fo << currentAncestor << "\n";
}
}