-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_york.cpp
143 lines (116 loc) · 3.35 KB
/
new_york.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// new_york.cpp
// Algorithms Lab
//
// Created by Jonas Gessner
// Copyright © 2019 Jonas Gessner. All rights reserved.
//
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> graph;
typedef boost::graph_traits<graph>::vertex_descriptor vertex_desc;
typedef graph::out_edge_iterator out_edge_it;
using namespace std;
typedef struct {
// int start;
int maxTemp;
int minTemp;
int length;
} Trip;
bool dfs(const int u, const graph &G, const vector<int> &temps, const int m, const int k, Trip *trip, const vector<Trip> &foundTrips) {
trip->length++;
if (trip->length == 1) {
trip->maxTemp = temps.at(u);
trip->minTemp = temps.at(u);
} else {
trip->maxTemp = max(trip->maxTemp, temps.at(u));
trip->minTemp = min(trip->minTemp, temps.at(u));
}
if (trip->maxTemp - trip->minTemp > k) {
return false;
}
const Trip &found = foundTrips.at(u);
if (found.length != -1) {
if (found.length == -2) { // Heavy misuse of struct members incoming
if (found.minTemp <= m - trip->length) { // NOT actually the min temp
trip->length = found.minTemp + trip->length; // This is all the information about how long this trip can be that we have.
return false;
}
} else if (max(trip->maxTemp, found.maxTemp) - min(trip->minTemp, found.minTemp) <= k) {
trip->maxTemp = max(trip->maxTemp, found.maxTemp);
trip->minTemp = min(trip->minTemp, found.minTemp);
trip->length = m;
return true;
}
}
if (trip->length == m) {
return true;
}
Trip longestNo = *trip;
out_edge_it ebeg, eend;
for (boost::tie(ebeg, eend) = boost::out_edges(u, G); ebeg != eend; ++ebeg) {
const int v = boost::target(*ebeg, G);
Trip cpy = *trip;
if (dfs(v, G, temps, m, k, &cpy, foundTrips)) {
*trip = cpy;
return true;
} else if (cpy.length > longestNo.length) {
longestNo = cpy;
}
}
*trip = longestNo;
return false;
}
void testcase() {
int n, m, k;
cin >> n >> m >> k;
vector<int> temps(n);
for (int i = 0; i < n; i++) {
cin >> temps.at(i);
}
graph G(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
boost::add_edge(u, v, G);
}
Trip no;
no.length = -1;
vector<Trip> foundTrips(n, no);
bool any = false;
for (int i = 0; i < n; i++) {
Trip trip;
trip.length = 0;
if (dfs(i, G, temps, m, k, &trip, foundTrips)) {
cout << i << " ";
any = true;
foundTrips.at(i) = trip;
} else {
Trip t;
t.length = -2;
t.minTemp = trip.length;
foundTrips.at(i) = t;
}
}
if (!any) {
cout << "Abort mission\n";
} else {
cout << "\n";
}
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}