#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
vector<pair<int, int>> g[MAX];
int k, ans = 1e9;
bitset<MAX> deleted;
struct cal {
unordered_map<int, int> mp;
void update(int u, int p, int sum, int depth) {
if (sum > k) return;
auto &cur = mp[sum];
if (cur == 0) cur = depth;
else cur = min(cur, depth);
for (auto &[v, w] : g[u]) {
if (v != p && !deleted[v]) {
update(v, u, sum + w, depth + 1);
}
}
}
void add(int u, int p, int sum, int depth) {
if (sum > k) return;
auto it = mp.find(k - sum);
if (it != mp.end()) ans = min(ans, it->second + depth);
for (auto &[v, w] : g[u]) {
if (v != p && !deleted[v]) {
add(v, u, sum + w, depth + 1);
}
}
}
void lamviec(int u) {
mp[0] = 0;
for (auto &[v, w] : g[u]) {
if (!deleted[v]) {
add(v, u, w, 1);
update(v, u, w, 1);
}
}
}
};
int sz[MAX];
void dfs(int u, int p) {
sz[u] = 1;
for (auto &[v, w] : g[u]) {
if (v != p && !deleted[v]) {
dfs(v, u);
sz[u] += sz[v];
}
}
}
int findcentroid(int u, int p, const int &n) {
for (auto &[v, w] : g[u]) {
if (v != p && !deleted[v] && sz[v] * 2 > n) return findcentroid(v, u, n);
}
return u;
}
void decompose(int u) {
dfs(u, 0);
u = findcentroid(u, 0, sz[u]);
deleted[u] = true;
cal tmp; tmp.lamviec(u);
for (auto &[v, w] : g[u]) {
if (!deleted[v]) {
decompose(v);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
int n = N;
k = K;
for (int u, v, w, i = 0; i < n - 1; i++) {
u = H[i][0], v = H[i][1], w = L[i];
++u, ++v;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
decompose(1);
if (ans == (int)1e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9576 KB |
Output is correct |
2 |
Correct |
1 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
1 ms |
9564 KB |
Output is correct |
5 |
Correct |
1 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
1 ms |
9564 KB |
Output is correct |
10 |
Correct |
1 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9560 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
1 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
1 ms |
9564 KB |
Output is correct |
16 |
Correct |
1 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
1 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9576 KB |
Output is correct |
2 |
Correct |
1 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
1 ms |
9564 KB |
Output is correct |
5 |
Correct |
1 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
1 ms |
9564 KB |
Output is correct |
10 |
Correct |
1 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9560 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
1 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
1 ms |
9564 KB |
Output is correct |
16 |
Correct |
1 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
1 ms |
9560 KB |
Output is correct |
19 |
Correct |
1 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9560 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
1 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9648 KB |
Output is correct |
32 |
Correct |
2 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9576 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9576 KB |
Output is correct |
2 |
Correct |
1 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
1 ms |
9564 KB |
Output is correct |
5 |
Correct |
1 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
1 ms |
9564 KB |
Output is correct |
10 |
Correct |
1 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9560 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
1 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
1 ms |
9564 KB |
Output is correct |
16 |
Correct |
1 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
1 ms |
9560 KB |
Output is correct |
19 |
Correct |
77 ms |
15452 KB |
Output is correct |
20 |
Correct |
76 ms |
15452 KB |
Output is correct |
21 |
Correct |
91 ms |
15392 KB |
Output is correct |
22 |
Correct |
84 ms |
15448 KB |
Output is correct |
23 |
Correct |
43 ms |
15704 KB |
Output is correct |
24 |
Correct |
36 ms |
15452 KB |
Output is correct |
25 |
Correct |
82 ms |
18248 KB |
Output is correct |
26 |
Correct |
83 ms |
21204 KB |
Output is correct |
27 |
Correct |
123 ms |
19280 KB |
Output is correct |
28 |
Correct |
142 ms |
30288 KB |
Output is correct |
29 |
Correct |
147 ms |
29528 KB |
Output is correct |
30 |
Correct |
128 ms |
19284 KB |
Output is correct |
31 |
Correct |
119 ms |
19292 KB |
Output is correct |
32 |
Correct |
121 ms |
19228 KB |
Output is correct |
33 |
Correct |
118 ms |
18256 KB |
Output is correct |
34 |
Correct |
96 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
9576 KB |
Output is correct |
2 |
Correct |
1 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
1 ms |
9564 KB |
Output is correct |
5 |
Correct |
1 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
1 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
1 ms |
9564 KB |
Output is correct |
10 |
Correct |
1 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9560 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
1 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
1 ms |
9564 KB |
Output is correct |
16 |
Correct |
1 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
1 ms |
9560 KB |
Output is correct |
19 |
Correct |
1 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9560 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9564 KB |
Output is correct |
27 |
Correct |
1 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9648 KB |
Output is correct |
32 |
Correct |
2 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
2 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
2 ms |
9576 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
77 ms |
15452 KB |
Output is correct |
40 |
Correct |
76 ms |
15452 KB |
Output is correct |
41 |
Correct |
91 ms |
15392 KB |
Output is correct |
42 |
Correct |
84 ms |
15448 KB |
Output is correct |
43 |
Correct |
43 ms |
15704 KB |
Output is correct |
44 |
Correct |
36 ms |
15452 KB |
Output is correct |
45 |
Correct |
82 ms |
18248 KB |
Output is correct |
46 |
Correct |
83 ms |
21204 KB |
Output is correct |
47 |
Correct |
123 ms |
19280 KB |
Output is correct |
48 |
Correct |
142 ms |
30288 KB |
Output is correct |
49 |
Correct |
147 ms |
29528 KB |
Output is correct |
50 |
Correct |
128 ms |
19284 KB |
Output is correct |
51 |
Correct |
119 ms |
19292 KB |
Output is correct |
52 |
Correct |
121 ms |
19228 KB |
Output is correct |
53 |
Correct |
118 ms |
18256 KB |
Output is correct |
54 |
Correct |
96 ms |
18260 KB |
Output is correct |
55 |
Correct |
9 ms |
10076 KB |
Output is correct |
56 |
Correct |
8 ms |
9820 KB |
Output is correct |
57 |
Correct |
68 ms |
15716 KB |
Output is correct |
58 |
Correct |
36 ms |
16592 KB |
Output is correct |
59 |
Correct |
132 ms |
28388 KB |
Output is correct |
60 |
Correct |
392 ms |
49784 KB |
Output is correct |
61 |
Correct |
157 ms |
22612 KB |
Output is correct |
62 |
Correct |
155 ms |
22544 KB |
Output is correct |
63 |
Correct |
153 ms |
22356 KB |
Output is correct |
64 |
Correct |
309 ms |
32456 KB |
Output is correct |
65 |
Correct |
90 ms |
23120 KB |
Output is correct |
66 |
Correct |
277 ms |
32084 KB |
Output is correct |
67 |
Correct |
94 ms |
23040 KB |
Output is correct |
68 |
Correct |
156 ms |
29028 KB |
Output is correct |
69 |
Correct |
163 ms |
29028 KB |
Output is correct |
70 |
Correct |
174 ms |
28656 KB |
Output is correct |