-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.cpp
More file actions
62 lines (52 loc) · 1.11 KB
/
task3.cpp
File metadata and controls
62 lines (52 loc) · 1.11 KB
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Node {
public:
int data;
int parent;
Node() {
data = 0;
parent = 0;
}
Node(int d) {
data = d;
parent = 0;
}
};
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
vector<Node> nodes(n);
for (int i = 0; i < n; i++) {
nodes.at(i) = Node(i + 1);
}
for (int i = 1; i <= n - 1; i++) {
int node, rel;
cin >> node >> rel;
Node* nodeFrom = &nodes.at(node - 1);
Node* nodeTo = &nodes.at(rel - 1);
if (nodes.at(rel - 1).parent != 0) {
nodeFrom->parent = nodeTo->data;
}
else {
nodeTo->parent = nodeFrom->data;
}
}
int q;
cin >> q;
for (int i = 0; i<q; i++) {
int node, k;
cin >> node >> k;
int idx = node - 1;
for (int i = 0; i < k; i++) {
idx = nodes.at(idx).parent - 1;
}
cout << nodes.at(idx).data << endl;
}
return 0;
}