Pagini recente » Cod sursa (job #2479028) | Cod sursa (job #826719) | Cod sursa (job #3243336) | Cod sursa (job #673918) | Cod sursa (job #2910063)
#include<iostream>
#include<fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n,m;
int LOG;
int up[250001][25];
void BuildMatrix()
{
for(int i=1;i<=n;i++)
{
f>>up[i][1];
for(int j=2;j<=LOG;j++)
{
up[i][j] = up[up[i][j-1]][j-1];
}
}
}
void GetKthAncestor(int node, int k)
{
for(int i=LOG;i>=1;i--)
{
if(k&(1<<(i-1)))
node = up[node][i];
}
g<<node<<"\n";
}
int main()
{
f>>n>>m;
while(1<<LOG<=n)
LOG++;
BuildMatrix();
for(int i=1;i<=m;i++)
{
int x,y;
f>>x>>y;
GetKthAncestor(x,y);
}
}