#include "bits/stdc++.h"
#include "race.h"
using namespace std;
const int maxn = 200001;
vector<pair<int,int>> adj[maxn];
int k;
unordered_map<long long,int> *maps[maxn];
void Check (int cur, int x, int y, int& ret, int dep2, int dep) {
if (maps[cur] -> count(k + 2 * dep - x)) {
ret = min(ret, y + (*maps[cur])[k + 2 * dep - x] - 2 * dep2);
}
}
void Insert (int cur, int x, int y) {
if (maps[cur] -> count(x)) (*maps[cur])[x] = min((*maps[cur])[x], y);
else (*maps[cur])[x] = y;
}
int dfs (int cur, int par, int dep, int dep2) { // dep = sum of weights // dep2 = number of edges from root
if ((adj[cur].empty()) || (adj[cur].size() == 1 and cur != 0)) {
maps[cur] = new unordered_map<long long, int>();
maps[cur] -> insert({dep, dep2});
return INT_MAX;
}
int mx = -1;
int ret = INT_MAX;
for (auto [i, j] : adj[cur]) {
if (i != par) {
ret = min(ret, dfs(i, cur, dep + j, dep2 + 1));
if (mx == -1 || maps[i] -> size() > maps[mx] -> size()) mx = i;
}
}
maps[cur] = maps[mx];
for (auto [i, j] : adj[cur]) {
if (i != par and i != mx) {
for (auto [x, y] : *maps[i]) {
Check(cur, x, y, ret, dep2, dep);
}
for (auto [x, y] : *maps[i]) {
Insert(cur, x, y);
}
}
}
Check(cur, dep, dep2, ret, dep2, dep);
Insert(cur, dep, dep2);
return ret;
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for (int i = 0 ; i < N - 1 ; i++) {
auto [u, v] = H[i];
int w = L[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
int ans;
return (ans = dfs(0, 0, 0, 0)) == INT_MAX ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
4 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
4 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5008 KB |
Output is correct |
20 |
Correct |
3 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
5 ms |
5204 KB |
Output is correct |
28 |
Correct |
3 ms |
5240 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5180 KB |
Output is correct |
34 |
Correct |
3 ms |
5148 KB |
Output is correct |
35 |
Correct |
3 ms |
5144 KB |
Output is correct |
36 |
Correct |
3 ms |
5140 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
4 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
82 ms |
25416 KB |
Output is correct |
20 |
Correct |
91 ms |
25372 KB |
Output is correct |
21 |
Correct |
81 ms |
25636 KB |
Output is correct |
22 |
Correct |
83 ms |
26836 KB |
Output is correct |
23 |
Correct |
100 ms |
35500 KB |
Output is correct |
24 |
Correct |
92 ms |
36584 KB |
Output is correct |
25 |
Correct |
60 ms |
19288 KB |
Output is correct |
26 |
Correct |
50 ms |
25588 KB |
Output is correct |
27 |
Correct |
154 ms |
44124 KB |
Output is correct |
28 |
Correct |
177 ms |
55812 KB |
Output is correct |
29 |
Correct |
173 ms |
54384 KB |
Output is correct |
30 |
Correct |
156 ms |
44016 KB |
Output is correct |
31 |
Correct |
171 ms |
44108 KB |
Output is correct |
32 |
Correct |
182 ms |
44160 KB |
Output is correct |
33 |
Correct |
135 ms |
27204 KB |
Output is correct |
34 |
Correct |
223 ms |
50284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
4 ms |
5024 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5008 KB |
Output is correct |
20 |
Correct |
3 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
5 ms |
5204 KB |
Output is correct |
28 |
Correct |
3 ms |
5240 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5180 KB |
Output is correct |
34 |
Correct |
3 ms |
5148 KB |
Output is correct |
35 |
Correct |
3 ms |
5144 KB |
Output is correct |
36 |
Correct |
3 ms |
5140 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
82 ms |
25416 KB |
Output is correct |
40 |
Correct |
91 ms |
25372 KB |
Output is correct |
41 |
Correct |
81 ms |
25636 KB |
Output is correct |
42 |
Correct |
83 ms |
26836 KB |
Output is correct |
43 |
Correct |
100 ms |
35500 KB |
Output is correct |
44 |
Correct |
92 ms |
36584 KB |
Output is correct |
45 |
Correct |
60 ms |
19288 KB |
Output is correct |
46 |
Correct |
50 ms |
25588 KB |
Output is correct |
47 |
Correct |
154 ms |
44124 KB |
Output is correct |
48 |
Correct |
177 ms |
55812 KB |
Output is correct |
49 |
Correct |
173 ms |
54384 KB |
Output is correct |
50 |
Correct |
156 ms |
44016 KB |
Output is correct |
51 |
Correct |
171 ms |
44108 KB |
Output is correct |
52 |
Correct |
182 ms |
44160 KB |
Output is correct |
53 |
Correct |
135 ms |
27204 KB |
Output is correct |
54 |
Correct |
223 ms |
50284 KB |
Output is correct |
55 |
Correct |
11 ms |
7904 KB |
Output is correct |
56 |
Correct |
8 ms |
6812 KB |
Output is correct |
57 |
Correct |
60 ms |
25452 KB |
Output is correct |
58 |
Correct |
56 ms |
32200 KB |
Output is correct |
59 |
Correct |
51 ms |
30004 KB |
Output is correct |
60 |
Correct |
172 ms |
54680 KB |
Output is correct |
61 |
Correct |
166 ms |
46520 KB |
Output is correct |
62 |
Correct |
146 ms |
44140 KB |
Output is correct |
63 |
Correct |
202 ms |
44164 KB |
Output is correct |
64 |
Correct |
284 ms |
74708 KB |
Output is correct |
65 |
Correct |
300 ms |
78456 KB |
Output is correct |
66 |
Correct |
198 ms |
51392 KB |
Output is correct |
67 |
Correct |
161 ms |
60096 KB |
Output is correct |
68 |
Correct |
252 ms |
61500 KB |
Output is correct |
69 |
Correct |
221 ms |
62796 KB |
Output is correct |
70 |
Correct |
197 ms |
59328 KB |
Output is correct |