#include "race.h"
// #include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define printmp(a) for(auto x : a) cout << x.first << ' ' << x.second << endl;
vector<vector<array<int, 2>>> graph;
vector<int> subtree_size;
vector<bool> removed, used;
struct Centroid_Decomposition{
void sub_size(int node, int par){
subtree_size[node] = 1;
for(auto &child : graph[node]){
if(child[0] != par and !removed[child[0]]){
sub_size(child[0], node);
subtree_size[node] += subtree_size[child[0]];
}
}
}
int center(int node, int par, int size){
for(auto [x, y] : graph[node]){
if(x != par and !removed[x]){
if(subtree_size[x] * 2 > size)
return center(x, node, size);
}
}
return node;
}
int find_center(int node){
sub_size(node, -1);
return center(node, -1, subtree_size[node]);
}
int dfs(int node, int k){
int center = find_center(node);
removed[center] = true;
int ans = 1e9;
map<int, int> mp;
mp[0] = 0;
for(auto y : graph[center]){
if(!removed[y[0]] and y[1] <= k){
queue<array<int, 3>> q;
q.push({y[0], y[1], 1});
used[y[0]] = 1;
while(!q.empty()){
array<int, 3> cur = q.front();
q.pop();
if(mp.count(k - cur[1]) == 1)
ans = min(ans, mp[k - cur[1]] + cur[2]);
for(auto x : graph[cur[0]]){
if(!used[x[0]] and !removed[x[0]] and cur[1] + x[1] <= k){
q.push({x[0], cur[1] + x[1], cur[2] + 1});
used[x[0]] = 1;
}
}
}
q.push({y[0], y[1], 1});
used[y[0]] = 0;
while(!q.empty()){
array<int, 3> cur = q.front();
q.pop();
if(mp.count(cur[1]) == 0)
mp[cur[1]] = cur[2];
else
mp[cur[1]] = min(mp[cur[1]], cur[2]);
for(auto x : graph[cur[0]]){
if(used[x[0]] and !removed[x[0]] and cur[1] + x[1] <= k){
q.push({x[0], cur[1] + x[1], cur[2] + 1});
used[x[0]] = 0;
}
}
}
}
}
for(auto y : graph[center])
if(!removed[y[0]])
ans = min(ans, dfs(y[0], k));
return ans;
}
int get(int k){
return dfs(0, k);
}
};
int best_path(int n, int k, int h[][2], int l[]){
graph.resize(n);
subtree_size.resize(n);
removed.resize(n);
used.resize(n);
for(int i = 0; i < n - 1; i++){
graph[h[i][0]].push_back({h[i][1], l[i]});
graph[h[i][1]].push_back({h[i][0], l[i]});
}
Centroid_Decomposition cd;
int ans = cd.get(k);
if(ans == 1e9)
return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2504 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2504 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 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 |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
1 ms |
2648 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2504 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
132 ms |
11344 KB |
Output is correct |
20 |
Correct |
135 ms |
12112 KB |
Output is correct |
21 |
Correct |
139 ms |
11936 KB |
Output is correct |
22 |
Correct |
130 ms |
12628 KB |
Output is correct |
23 |
Correct |
65 ms |
12820 KB |
Output is correct |
24 |
Correct |
54 ms |
12376 KB |
Output is correct |
25 |
Correct |
114 ms |
13404 KB |
Output is correct |
26 |
Correct |
96 ms |
14416 KB |
Output is correct |
27 |
Correct |
126 ms |
19092 KB |
Output is correct |
28 |
Correct |
156 ms |
24404 KB |
Output is correct |
29 |
Correct |
168 ms |
24272 KB |
Output is correct |
30 |
Correct |
133 ms |
20052 KB |
Output is correct |
31 |
Correct |
130 ms |
19796 KB |
Output is correct |
32 |
Correct |
132 ms |
19712 KB |
Output is correct |
33 |
Correct |
192 ms |
18600 KB |
Output is correct |
34 |
Correct |
103 ms |
18940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2504 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 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 |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2636 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
1 ms |
2648 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
132 ms |
11344 KB |
Output is correct |
40 |
Correct |
135 ms |
12112 KB |
Output is correct |
41 |
Correct |
139 ms |
11936 KB |
Output is correct |
42 |
Correct |
130 ms |
12628 KB |
Output is correct |
43 |
Correct |
65 ms |
12820 KB |
Output is correct |
44 |
Correct |
54 ms |
12376 KB |
Output is correct |
45 |
Correct |
114 ms |
13404 KB |
Output is correct |
46 |
Correct |
96 ms |
14416 KB |
Output is correct |
47 |
Correct |
126 ms |
19092 KB |
Output is correct |
48 |
Correct |
156 ms |
24404 KB |
Output is correct |
49 |
Correct |
168 ms |
24272 KB |
Output is correct |
50 |
Correct |
133 ms |
20052 KB |
Output is correct |
51 |
Correct |
130 ms |
19796 KB |
Output is correct |
52 |
Correct |
132 ms |
19712 KB |
Output is correct |
53 |
Correct |
192 ms |
18600 KB |
Output is correct |
54 |
Correct |
103 ms |
18940 KB |
Output is correct |
55 |
Correct |
14 ms |
3628 KB |
Output is correct |
56 |
Correct |
10 ms |
3164 KB |
Output is correct |
57 |
Correct |
103 ms |
11792 KB |
Output is correct |
58 |
Correct |
33 ms |
12456 KB |
Output is correct |
59 |
Correct |
259 ms |
21556 KB |
Output is correct |
60 |
Correct |
625 ms |
41304 KB |
Output is correct |
61 |
Correct |
189 ms |
19264 KB |
Output is correct |
62 |
Correct |
207 ms |
19928 KB |
Output is correct |
63 |
Correct |
234 ms |
19796 KB |
Output is correct |
64 |
Correct |
617 ms |
29680 KB |
Output is correct |
65 |
Correct |
103 ms |
20820 KB |
Output is correct |
66 |
Correct |
414 ms |
22764 KB |
Output is correct |
67 |
Correct |
82 ms |
21444 KB |
Output is correct |
68 |
Correct |
278 ms |
26968 KB |
Output is correct |
69 |
Correct |
261 ms |
24912 KB |
Output is correct |
70 |
Correct |
264 ms |
26732 KB |
Output is correct |