#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int INF = (int)1e9;
struct MinMaxMerge {
map<int, int> dist;
int lazy_weight, lazy_edges;
MinMaxMerge() : lazy_weight(0), lazy_edges(0) {
dist.clear();
}
};
struct Solver {
Solver(int k_, const vector<vector<pair<int, int>>> &adj_) :
n(adj_.size()), k(k_), adj(adj_) {}
int n, k, ans = INF;
vector<vector<pair<int, int>>> adj;
void Merge(MinMaxMerge &curr, MinMaxMerge &him) {
if (curr.dist.size() < him.dist.size()) {
swap(curr, him);
}
for (auto &p : him.dist) {
int real_weight = p.first + him.lazy_weight;
int real_edges = p.second + him.lazy_edges;
int search = k - real_weight - curr.lazy_weight;
if (curr.dist.count(search)) {
ans = min(ans, curr.dist[search] + curr.lazy_edges + real_edges);
}
}
for (auto &p : him.dist) {
int dist = p.first + him.lazy_weight - curr.lazy_weight;
int edges = p.second + him.lazy_edges - curr.lazy_edges;
if (curr.dist.count(dist)) {
curr.dist[dist] = min(curr.dist[dist], edges);
} else {
curr.dist[dist] = edges;
}
}
}
MinMaxMerge DFS(int node, int parent) {
MinMaxMerge curr;
curr.dist[0] = 0;
for (auto &p : adj[node]) if (p.first != parent) {
auto him = DFS(p.first, node);
him.lazy_weight += p.second;
++him.lazy_edges;
Merge(curr, him);
}
return curr;
}
int Solve() {
DFS(0, -1);
return ans == INF ? -1 : ans;
}
};
int best_path(int n, int k, int H[][2], int L[]) {
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n - 1; ++i) {
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
Solver solver(k, adj);
return solver.Solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
4 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
504 KB |
Output is correct |
25 |
Correct |
4 ms |
504 KB |
Output is correct |
26 |
Correct |
4 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
3 ms |
508 KB |
Output is correct |
29 |
Correct |
4 ms |
504 KB |
Output is correct |
30 |
Correct |
4 ms |
508 KB |
Output is correct |
31 |
Correct |
3 ms |
508 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
4 ms |
504 KB |
Output is correct |
34 |
Correct |
3 ms |
504 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
532 KB |
Output is correct |
37 |
Correct |
3 ms |
504 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
161 ms |
14628 KB |
Output is correct |
20 |
Correct |
147 ms |
14628 KB |
Output is correct |
21 |
Correct |
146 ms |
14728 KB |
Output is correct |
22 |
Correct |
159 ms |
14712 KB |
Output is correct |
23 |
Correct |
196 ms |
14968 KB |
Output is correct |
24 |
Correct |
164 ms |
15472 KB |
Output is correct |
25 |
Correct |
148 ms |
29780 KB |
Output is correct |
26 |
Correct |
96 ms |
43500 KB |
Output is correct |
27 |
Correct |
262 ms |
29904 KB |
Output is correct |
28 |
Correct |
472 ms |
87088 KB |
Output is correct |
29 |
Correct |
508 ms |
84088 KB |
Output is correct |
30 |
Correct |
259 ms |
29816 KB |
Output is correct |
31 |
Correct |
263 ms |
29944 KB |
Output is correct |
32 |
Correct |
305 ms |
29944 KB |
Output is correct |
33 |
Correct |
325 ms |
28560 KB |
Output is correct |
34 |
Correct |
492 ms |
39036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
4 ms |
504 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
504 KB |
Output is correct |
25 |
Correct |
4 ms |
504 KB |
Output is correct |
26 |
Correct |
4 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
3 ms |
508 KB |
Output is correct |
29 |
Correct |
4 ms |
504 KB |
Output is correct |
30 |
Correct |
4 ms |
508 KB |
Output is correct |
31 |
Correct |
3 ms |
508 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
4 ms |
504 KB |
Output is correct |
34 |
Correct |
3 ms |
504 KB |
Output is correct |
35 |
Correct |
3 ms |
504 KB |
Output is correct |
36 |
Correct |
3 ms |
532 KB |
Output is correct |
37 |
Correct |
3 ms |
504 KB |
Output is correct |
38 |
Correct |
3 ms |
504 KB |
Output is correct |
39 |
Correct |
161 ms |
14628 KB |
Output is correct |
40 |
Correct |
147 ms |
14628 KB |
Output is correct |
41 |
Correct |
146 ms |
14728 KB |
Output is correct |
42 |
Correct |
159 ms |
14712 KB |
Output is correct |
43 |
Correct |
196 ms |
14968 KB |
Output is correct |
44 |
Correct |
164 ms |
15472 KB |
Output is correct |
45 |
Correct |
148 ms |
29780 KB |
Output is correct |
46 |
Correct |
96 ms |
43500 KB |
Output is correct |
47 |
Correct |
262 ms |
29904 KB |
Output is correct |
48 |
Correct |
472 ms |
87088 KB |
Output is correct |
49 |
Correct |
508 ms |
84088 KB |
Output is correct |
50 |
Correct |
259 ms |
29816 KB |
Output is correct |
51 |
Correct |
263 ms |
29944 KB |
Output is correct |
52 |
Correct |
305 ms |
29944 KB |
Output is correct |
53 |
Correct |
325 ms |
28560 KB |
Output is correct |
54 |
Correct |
492 ms |
39036 KB |
Output is correct |
55 |
Correct |
20 ms |
2168 KB |
Output is correct |
56 |
Correct |
12 ms |
1912 KB |
Output is correct |
57 |
Correct |
82 ms |
14644 KB |
Output is correct |
58 |
Correct |
70 ms |
15040 KB |
Output is correct |
59 |
Correct |
153 ms |
43512 KB |
Output is correct |
60 |
Correct |
459 ms |
84920 KB |
Output is correct |
61 |
Correct |
300 ms |
31224 KB |
Output is correct |
62 |
Correct |
251 ms |
29836 KB |
Output is correct |
63 |
Correct |
296 ms |
29944 KB |
Output is correct |
64 |
Correct |
593 ms |
36216 KB |
Output is correct |
65 |
Correct |
588 ms |
39672 KB |
Output is correct |
66 |
Correct |
462 ms |
77432 KB |
Output is correct |
67 |
Correct |
235 ms |
31208 KB |
Output is correct |
68 |
Correct |
453 ms |
38392 KB |
Output is correct |
69 |
Correct |
485 ms |
42020 KB |
Output is correct |
70 |
Correct |
413 ms |
36836 KB |
Output is correct |