#include <bits/stdc++.h>
using namespace std;
const int inf = (int) 1e9;
const int N = (int) 2e5 + 1;
const int M = (int) 1e6 + 1;
vector<pair<int, int>> adj[N];
vector<pair<int, int>> tmp;
int Get_Min[M], idx[M];
int sz[N];
bool vis[N];
int ans = inf;
int k;
int Dfs2(int node, int pre, int depth, int tot, int id) {
if (tot > k) return inf;
int res = inf;
if (idx[k - tot] == id) {
res = min(res, Get_Min[k - tot] + depth);
}
tmp.emplace_back(tot, depth);
for (auto [x, y] : adj[node]) {
if (x == pre || vis[x]) {
continue;
}
res = min(res, Dfs2(x, node, depth + 1, tot + y, id));
}
return res;
}
int best_path(int n, int K, int edges[][2], int w[]) {
k = K;
for (int i = 0; i < n - 1; i++) {
int a = edges[i][0];
int b = edges[i][1];
int c = w[i];
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
int cnt = 0;
for (int i = 0; i <= k; i++) {
Get_Min[i] = inf;
idx[i] = -1;
}
function<int(int, int)> Get_Sub_Sizes = [&](int node, int pre) {
sz[node] = 1;
for (auto [x, y] : adj[node]) {
if (x == pre || vis[x]) {
continue;
}
sz[node] += Get_Sub_Sizes(x, node);
}
return sz[node];
};
function<int(int, int, int)> Get_Centroid = [&](int node, int pre, int s) {
for (auto [x, y] : adj[node]) {
if (x == pre || vis[x]) {
continue;
}
if (sz[x] > s / 2) {
return Get_Centroid(x, node, s);
}
}
return node;
};
function<void(int, int)> Decompose = [&](int node, int pre) {
Get_Sub_Sizes(node, pre);
int c = Get_Centroid(node, pre, sz[node]);
vis[c] = true;
Get_Min[0] = 0;
idx[0] = cnt++;
for (auto [x, y] : adj[c]) {
if (x == pre || vis[x]) continue;
tmp.clear();
ans = min(ans, Dfs2(x, c, 1, y, idx[0]));
for (int j = 0; j < (int) tmp.size(); j++) {
if (idx[tmp[j].first] == idx[0]) {
Get_Min[tmp[j].first] = min(Get_Min[tmp[j].first], tmp[j].second);
}
else {
Get_Min[tmp[j].first] = tmp[j].second;
idx[tmp[j].first] = idx[0];
}
}
}
for (auto [x, y] : adj[c]) {
if (vis[x]) {
continue;
}
Decompose(x, c);
}
};
Decompose(0, -1);
if (ans >= inf) {
return -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 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 |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 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 |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
5004 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
12244 KB |
Output is correct |
23 |
Correct |
6 ms |
10964 KB |
Output is correct |
24 |
Correct |
6 ms |
11860 KB |
Output is correct |
25 |
Correct |
7 ms |
11656 KB |
Output is correct |
26 |
Correct |
5 ms |
7700 KB |
Output is correct |
27 |
Correct |
6 ms |
11336 KB |
Output is correct |
28 |
Correct |
5 ms |
6548 KB |
Output is correct |
29 |
Correct |
5 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7892 KB |
Output is correct |
31 |
Correct |
6 ms |
10196 KB |
Output is correct |
32 |
Correct |
7 ms |
10652 KB |
Output is correct |
33 |
Correct |
7 ms |
11220 KB |
Output is correct |
34 |
Correct |
6 ms |
9668 KB |
Output is correct |
35 |
Correct |
7 ms |
11408 KB |
Output is correct |
36 |
Correct |
8 ms |
12308 KB |
Output is correct |
37 |
Correct |
6 ms |
11348 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 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 |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
112 ms |
11816 KB |
Output is correct |
20 |
Correct |
141 ms |
11792 KB |
Output is correct |
21 |
Correct |
127 ms |
11972 KB |
Output is correct |
22 |
Correct |
110 ms |
12220 KB |
Output is correct |
23 |
Correct |
75 ms |
11976 KB |
Output is correct |
24 |
Correct |
54 ms |
11852 KB |
Output is correct |
25 |
Correct |
104 ms |
16140 KB |
Output is correct |
26 |
Correct |
84 ms |
21108 KB |
Output is correct |
27 |
Correct |
158 ms |
18884 KB |
Output is correct |
28 |
Correct |
272 ms |
36284 KB |
Output is correct |
29 |
Correct |
274 ms |
35164 KB |
Output is correct |
30 |
Correct |
180 ms |
18796 KB |
Output is correct |
31 |
Correct |
167 ms |
18800 KB |
Output is correct |
32 |
Correct |
188 ms |
19000 KB |
Output is correct |
33 |
Correct |
245 ms |
17744 KB |
Output is correct |
34 |
Correct |
196 ms |
18596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5000 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5004 KB |
Output is correct |
7 |
Correct |
3 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
5000 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 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 |
5000 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5000 KB |
Output is correct |
20 |
Correct |
3 ms |
5004 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
12244 KB |
Output is correct |
23 |
Correct |
6 ms |
10964 KB |
Output is correct |
24 |
Correct |
6 ms |
11860 KB |
Output is correct |
25 |
Correct |
7 ms |
11656 KB |
Output is correct |
26 |
Correct |
5 ms |
7700 KB |
Output is correct |
27 |
Correct |
6 ms |
11336 KB |
Output is correct |
28 |
Correct |
5 ms |
6548 KB |
Output is correct |
29 |
Correct |
5 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7892 KB |
Output is correct |
31 |
Correct |
6 ms |
10196 KB |
Output is correct |
32 |
Correct |
7 ms |
10652 KB |
Output is correct |
33 |
Correct |
7 ms |
11220 KB |
Output is correct |
34 |
Correct |
6 ms |
9668 KB |
Output is correct |
35 |
Correct |
7 ms |
11408 KB |
Output is correct |
36 |
Correct |
8 ms |
12308 KB |
Output is correct |
37 |
Correct |
6 ms |
11348 KB |
Output is correct |
38 |
Correct |
5 ms |
9044 KB |
Output is correct |
39 |
Correct |
112 ms |
11816 KB |
Output is correct |
40 |
Correct |
141 ms |
11792 KB |
Output is correct |
41 |
Correct |
127 ms |
11972 KB |
Output is correct |
42 |
Correct |
110 ms |
12220 KB |
Output is correct |
43 |
Correct |
75 ms |
11976 KB |
Output is correct |
44 |
Correct |
54 ms |
11852 KB |
Output is correct |
45 |
Correct |
104 ms |
16140 KB |
Output is correct |
46 |
Correct |
84 ms |
21108 KB |
Output is correct |
47 |
Correct |
158 ms |
18884 KB |
Output is correct |
48 |
Correct |
272 ms |
36284 KB |
Output is correct |
49 |
Correct |
274 ms |
35164 KB |
Output is correct |
50 |
Correct |
180 ms |
18796 KB |
Output is correct |
51 |
Correct |
167 ms |
18800 KB |
Output is correct |
52 |
Correct |
188 ms |
19000 KB |
Output is correct |
53 |
Correct |
245 ms |
17744 KB |
Output is correct |
54 |
Correct |
196 ms |
18596 KB |
Output is correct |
55 |
Correct |
11 ms |
5780 KB |
Output is correct |
56 |
Correct |
11 ms |
5796 KB |
Output is correct |
57 |
Correct |
71 ms |
12492 KB |
Output is correct |
58 |
Correct |
36 ms |
11640 KB |
Output is correct |
59 |
Correct |
99 ms |
22480 KB |
Output is correct |
60 |
Correct |
361 ms |
44040 KB |
Output is correct |
61 |
Correct |
190 ms |
19004 KB |
Output is correct |
62 |
Correct |
208 ms |
26832 KB |
Output is correct |
63 |
Correct |
241 ms |
26640 KB |
Output is correct |
64 |
Correct |
402 ms |
24088 KB |
Output is correct |
65 |
Correct |
244 ms |
19660 KB |
Output is correct |
66 |
Correct |
340 ms |
39904 KB |
Output is correct |
67 |
Correct |
103 ms |
27288 KB |
Output is correct |
68 |
Correct |
222 ms |
27428 KB |
Output is correct |
69 |
Correct |
231 ms |
27596 KB |
Output is correct |
70 |
Correct |
199 ms |
26768 KB |
Output is correct |