#include "race.h"
#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long llint;
const int maxn = 2e5+10;
const int maxk = 1e6+10;
const int inf = 0x3f3f3f3f;
int n, k;
vector< pair<int, int> > graph[maxn];
map< llint, llint > s[maxn];
llint sol = inf;
llint ofx[maxn], ofy[maxn];
void solve(int x, int parr) {
//printf("solving: %d %d\n", x, parr);
s[x][0] = 0;
for (auto iter : graph[x]) {
int tren = iter.X;
int cost = iter.Y;
if (tren == parr) continue;
solve(tren, x);
//printf("merging %d %d\n", x, tren);
ofx[tren] += cost;
ofy[tren]++;
if (s[tren].size() > s[x].size()) {
//printf("child is larger\n");
swap(s[tren], s[x]);
swap(ofx[tren], ofx[x]);
swap(ofy[tren], ofy[x]);
}
for (auto iter : s[tren]) {
llint dis = iter.X + ofx[tren];
//printf("trying %lld\n", dis);
if (s[x].count(k - dis - ofx[x]))
sol = min(sol, s[x][k - dis - ofx[x]] + iter.Y + ofy[tren] + ofy[x]);
}
for (auto iter : s[tren]) {
llint dis = iter.X + ofx[tren] - ofx[x];
if (s[x].count(dis)) s[x][dis] = min(s[x][dis], iter.Y + ofy[tren] - ofy[x]);
else s[x][dis] = iter.Y + ofy[tren] - ofy[x];
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
int a = H[i][0];
int b = H[i][1];
graph[a].push_back({b, L[i]});
graph[b].push_back({a, L[i]});
}
solve(0, -1);
if (sol == inf) return -1;
return sol;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14444 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14348 KB |
Output is correct |
8 |
Correct |
9 ms |
14460 KB |
Output is correct |
9 |
Correct |
11 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14436 KB |
Output is correct |
12 |
Correct |
7 ms |
14392 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14436 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14444 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14348 KB |
Output is correct |
8 |
Correct |
9 ms |
14460 KB |
Output is correct |
9 |
Correct |
11 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14436 KB |
Output is correct |
12 |
Correct |
7 ms |
14392 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14436 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14400 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14664 KB |
Output is correct |
22 |
Correct |
8 ms |
14768 KB |
Output is correct |
23 |
Correct |
8 ms |
14664 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
11 ms |
14708 KB |
Output is correct |
26 |
Correct |
9 ms |
14660 KB |
Output is correct |
27 |
Correct |
8 ms |
14500 KB |
Output is correct |
28 |
Correct |
8 ms |
14684 KB |
Output is correct |
29 |
Correct |
9 ms |
14672 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14628 KB |
Output is correct |
32 |
Correct |
9 ms |
14648 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14664 KB |
Output is correct |
35 |
Correct |
8 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14660 KB |
Output is correct |
37 |
Correct |
8 ms |
14572 KB |
Output is correct |
38 |
Correct |
8 ms |
14656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14444 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14348 KB |
Output is correct |
8 |
Correct |
9 ms |
14460 KB |
Output is correct |
9 |
Correct |
11 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14436 KB |
Output is correct |
12 |
Correct |
7 ms |
14392 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14436 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
107 ms |
37416 KB |
Output is correct |
20 |
Correct |
105 ms |
37444 KB |
Output is correct |
21 |
Correct |
110 ms |
37076 KB |
Output is correct |
22 |
Correct |
110 ms |
36668 KB |
Output is correct |
23 |
Correct |
148 ms |
49444 KB |
Output is correct |
24 |
Correct |
124 ms |
40124 KB |
Output is correct |
25 |
Correct |
98 ms |
36804 KB |
Output is correct |
26 |
Correct |
51 ms |
43440 KB |
Output is correct |
27 |
Correct |
159 ms |
49400 KB |
Output is correct |
28 |
Correct |
253 ms |
83388 KB |
Output is correct |
29 |
Correct |
239 ms |
81940 KB |
Output is correct |
30 |
Correct |
170 ms |
47524 KB |
Output is correct |
31 |
Correct |
180 ms |
47632 KB |
Output is correct |
32 |
Correct |
234 ms |
47608 KB |
Output is correct |
33 |
Correct |
217 ms |
52424 KB |
Output is correct |
34 |
Correct |
308 ms |
84412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14444 KB |
Output is correct |
3 |
Correct |
7 ms |
14412 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14348 KB |
Output is correct |
8 |
Correct |
9 ms |
14460 KB |
Output is correct |
9 |
Correct |
11 ms |
14424 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14436 KB |
Output is correct |
12 |
Correct |
7 ms |
14392 KB |
Output is correct |
13 |
Correct |
7 ms |
14456 KB |
Output is correct |
14 |
Correct |
7 ms |
14436 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14400 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14664 KB |
Output is correct |
22 |
Correct |
8 ms |
14768 KB |
Output is correct |
23 |
Correct |
8 ms |
14664 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
11 ms |
14708 KB |
Output is correct |
26 |
Correct |
9 ms |
14660 KB |
Output is correct |
27 |
Correct |
8 ms |
14500 KB |
Output is correct |
28 |
Correct |
8 ms |
14684 KB |
Output is correct |
29 |
Correct |
9 ms |
14672 KB |
Output is correct |
30 |
Correct |
9 ms |
14668 KB |
Output is correct |
31 |
Correct |
10 ms |
14628 KB |
Output is correct |
32 |
Correct |
9 ms |
14648 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14664 KB |
Output is correct |
35 |
Correct |
8 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14660 KB |
Output is correct |
37 |
Correct |
8 ms |
14572 KB |
Output is correct |
38 |
Correct |
8 ms |
14656 KB |
Output is correct |
39 |
Correct |
107 ms |
37416 KB |
Output is correct |
40 |
Correct |
105 ms |
37444 KB |
Output is correct |
41 |
Correct |
110 ms |
37076 KB |
Output is correct |
42 |
Correct |
110 ms |
36668 KB |
Output is correct |
43 |
Correct |
148 ms |
49444 KB |
Output is correct |
44 |
Correct |
124 ms |
40124 KB |
Output is correct |
45 |
Correct |
98 ms |
36804 KB |
Output is correct |
46 |
Correct |
51 ms |
43440 KB |
Output is correct |
47 |
Correct |
159 ms |
49400 KB |
Output is correct |
48 |
Correct |
253 ms |
83388 KB |
Output is correct |
49 |
Correct |
239 ms |
81940 KB |
Output is correct |
50 |
Correct |
170 ms |
47524 KB |
Output is correct |
51 |
Correct |
180 ms |
47632 KB |
Output is correct |
52 |
Correct |
234 ms |
47608 KB |
Output is correct |
53 |
Correct |
217 ms |
52424 KB |
Output is correct |
54 |
Correct |
308 ms |
84412 KB |
Output is correct |
55 |
Correct |
21 ms |
17648 KB |
Output is correct |
56 |
Correct |
13 ms |
16420 KB |
Output is correct |
57 |
Correct |
62 ms |
34788 KB |
Output is correct |
58 |
Correct |
48 ms |
28260 KB |
Output is correct |
59 |
Correct |
76 ms |
49364 KB |
Output is correct |
60 |
Correct |
232 ms |
82384 KB |
Output is correct |
61 |
Correct |
197 ms |
50872 KB |
Output is correct |
62 |
Correct |
153 ms |
47432 KB |
Output is correct |
63 |
Correct |
217 ms |
47592 KB |
Output is correct |
64 |
Correct |
407 ms |
102296 KB |
Output is correct |
65 |
Correct |
385 ms |
100020 KB |
Output is correct |
66 |
Correct |
248 ms |
78800 KB |
Output is correct |
67 |
Correct |
165 ms |
41472 KB |
Output is correct |
68 |
Correct |
321 ms |
63812 KB |
Output is correct |
69 |
Correct |
344 ms |
68820 KB |
Output is correct |
70 |
Correct |
299 ms |
61888 KB |
Output is correct |