#include <bits/stdc++.h>
#include "race.h"
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;
}
/*int h[NMAX][2];
int len[NMAX];
int main() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n - 1; i++)
cin >> h[i][0] >> h[i][1];
for(int i = 0; i < n - 1; i++)
cin >> len[i];
cout << best_path(n, k, h, len);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
2 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 |
3 ms |
12380 KB |
Output is correct |
9 |
Correct |
3 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
2 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
3 ms |
12376 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
3 ms |
12392 KB |
Output is correct |
18 |
Correct |
3 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
2 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 |
3 ms |
12380 KB |
Output is correct |
9 |
Correct |
3 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
2 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
3 ms |
12376 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
3 ms |
12392 KB |
Output is correct |
18 |
Correct |
3 ms |
12380 KB |
Output is correct |
19 |
Correct |
2 ms |
12376 KB |
Output is correct |
20 |
Correct |
3 ms |
12628 KB |
Output is correct |
21 |
Correct |
3 ms |
12376 KB |
Output is correct |
22 |
Correct |
3 ms |
14428 KB |
Output is correct |
23 |
Correct |
3 ms |
14424 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 |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14456 KB |
Output is correct |
28 |
Correct |
3 ms |
14428 KB |
Output is correct |
29 |
Correct |
3 ms |
14428 KB |
Output is correct |
30 |
Correct |
3 ms |
14428 KB |
Output is correct |
31 |
Correct |
3 ms |
14428 KB |
Output is correct |
32 |
Correct |
3 ms |
14428 KB |
Output is correct |
33 |
Correct |
4 ms |
14424 KB |
Output is correct |
34 |
Correct |
3 ms |
14428 KB |
Output is correct |
35 |
Correct |
3 ms |
14448 KB |
Output is correct |
36 |
Correct |
3 ms |
14424 KB |
Output is correct |
37 |
Correct |
3 ms |
14428 KB |
Output is correct |
38 |
Correct |
4 ms |
14484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
2 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 |
3 ms |
12380 KB |
Output is correct |
9 |
Correct |
3 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
2 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
3 ms |
12376 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
3 ms |
12392 KB |
Output is correct |
18 |
Correct |
3 ms |
12380 KB |
Output is correct |
19 |
Correct |
73 ms |
19932 KB |
Output is correct |
20 |
Correct |
72 ms |
19800 KB |
Output is correct |
21 |
Correct |
79 ms |
20588 KB |
Output is correct |
22 |
Correct |
84 ms |
21704 KB |
Output is correct |
23 |
Correct |
45 ms |
19932 KB |
Output is correct |
24 |
Correct |
35 ms |
19804 KB |
Output is correct |
25 |
Correct |
78 ms |
22292 KB |
Output is correct |
26 |
Correct |
68 ms |
27348 KB |
Output is correct |
27 |
Correct |
97 ms |
25180 KB |
Output is correct |
28 |
Correct |
150 ms |
36176 KB |
Output is correct |
29 |
Correct |
148 ms |
35412 KB |
Output is correct |
30 |
Correct |
124 ms |
25172 KB |
Output is correct |
31 |
Correct |
99 ms |
25216 KB |
Output is correct |
32 |
Correct |
113 ms |
25168 KB |
Output is correct |
33 |
Correct |
121 ms |
23888 KB |
Output is correct |
34 |
Correct |
102 ms |
24696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12380 KB |
Output is correct |
2 |
Correct |
2 ms |
12380 KB |
Output is correct |
3 |
Correct |
2 ms |
12380 KB |
Output is correct |
4 |
Correct |
2 ms |
12380 KB |
Output is correct |
5 |
Correct |
2 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 |
3 ms |
12380 KB |
Output is correct |
9 |
Correct |
3 ms |
12380 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12380 KB |
Output is correct |
12 |
Correct |
2 ms |
12380 KB |
Output is correct |
13 |
Correct |
2 ms |
12380 KB |
Output is correct |
14 |
Correct |
3 ms |
12380 KB |
Output is correct |
15 |
Correct |
3 ms |
12376 KB |
Output is correct |
16 |
Correct |
2 ms |
12380 KB |
Output is correct |
17 |
Correct |
3 ms |
12392 KB |
Output is correct |
18 |
Correct |
3 ms |
12380 KB |
Output is correct |
19 |
Correct |
2 ms |
12376 KB |
Output is correct |
20 |
Correct |
3 ms |
12628 KB |
Output is correct |
21 |
Correct |
3 ms |
12376 KB |
Output is correct |
22 |
Correct |
3 ms |
14428 KB |
Output is correct |
23 |
Correct |
3 ms |
14424 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 |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14456 KB |
Output is correct |
28 |
Correct |
3 ms |
14428 KB |
Output is correct |
29 |
Correct |
3 ms |
14428 KB |
Output is correct |
30 |
Correct |
3 ms |
14428 KB |
Output is correct |
31 |
Correct |
3 ms |
14428 KB |
Output is correct |
32 |
Correct |
3 ms |
14428 KB |
Output is correct |
33 |
Correct |
4 ms |
14424 KB |
Output is correct |
34 |
Correct |
3 ms |
14428 KB |
Output is correct |
35 |
Correct |
3 ms |
14448 KB |
Output is correct |
36 |
Correct |
3 ms |
14424 KB |
Output is correct |
37 |
Correct |
3 ms |
14428 KB |
Output is correct |
38 |
Correct |
4 ms |
14484 KB |
Output is correct |
39 |
Correct |
73 ms |
19932 KB |
Output is correct |
40 |
Correct |
72 ms |
19800 KB |
Output is correct |
41 |
Correct |
79 ms |
20588 KB |
Output is correct |
42 |
Correct |
84 ms |
21704 KB |
Output is correct |
43 |
Correct |
45 ms |
19932 KB |
Output is correct |
44 |
Correct |
35 ms |
19804 KB |
Output is correct |
45 |
Correct |
78 ms |
22292 KB |
Output is correct |
46 |
Correct |
68 ms |
27348 KB |
Output is correct |
47 |
Correct |
97 ms |
25180 KB |
Output is correct |
48 |
Correct |
150 ms |
36176 KB |
Output is correct |
49 |
Correct |
148 ms |
35412 KB |
Output is correct |
50 |
Correct |
124 ms |
25172 KB |
Output is correct |
51 |
Correct |
99 ms |
25216 KB |
Output is correct |
52 |
Correct |
113 ms |
25168 KB |
Output is correct |
53 |
Correct |
121 ms |
23888 KB |
Output is correct |
54 |
Correct |
102 ms |
24696 KB |
Output is correct |
55 |
Correct |
8 ms |
13144 KB |
Output is correct |
56 |
Correct |
10 ms |
13172 KB |
Output is correct |
57 |
Correct |
55 ms |
21960 KB |
Output is correct |
58 |
Correct |
29 ms |
21580 KB |
Output is correct |
59 |
Correct |
75 ms |
29388 KB |
Output is correct |
60 |
Correct |
269 ms |
42180 KB |
Output is correct |
61 |
Correct |
124 ms |
26368 KB |
Output is correct |
62 |
Correct |
121 ms |
32708 KB |
Output is correct |
63 |
Correct |
145 ms |
32424 KB |
Output is correct |
64 |
Correct |
262 ms |
33136 KB |
Output is correct |
65 |
Correct |
108 ms |
25960 KB |
Output is correct |
66 |
Correct |
224 ms |
35780 KB |
Output is correct |
67 |
Correct |
75 ms |
33632 KB |
Output is correct |
68 |
Correct |
145 ms |
30152 KB |
Output is correct |
69 |
Correct |
137 ms |
30152 KB |
Output is correct |
70 |
Correct |
127 ms |
29640 KB |
Output is correct |