forked from pankaj443/CODEFORCES
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1006E.cpp
More file actions
47 lines (39 loc) · 662 Bytes
/
1006E.cpp
File metadata and controls
47 lines (39 loc) · 662 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N=1000001;
vector<int>v[N];
int k;
long int visit[N],o[N],p[N],cnt[N];
void dfs(int s){
visit[s]=1;
p[s]=k;
o[k]=s;
k++;
for(int i=0;i<v[s].size();i++){
if(!visit[v[s][i]])
{
dfs(v[s][i]);
}
cnt[s]+= cnt[v[s][i]];
}
}
int main()
{
int n,m,a,b;
cin>>n>>m;
cnt[1]=1;
for(int i=2;i<=n;i++){
int x;
cin>>x;
cnt[i]=1;
v[x].push_back(i);
}
dfs(1);
while(m--)
{
cin>>a>>b;
if(cnt[a]<b)cout<<"-1"<<endl;
else cout<<o[p[a]+b-1]<<endl;
}
return 0;
}