#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int inf = 1e9+9999;
vector<vector<pair<int, int>>> graph;
map<int, int> sums, tempsums;
vector<int> sub;
int k, centroid;
int ans = inf;
int rem;
vector<bool> vis, mark;
int dfs1(int node, int par) {
int val = 1;
mark[node] = true;
for(auto it : graph[node]) {
if(it.first == par)
continue;
if(vis[it.first])
continue;
val += dfs1(it.first, node);
}
sub[node] = val;
return val;
}
void dfs2(int node, int par, int cur, int len) {
if(cur > k)
return;
if(k == cur)
ans = min(ans, len);
else if(sums[k - cur])
ans = min(ans, sums[k - cur] + len);
for(auto it : graph[node]) {
if(vis[it.first])
continue;
if(it.first == par)
continue;
if(node == centroid) {
for(auto it: tempsums) {
if(!sums[it.first])
sums[it.first] = it.second;
sums[it.first] = min(it.second, sums[it.first]);
}
tempsums.clear();
}
dfs2(it.first, node, cur + it.second, len + 1);
}
if(!tempsums[cur])
tempsums[cur] = len;
tempsums[cur] = min(len, tempsums[cur]);
return;
}
int findcentroid(int node, int par, int sz) {
for(auto it : graph[node]){
if(vis[it.first])
continue;
if(it.first == par)
continue;
if(sub[it.first] >= sz / 2)
return findcentroid(it.first, node, sz);
}
return node;
}
int best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[]) {
rem = N;
k = K;
graph.assign(N + 5, vector<pair<int, int>>());
vis.assign(N + 5, false);
for(int i = 0; i < N - 1; i++) {
graph[H[i][1]].push_back({H[i][0], L[i]});
graph[H[i][0]].push_back({H[i][1], L[i]});
}
while(rem) {
mark.assign(N + 5, false);
sub.assign(N + 5, 0);
for(int i = 0; i < N; i++) {
sums.clear();
tempsums.clear();
if(mark[i] || vis[i])
continue;
int sz = dfs1(i, -1);
centroid = findcentroid(i, -1, sz);
dfs2(centroid, -1, 0, 0);
vis[centroid] = true;
rem--;
}
}
if(ans == inf)
return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2648 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
3 ms |
2392 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2672 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
4 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
226 ms |
12240 KB |
Output is correct |
20 |
Correct |
217 ms |
12380 KB |
Output is correct |
21 |
Correct |
240 ms |
12488 KB |
Output is correct |
22 |
Correct |
216 ms |
12600 KB |
Output is correct |
23 |
Correct |
83 ms |
12628 KB |
Output is correct |
24 |
Correct |
83 ms |
12712 KB |
Output is correct |
25 |
Correct |
136 ms |
15248 KB |
Output is correct |
26 |
Correct |
92 ms |
18996 KB |
Output is correct |
27 |
Correct |
223 ms |
20672 KB |
Output is correct |
28 |
Correct |
278 ms |
32080 KB |
Output is correct |
29 |
Correct |
268 ms |
31060 KB |
Output is correct |
30 |
Correct |
198 ms |
20564 KB |
Output is correct |
31 |
Correct |
245 ms |
20672 KB |
Output is correct |
32 |
Correct |
252 ms |
20564 KB |
Output is correct |
33 |
Correct |
389 ms |
19540 KB |
Output is correct |
34 |
Correct |
176 ms |
20232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2648 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
3 ms |
2392 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2672 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
4 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
4 ms |
2652 KB |
Output is correct |
39 |
Correct |
226 ms |
12240 KB |
Output is correct |
40 |
Correct |
217 ms |
12380 KB |
Output is correct |
41 |
Correct |
240 ms |
12488 KB |
Output is correct |
42 |
Correct |
216 ms |
12600 KB |
Output is correct |
43 |
Correct |
83 ms |
12628 KB |
Output is correct |
44 |
Correct |
83 ms |
12712 KB |
Output is correct |
45 |
Correct |
136 ms |
15248 KB |
Output is correct |
46 |
Correct |
92 ms |
18996 KB |
Output is correct |
47 |
Correct |
223 ms |
20672 KB |
Output is correct |
48 |
Correct |
278 ms |
32080 KB |
Output is correct |
49 |
Correct |
268 ms |
31060 KB |
Output is correct |
50 |
Correct |
198 ms |
20564 KB |
Output is correct |
51 |
Correct |
245 ms |
20672 KB |
Output is correct |
52 |
Correct |
252 ms |
20564 KB |
Output is correct |
53 |
Correct |
389 ms |
19540 KB |
Output is correct |
54 |
Correct |
176 ms |
20232 KB |
Output is correct |
55 |
Correct |
29 ms |
3420 KB |
Output is correct |
56 |
Correct |
16 ms |
3308 KB |
Output is correct |
57 |
Correct |
139 ms |
12768 KB |
Output is correct |
58 |
Correct |
47 ms |
12448 KB |
Output is correct |
59 |
Correct |
435 ms |
27124 KB |
Output is correct |
60 |
Correct |
1153 ms |
51016 KB |
Output is correct |
61 |
Correct |
299 ms |
20820 KB |
Output is correct |
62 |
Correct |
379 ms |
21128 KB |
Output is correct |
63 |
Correct |
430 ms |
21140 KB |
Output is correct |
64 |
Correct |
1688 ms |
32296 KB |
Output is correct |
65 |
Correct |
257 ms |
21616 KB |
Output is correct |
66 |
Correct |
789 ms |
29768 KB |
Output is correct |
67 |
Correct |
137 ms |
21540 KB |
Output is correct |
68 |
Correct |
589 ms |
28796 KB |
Output is correct |
69 |
Correct |
537 ms |
28500 KB |
Output is correct |
70 |
Correct |
539 ms |
27472 KB |
Output is correct |