-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
87 lines (68 loc) · 1.63 KB
/
algorithm.cpp
File metadata and controls
87 lines (68 loc) · 1.63 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
typedef std::priority_queue<std::pair<int,int>> prio_q;
int n, m, k;
bool one_safe;
prio_q q_max, q_min;
std::vector<int> prev, temps, parent;
std::vector<bool> leaf, safe, visited;
void testcase() {
std::cin >> n >> m >> k;
one_safe = false;
prev = std::vector<int>(n, -1);
temps = std::vector<int>(n);
parent = std::vector<int>(n);
leaf = std::vector<bool>(n, true);
safe = std::vector<bool>(n, false);
visited = std::vector<bool>(n, false);
for (int i = 0; i < n; i++)
std::cin >> temps[i];
for (int i = 0; i < n - 1; i++) {
int u, v; std::cin >> u >> v;
prev[v] = u;
leaf[u] = false;
}
for (int i = 0; i < n; i++) {
if (leaf[i]) {
int curr = i;
q_max = prio_q();
q_min = prio_q();
for (int j = 0; curr != -1; j++, curr = prev[curr]) {
parent[j] = curr;
q_max.push({temps[curr], j});
q_min.push({-temps[curr], j});
while(q_max.top().second + m <= j)
q_max.pop();
while(q_min.top().second + m <= j)
q_min.pop();
if (j + 1 >= m) {
if (q_max.top().first + q_min.top().first <= k)
one_safe = safe[curr] = true;
if (visited[parent[j + 1 - m]])
break;
visited[parent[j + 1 - m]] = true;
}
}
}
}
if (one_safe) {
for (int i = 0; i < n; i++)
if (safe[i])
std::cout << i << " ";
std::cout << std::endl;
} else
std::cout << "Abort mission" << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}