#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> adj(200000);
vector<map<int64_t, int>> mp(200000);
int offsetw[200000];
int offsetl[200000];
int k;
int ans = INT_MAX;
void dfs(int c, int p, int w = 0) {
mp[c][0] = 0;
offsetl[c] = 0;
offsetw[c] = 0;
for (auto &i : adj[c]) {
if (i.first == p) continue;
int x = i.first;
dfs(x, c, i.second);
if (mp[c].size() < mp[x].size()) {
swap(mp[x], mp[c]);
swap(offsetl[c], offsetl[x]);
swap(offsetw[c], offsetw[x]);
}
for (auto &j : mp[x]) {
auto it = mp[c].find(k - (j.first + offsetw[x]) - offsetw[c]);
if (it != mp[c].end()) ans = min(ans, j.second + it->second + 1 + offsetl[c] + offsetl[x]);
}
for (auto &j : mp[x]) {
auto it = mp[c].find(j.first + offsetw[x] - offsetw[c]);
if (it != mp[c].end()) {
it->second = min(it->second, j.second + offsetl[x] - offsetl[c]);
}
else {
mp[c][j.first + offsetw[x] - offsetw[c]] = j.second + offsetl[x] - offsetl[c];
}
}
}
offsetl[c]++;
offsetw[c] += w;
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; i++) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
k = K;
dfs(0, -1);
return (ans == INT_MAX ? -1 : (ans - 1));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14356 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14452 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14344 KB |
Output is correct |
16 |
Correct |
8 ms |
14428 KB |
Output is correct |
17 |
Correct |
8 ms |
14352 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14356 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14452 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14344 KB |
Output is correct |
16 |
Correct |
8 ms |
14428 KB |
Output is correct |
17 |
Correct |
8 ms |
14352 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14408 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14668 KB |
Output is correct |
22 |
Correct |
9 ms |
14668 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
9 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14676 KB |
Output is correct |
27 |
Correct |
9 ms |
14544 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
10 ms |
14604 KB |
Output is correct |
30 |
Correct |
10 ms |
14800 KB |
Output is correct |
31 |
Correct |
9 ms |
14668 KB |
Output is correct |
32 |
Correct |
11 ms |
14612 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
9 ms |
14688 KB |
Output is correct |
35 |
Correct |
11 ms |
14624 KB |
Output is correct |
36 |
Correct |
9 ms |
14668 KB |
Output is correct |
37 |
Correct |
10 ms |
14540 KB |
Output is correct |
38 |
Correct |
9 ms |
14668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14356 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14452 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14344 KB |
Output is correct |
16 |
Correct |
8 ms |
14428 KB |
Output is correct |
17 |
Correct |
8 ms |
14352 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
118 ms |
36988 KB |
Output is correct |
20 |
Correct |
136 ms |
36960 KB |
Output is correct |
21 |
Correct |
131 ms |
36648 KB |
Output is correct |
22 |
Correct |
161 ms |
36328 KB |
Output is correct |
23 |
Correct |
171 ms |
49072 KB |
Output is correct |
24 |
Correct |
136 ms |
39656 KB |
Output is correct |
25 |
Correct |
99 ms |
37584 KB |
Output is correct |
26 |
Correct |
63 ms |
45892 KB |
Output is correct |
27 |
Correct |
192 ms |
47808 KB |
Output is correct |
28 |
Correct |
335 ms |
90176 KB |
Output is correct |
29 |
Correct |
376 ms |
88244 KB |
Output is correct |
30 |
Correct |
187 ms |
47852 KB |
Output is correct |
31 |
Correct |
192 ms |
47860 KB |
Output is correct |
32 |
Correct |
266 ms |
47996 KB |
Output is correct |
33 |
Correct |
307 ms |
52876 KB |
Output is correct |
34 |
Correct |
365 ms |
85704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
10 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14356 KB |
Output is correct |
12 |
Correct |
9 ms |
14412 KB |
Output is correct |
13 |
Correct |
10 ms |
14452 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14344 KB |
Output is correct |
16 |
Correct |
8 ms |
14428 KB |
Output is correct |
17 |
Correct |
8 ms |
14352 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
7 ms |
14408 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14668 KB |
Output is correct |
22 |
Correct |
9 ms |
14668 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
11 ms |
14668 KB |
Output is correct |
25 |
Correct |
9 ms |
14668 KB |
Output is correct |
26 |
Correct |
10 ms |
14676 KB |
Output is correct |
27 |
Correct |
9 ms |
14544 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
10 ms |
14604 KB |
Output is correct |
30 |
Correct |
10 ms |
14800 KB |
Output is correct |
31 |
Correct |
9 ms |
14668 KB |
Output is correct |
32 |
Correct |
11 ms |
14612 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
9 ms |
14688 KB |
Output is correct |
35 |
Correct |
11 ms |
14624 KB |
Output is correct |
36 |
Correct |
9 ms |
14668 KB |
Output is correct |
37 |
Correct |
10 ms |
14540 KB |
Output is correct |
38 |
Correct |
9 ms |
14668 KB |
Output is correct |
39 |
Correct |
118 ms |
36988 KB |
Output is correct |
40 |
Correct |
136 ms |
36960 KB |
Output is correct |
41 |
Correct |
131 ms |
36648 KB |
Output is correct |
42 |
Correct |
161 ms |
36328 KB |
Output is correct |
43 |
Correct |
171 ms |
49072 KB |
Output is correct |
44 |
Correct |
136 ms |
39656 KB |
Output is correct |
45 |
Correct |
99 ms |
37584 KB |
Output is correct |
46 |
Correct |
63 ms |
45892 KB |
Output is correct |
47 |
Correct |
192 ms |
47808 KB |
Output is correct |
48 |
Correct |
335 ms |
90176 KB |
Output is correct |
49 |
Correct |
376 ms |
88244 KB |
Output is correct |
50 |
Correct |
187 ms |
47852 KB |
Output is correct |
51 |
Correct |
192 ms |
47860 KB |
Output is correct |
52 |
Correct |
266 ms |
47996 KB |
Output is correct |
53 |
Correct |
307 ms |
52876 KB |
Output is correct |
54 |
Correct |
365 ms |
85704 KB |
Output is correct |
55 |
Correct |
28 ms |
17612 KB |
Output is correct |
56 |
Correct |
15 ms |
16320 KB |
Output is correct |
57 |
Correct |
75 ms |
34404 KB |
Output is correct |
58 |
Correct |
52 ms |
27560 KB |
Output is correct |
59 |
Correct |
119 ms |
52072 KB |
Output is correct |
60 |
Correct |
348 ms |
88708 KB |
Output is correct |
61 |
Correct |
236 ms |
51416 KB |
Output is correct |
62 |
Correct |
209 ms |
47904 KB |
Output is correct |
63 |
Correct |
258 ms |
47968 KB |
Output is correct |
64 |
Correct |
454 ms |
103112 KB |
Output is correct |
65 |
Correct |
542 ms |
101328 KB |
Output is correct |
66 |
Correct |
343 ms |
84400 KB |
Output is correct |
67 |
Correct |
180 ms |
42292 KB |
Output is correct |
68 |
Correct |
432 ms |
64968 KB |
Output is correct |
69 |
Correct |
415 ms |
69896 KB |
Output is correct |
70 |
Correct |
380 ms |
62932 KB |
Output is correct |