Pagini recente » Cod sursa (job #1696071) | Cod sursa (job #1160593) | Cod sursa (job #660024) | Cod sursa (job #122529) | Cod sursa (job #1663357)
#include <fstream>
#include<vector>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n,m,i,j,t,a,b;
int tata[250001];
vector<int> v[250001];
vector<int> querry[250001];
vector<int> querrypos[250001];
int sol[300001],st[250001];
void dfs(int nod,int level)
{
int i;
st[level]=nod;
for(i=0;i<querry[nod].size();++i)
{
if(level>querry[nod][i])
sol[querrypos[nod][i]]= st[level- querry[nod][i]];
}
for(i=0;i<v[nod].size();++i)
dfs(v[nod][i],level+1);
}
int main()
{
fin>>n>>m;
for(i=1;i<=n;++i)
{
fin>>tata[i];
v[tata[i]].push_back(i);
}
for(i=1;i<=m;++i)
{
fin>>a>>b;
querry[a].push_back(b);
querrypos[a].push_back(i);
}
for(i=1;i<=n;++i)
{
if(tata[i]==0)
dfs(i,1);
}
for(i=1;i<=m;++i)
fout<<sol[i]<<'\n';
return 0;
}