#include <iostream>
#include <vector>
using namespace std;
const int NMAX = 2e5;
const int INF = 1e9;
const int KMAX = 1e6;
int totalSize;
vector<pair<int, int>> edges[NMAX];
int sz[NMAX];
int centroidParent[NMAX];
bool isIn[NMAX];
int minimumDist[KMAX];
vector<long long> used;
int kk;
int ans;
int dfsCalcSize(int node, int parent) {
sz[node] = 1;
for(auto child : edges[node]) {
if(child.first != parent && isIn[child.first]) {
sz[node] += dfsCalcSize(child.first, node);
}
}
return sz[node];
}
int dfsDetCentroid(int node, int parent) {
for(auto child : edges[node]) {
if(child.first != parent && sz[child.first] > totalSize / 2 && isIn[child.first]) {
return dfsDetCentroid(child.first, node);
}
}
return node;
}
void computeDist(int node, int parent, int depth, long long sum, int flag) {
if(sum > kk)
return;
used.push_back(sum);
if(flag == true) {
ans = min(ans, minimumDist[kk - sum] + depth);
} else {
minimumDist[sum] = min(minimumDist[sum], depth);
}
for(auto child : edges[node]) {
if(isIn[child.first] && child.first != parent) {
computeDist(child.first, node, depth + 1, sum + child.second, flag);
}
}
}
void decompose(int root, int parent) {
totalSize = dfsCalcSize(root, root);
int centroid = dfsDetCentroid(root, root);
isIn[centroid] = false;
minimumDist[0] = 0;
for(auto child : edges[centroid]) {
if(isIn[child.first]) {
computeDist(child.first, centroid, 1, child.second, true);
computeDist(child.first, centroid, 1, child.second, false);
}
}
for(auto x : used) {
minimumDist[x] = INF;
}
used.clear();
centroidParent[centroid] = parent;
for(auto child : edges[centroid]) {
if(isIn[child.first])
decompose(child.first, centroid);
}
}
int best_path(int n, int k, int h[][2], int len[]) {
for(int i = 0; i < n - 1; i++) {
edges[h[i][0]].push_back({h[i][1], len[i]});
edges[h[i][1]].push_back({h[i][0], len[i]});
}
kk = k;
for(int i = 0; i <= k; i++)
minimumDist[i] = INF;
for(int i = 0; i < n; i++)
isIn[i] = true;
ans = INF;
decompose(0, -1);
if(ans == INF)
ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12376 KB |
Output is correct |
2 |
Correct |
2 ms |
12376 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
3 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12380 KB |
Output is correct |
9 |
Correct |
2 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12380 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
4 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
2 ms |
12380 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
2 ms |
12392 KB |
Output is correct |
18 |
Correct |
2 ms |
12296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12376 KB |
Output is correct |
2 |
Correct |
2 ms |
12376 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
3 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12380 KB |
Output is correct |
9 |
Correct |
2 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12380 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
4 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
2 ms |
12380 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
2 ms |
12392 KB |
Output is correct |
18 |
Correct |
2 ms |
12296 KB |
Output is correct |
19 |
Correct |
2 ms |
12380 KB |
Output is correct |
20 |
Correct |
3 ms |
12392 KB |
Output is correct |
21 |
Correct |
3 ms |
12396 KB |
Output is correct |
22 |
Correct |
4 ms |
14428 KB |
Output is correct |
23 |
Correct |
4 ms |
14260 KB |
Output is correct |
24 |
Correct |
3 ms |
14428 KB |
Output is correct |
25 |
Correct |
3 ms |
14428 KB |
Output is correct |
26 |
Correct |
3 ms |
14500 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
3 ms |
14424 KB |
Output is correct |
29 |
Correct |
3 ms |
14428 KB |
Output is correct |
30 |
Correct |
3 ms |
14424 KB |
Output is correct |
31 |
Correct |
4 ms |
14424 KB |
Output is correct |
32 |
Correct |
3 ms |
14424 KB |
Output is correct |
33 |
Correct |
3 ms |
14680 KB |
Output is correct |
34 |
Correct |
3 ms |
14448 KB |
Output is correct |
35 |
Correct |
3 ms |
14428 KB |
Output is correct |
36 |
Correct |
3 ms |
14428 KB |
Output is correct |
37 |
Correct |
3 ms |
14428 KB |
Output is correct |
38 |
Correct |
3 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12376 KB |
Output is correct |
2 |
Correct |
2 ms |
12376 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
3 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12380 KB |
Output is correct |
9 |
Correct |
2 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12380 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
4 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
2 ms |
12380 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
2 ms |
12392 KB |
Output is correct |
18 |
Correct |
2 ms |
12296 KB |
Output is correct |
19 |
Correct |
79 ms |
19884 KB |
Output is correct |
20 |
Correct |
88 ms |
19612 KB |
Output is correct |
21 |
Correct |
85 ms |
20612 KB |
Output is correct |
22 |
Correct |
73 ms |
21704 KB |
Output is correct |
23 |
Correct |
53 ms |
19960 KB |
Output is correct |
24 |
Correct |
41 ms |
19800 KB |
Output is correct |
25 |
Correct |
78 ms |
22492 KB |
Output is correct |
26 |
Correct |
74 ms |
27340 KB |
Output is correct |
27 |
Correct |
107 ms |
25096 KB |
Output is correct |
28 |
Correct |
162 ms |
36436 KB |
Output is correct |
29 |
Correct |
159 ms |
35416 KB |
Output is correct |
30 |
Correct |
96 ms |
25216 KB |
Output is correct |
31 |
Correct |
109 ms |
25220 KB |
Output is correct |
32 |
Correct |
138 ms |
25076 KB |
Output is correct |
33 |
Correct |
141 ms |
23968 KB |
Output is correct |
34 |
Correct |
105 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12376 KB |
Output is correct |
2 |
Correct |
2 ms |
12376 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
3 ms |
12380 KB |
Output is correct |
5 |
Correct |
3 ms |
12380 KB |
Output is correct |
6 |
Correct |
3 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12380 KB |
Output is correct |
8 |
Correct |
2 ms |
12380 KB |
Output is correct |
9 |
Correct |
2 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12380 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
4 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
2 ms |
12380 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
2 ms |
12392 KB |
Output is correct |
18 |
Correct |
2 ms |
12296 KB |
Output is correct |
19 |
Correct |
2 ms |
12380 KB |
Output is correct |
20 |
Correct |
3 ms |
12392 KB |
Output is correct |
21 |
Correct |
3 ms |
12396 KB |
Output is correct |
22 |
Correct |
4 ms |
14428 KB |
Output is correct |
23 |
Correct |
4 ms |
14260 KB |
Output is correct |
24 |
Correct |
3 ms |
14428 KB |
Output is correct |
25 |
Correct |
3 ms |
14428 KB |
Output is correct |
26 |
Correct |
3 ms |
14500 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
3 ms |
14424 KB |
Output is correct |
29 |
Correct |
3 ms |
14428 KB |
Output is correct |
30 |
Correct |
3 ms |
14424 KB |
Output is correct |
31 |
Correct |
4 ms |
14424 KB |
Output is correct |
32 |
Correct |
3 ms |
14424 KB |
Output is correct |
33 |
Correct |
3 ms |
14680 KB |
Output is correct |
34 |
Correct |
3 ms |
14448 KB |
Output is correct |
35 |
Correct |
3 ms |
14428 KB |
Output is correct |
36 |
Correct |
3 ms |
14428 KB |
Output is correct |
37 |
Correct |
3 ms |
14428 KB |
Output is correct |
38 |
Correct |
3 ms |
14424 KB |
Output is correct |
39 |
Correct |
79 ms |
19884 KB |
Output is correct |
40 |
Correct |
88 ms |
19612 KB |
Output is correct |
41 |
Correct |
85 ms |
20612 KB |
Output is correct |
42 |
Correct |
73 ms |
21704 KB |
Output is correct |
43 |
Correct |
53 ms |
19960 KB |
Output is correct |
44 |
Correct |
41 ms |
19800 KB |
Output is correct |
45 |
Correct |
78 ms |
22492 KB |
Output is correct |
46 |
Correct |
74 ms |
27340 KB |
Output is correct |
47 |
Correct |
107 ms |
25096 KB |
Output is correct |
48 |
Correct |
162 ms |
36436 KB |
Output is correct |
49 |
Correct |
159 ms |
35416 KB |
Output is correct |
50 |
Correct |
96 ms |
25216 KB |
Output is correct |
51 |
Correct |
109 ms |
25220 KB |
Output is correct |
52 |
Correct |
138 ms |
25076 KB |
Output is correct |
53 |
Correct |
141 ms |
23968 KB |
Output is correct |
54 |
Correct |
105 ms |
24656 KB |
Output is correct |
55 |
Correct |
8 ms |
13148 KB |
Output is correct |
56 |
Correct |
9 ms |
13236 KB |
Output is correct |
57 |
Correct |
62 ms |
22048 KB |
Output is correct |
58 |
Correct |
30 ms |
21748 KB |
Output is correct |
59 |
Correct |
68 ms |
29400 KB |
Output is correct |
60 |
Correct |
271 ms |
42692 KB |
Output is correct |
61 |
Correct |
140 ms |
26308 KB |
Output is correct |
62 |
Correct |
166 ms |
32012 KB |
Output is correct |
63 |
Correct |
141 ms |
32492 KB |
Output is correct |
64 |
Correct |
305 ms |
33472 KB |
Output is correct |
65 |
Correct |
152 ms |
25884 KB |
Output is correct |
66 |
Correct |
234 ms |
35780 KB |
Output is correct |
67 |
Correct |
99 ms |
33568 KB |
Output is correct |
68 |
Correct |
174 ms |
30148 KB |
Output is correct |
69 |
Correct |
211 ms |
30376 KB |
Output is correct |
70 |
Correct |
125 ms |
29640 KB |
Output is correct |