-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
70 lines (61 loc) · 1.09 KB
/
sol.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 2e5 + 1;
vector<int> adj[MAX_N];
vector<int> vec;
bool vis[MAX_N];
int first[MAX_N], last[MAX_N];
void dfs(int u, int& node) {
vis[u] = true;
first[u] = node;
node++;
vec.push_back(u);
for (int& v : adj[u]) {
if (!vis[v]) {
dfs(v, node);
}
}
last[u] = node;
}
void test_case(int& tc) {
int n, q;
cin >> n >> q;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
adj[p-1].push_back(i);
}
int node = 0;
dfs(0, node);
// for (int u = 0; u < n; u++) {
// if (first[u] != last[u]) {
// cout << "subtree of node " << u + 1 << " = ";
// for (int i = first[u]; i < last[u]; i++) {
// cout << vec[i] + 1 << " ";
// }
// cout << "\n";
// }
// }
// cout << "\n";
for (int i = 0; i < q; i++) {
int u, k;
cin >> u >> k;
--u; --k;
if (first[u] + k < last[u]) {
cout << vec[first[u]+k] + 1;
} else {
cout << -1;
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
test_case(tc);
}
}