#include "race.h"
#include "bits/stdc++.h"
using namespace std;
vector < pair <int, int> > g[200010];
int ans;
int len;
void dfs(int x, int par, map <long long, int> &f, long long &add, int &extra) {
add = 0;
extra = 0;
f[0] = 0;
for(auto i : g[x]) {
if(i.first == par) continue;
map <long long, int> t;
long long plus;
int rem;
dfs(i.first, x, t, plus, rem);
plus += i.second;
rem += 1;
if(t.size() > f.size()) {
f.swap(t);
swap(add, plus);
swap(extra, rem);
}
for(auto i : t) {
if(f.find(len - i.first - plus - add) != f.end()) {
ans = min(ans, i.second + f[len - i.first - plus - add] + extra + rem);
}
}
plus -= add;
rem -= extra;
for(auto i : t) {
long long p = i.first + plus;
if(f.find(p) == f.end()) {
f[p] = i.second + rem;
} else {
f[p] = min(f[p], i.second + rem);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
ans = INT_MAX;
len = K;
for(int i = 0; i <= N; i++) g[i].clear();
for(int i = 0; i < N-1; i++) {
int p = H[i][0];
int q = H[i][1];
int r = L[i];
g[p].push_back(make_pair(q, r));
g[q].push_back(make_pair(p, r));
}
map <long long, int> f;
long long add;
int extra;
dfs(0, -1, f, add, extra);
return ans == INT_MAX ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5080 KB |
Output is correct |
2 |
Correct |
6 ms |
5152 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
6 ms |
5228 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
6 |
Correct |
7 ms |
5380 KB |
Output is correct |
7 |
Correct |
7 ms |
5380 KB |
Output is correct |
8 |
Correct |
5 ms |
5380 KB |
Output is correct |
9 |
Correct |
6 ms |
5380 KB |
Output is correct |
10 |
Correct |
5 ms |
5380 KB |
Output is correct |
11 |
Correct |
6 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5380 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
7 ms |
5380 KB |
Output is correct |
15 |
Correct |
7 ms |
5380 KB |
Output is correct |
16 |
Correct |
6 ms |
5380 KB |
Output is correct |
17 |
Correct |
6 ms |
5380 KB |
Output is correct |
18 |
Correct |
7 ms |
5380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5080 KB |
Output is correct |
2 |
Correct |
6 ms |
5152 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
6 ms |
5228 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
6 |
Correct |
7 ms |
5380 KB |
Output is correct |
7 |
Correct |
7 ms |
5380 KB |
Output is correct |
8 |
Correct |
5 ms |
5380 KB |
Output is correct |
9 |
Correct |
6 ms |
5380 KB |
Output is correct |
10 |
Correct |
5 ms |
5380 KB |
Output is correct |
11 |
Correct |
6 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5380 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
7 ms |
5380 KB |
Output is correct |
15 |
Correct |
7 ms |
5380 KB |
Output is correct |
16 |
Correct |
6 ms |
5380 KB |
Output is correct |
17 |
Correct |
6 ms |
5380 KB |
Output is correct |
18 |
Correct |
7 ms |
5380 KB |
Output is correct |
19 |
Correct |
7 ms |
5380 KB |
Output is correct |
20 |
Correct |
7 ms |
5380 KB |
Output is correct |
21 |
Correct |
7 ms |
5380 KB |
Output is correct |
22 |
Correct |
10 ms |
5380 KB |
Output is correct |
23 |
Correct |
7 ms |
5380 KB |
Output is correct |
24 |
Correct |
7 ms |
5380 KB |
Output is correct |
25 |
Correct |
7 ms |
5380 KB |
Output is correct |
26 |
Correct |
9 ms |
5380 KB |
Output is correct |
27 |
Correct |
6 ms |
5392 KB |
Output is correct |
28 |
Correct |
9 ms |
5392 KB |
Output is correct |
29 |
Correct |
8 ms |
5392 KB |
Output is correct |
30 |
Correct |
7 ms |
5392 KB |
Output is correct |
31 |
Correct |
8 ms |
5392 KB |
Output is correct |
32 |
Correct |
8 ms |
5392 KB |
Output is correct |
33 |
Correct |
9 ms |
5392 KB |
Output is correct |
34 |
Correct |
7 ms |
5392 KB |
Output is correct |
35 |
Correct |
7 ms |
5392 KB |
Output is correct |
36 |
Correct |
7 ms |
5480 KB |
Output is correct |
37 |
Correct |
6 ms |
5480 KB |
Output is correct |
38 |
Correct |
6 ms |
5480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5080 KB |
Output is correct |
2 |
Correct |
6 ms |
5152 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
6 ms |
5228 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
6 |
Correct |
7 ms |
5380 KB |
Output is correct |
7 |
Correct |
7 ms |
5380 KB |
Output is correct |
8 |
Correct |
5 ms |
5380 KB |
Output is correct |
9 |
Correct |
6 ms |
5380 KB |
Output is correct |
10 |
Correct |
5 ms |
5380 KB |
Output is correct |
11 |
Correct |
6 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5380 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
7 ms |
5380 KB |
Output is correct |
15 |
Correct |
7 ms |
5380 KB |
Output is correct |
16 |
Correct |
6 ms |
5380 KB |
Output is correct |
17 |
Correct |
6 ms |
5380 KB |
Output is correct |
18 |
Correct |
7 ms |
5380 KB |
Output is correct |
19 |
Correct |
142 ms |
10212 KB |
Output is correct |
20 |
Correct |
198 ms |
10316 KB |
Output is correct |
21 |
Correct |
133 ms |
10352 KB |
Output is correct |
22 |
Correct |
174 ms |
10352 KB |
Output is correct |
23 |
Correct |
187 ms |
10632 KB |
Output is correct |
24 |
Correct |
182 ms |
10632 KB |
Output is correct |
25 |
Correct |
104 ms |
24804 KB |
Output is correct |
26 |
Correct |
89 ms |
37732 KB |
Output is correct |
27 |
Correct |
268 ms |
37732 KB |
Output is correct |
28 |
Correct |
474 ms |
70180 KB |
Output is correct |
29 |
Correct |
458 ms |
70180 KB |
Output is correct |
30 |
Correct |
274 ms |
70180 KB |
Output is correct |
31 |
Correct |
269 ms |
70180 KB |
Output is correct |
32 |
Correct |
252 ms |
70180 KB |
Output is correct |
33 |
Correct |
239 ms |
70180 KB |
Output is correct |
34 |
Correct |
482 ms |
70180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5080 KB |
Output is correct |
2 |
Correct |
6 ms |
5152 KB |
Output is correct |
3 |
Correct |
5 ms |
5228 KB |
Output is correct |
4 |
Correct |
6 ms |
5228 KB |
Output is correct |
5 |
Correct |
6 ms |
5380 KB |
Output is correct |
6 |
Correct |
7 ms |
5380 KB |
Output is correct |
7 |
Correct |
7 ms |
5380 KB |
Output is correct |
8 |
Correct |
5 ms |
5380 KB |
Output is correct |
9 |
Correct |
6 ms |
5380 KB |
Output is correct |
10 |
Correct |
5 ms |
5380 KB |
Output is correct |
11 |
Correct |
6 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5380 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
7 ms |
5380 KB |
Output is correct |
15 |
Correct |
7 ms |
5380 KB |
Output is correct |
16 |
Correct |
6 ms |
5380 KB |
Output is correct |
17 |
Correct |
6 ms |
5380 KB |
Output is correct |
18 |
Correct |
7 ms |
5380 KB |
Output is correct |
19 |
Correct |
7 ms |
5380 KB |
Output is correct |
20 |
Correct |
7 ms |
5380 KB |
Output is correct |
21 |
Correct |
7 ms |
5380 KB |
Output is correct |
22 |
Correct |
10 ms |
5380 KB |
Output is correct |
23 |
Correct |
7 ms |
5380 KB |
Output is correct |
24 |
Correct |
7 ms |
5380 KB |
Output is correct |
25 |
Correct |
7 ms |
5380 KB |
Output is correct |
26 |
Correct |
9 ms |
5380 KB |
Output is correct |
27 |
Correct |
6 ms |
5392 KB |
Output is correct |
28 |
Correct |
9 ms |
5392 KB |
Output is correct |
29 |
Correct |
8 ms |
5392 KB |
Output is correct |
30 |
Correct |
7 ms |
5392 KB |
Output is correct |
31 |
Correct |
8 ms |
5392 KB |
Output is correct |
32 |
Correct |
8 ms |
5392 KB |
Output is correct |
33 |
Correct |
9 ms |
5392 KB |
Output is correct |
34 |
Correct |
7 ms |
5392 KB |
Output is correct |
35 |
Correct |
7 ms |
5392 KB |
Output is correct |
36 |
Correct |
7 ms |
5480 KB |
Output is correct |
37 |
Correct |
6 ms |
5480 KB |
Output is correct |
38 |
Correct |
6 ms |
5480 KB |
Output is correct |
39 |
Correct |
142 ms |
10212 KB |
Output is correct |
40 |
Correct |
198 ms |
10316 KB |
Output is correct |
41 |
Correct |
133 ms |
10352 KB |
Output is correct |
42 |
Correct |
174 ms |
10352 KB |
Output is correct |
43 |
Correct |
187 ms |
10632 KB |
Output is correct |
44 |
Correct |
182 ms |
10632 KB |
Output is correct |
45 |
Correct |
104 ms |
24804 KB |
Output is correct |
46 |
Correct |
89 ms |
37732 KB |
Output is correct |
47 |
Correct |
268 ms |
37732 KB |
Output is correct |
48 |
Correct |
474 ms |
70180 KB |
Output is correct |
49 |
Correct |
458 ms |
70180 KB |
Output is correct |
50 |
Correct |
274 ms |
70180 KB |
Output is correct |
51 |
Correct |
269 ms |
70180 KB |
Output is correct |
52 |
Correct |
252 ms |
70180 KB |
Output is correct |
53 |
Correct |
239 ms |
70180 KB |
Output is correct |
54 |
Correct |
482 ms |
70180 KB |
Output is correct |
55 |
Correct |
22 ms |
70180 KB |
Output is correct |
56 |
Correct |
15 ms |
70180 KB |
Output is correct |
57 |
Correct |
91 ms |
70180 KB |
Output is correct |
58 |
Correct |
64 ms |
70180 KB |
Output is correct |
59 |
Correct |
145 ms |
70180 KB |
Output is correct |
60 |
Correct |
408 ms |
70180 KB |
Output is correct |
61 |
Correct |
296 ms |
70180 KB |
Output is correct |
62 |
Correct |
242 ms |
70180 KB |
Output is correct |
63 |
Correct |
280 ms |
70180 KB |
Output is correct |
64 |
Correct |
550 ms |
70180 KB |
Output is correct |
65 |
Correct |
603 ms |
70180 KB |
Output is correct |
66 |
Correct |
451 ms |
70180 KB |
Output is correct |
67 |
Correct |
175 ms |
70180 KB |
Output is correct |
68 |
Correct |
469 ms |
70180 KB |
Output is correct |
69 |
Correct |
497 ms |
70180 KB |
Output is correct |
70 |
Correct |
461 ms |
70180 KB |
Output is correct |