#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int NN = 2e5 + 5;
bool used[NN];
int sz[NN];
int n, k;
vector<pair<int, int>> edges[NN];
void dfs_size(int u, int p){
sz[u] = 1;
for(auto X : edges[u]){
int v = X.first;
if(used[v] || v == p)continue;
dfs_size(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int size){
for(auto X : edges[u]){
int v = X.first;
if(used[v] || v == p)continue;
if(sz[v] > size/2)return centroid(v, u, size);
}
return u;
}
vector<pair<int, int>> m;
map<int, int> len;
void dfs(int u, int p, int dist, int t){
if(dist > k)return;
m.push_back({dist, t});
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(used[v] || v == p)continue;
dfs(v, u, dist+cost, t+1);
}
}
void dfs2(int u, int p, int dist, int t){
if(dist > k)return;
if(!len.count(dist))len[dist] = t;
else len[dist] = min(len[dist], t);
for(auto X : edges[u]){
int v = X.first;
int cost = X.second;
if(used[v] || v == p)continue;
dfs2(v, u, dist+cost, t+1);
}
}
int find_min(int u, int p=-1){
int ans = 1e9;
len.clear();
dfs_size(u, u);
u = centroid(u, u, sz[u]);
len[0] = 0;
used[u] = true;//delete centroid
for(auto X : edges[u]){
m.clear();
int v = X.first;
int cost = X.second;
if(used[v])continue;
dfs(v, u, cost, 1);
for(auto X : m){
int dist = X.first;
int time = X.second;
if(len.count(k-dist)){
ans = min(ans, len[k-dist]+time);
}//cout << dist << ' ' << time << '\n';
}
dfs2(v, u, cost, 1);
}
for(auto X : edges[u]){
int v = X.first;
if(used[v] || p == v)continue;
ans = min(ans, find_min(v, u));
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i=0;i<n-1;i++){
edges[H[i][0]].push_back({H[i][1],L[i]});
edges[H[i][1]].push_back({H[i][0],L[i]});
}
if(k == 0){
return 0;
}
int ans = find_min(0);
// cout << '\n' << ans << '\n';
if(ans == 1e9)ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
5 ms |
5024 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5120 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
7 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
266 ms |
10488 KB |
Output is correct |
20 |
Correct |
252 ms |
10616 KB |
Output is correct |
21 |
Correct |
293 ms |
10876 KB |
Output is correct |
22 |
Correct |
288 ms |
10872 KB |
Output is correct |
23 |
Correct |
122 ms |
10744 KB |
Output is correct |
24 |
Correct |
111 ms |
10676 KB |
Output is correct |
25 |
Correct |
182 ms |
13304 KB |
Output is correct |
26 |
Correct |
140 ms |
16760 KB |
Output is correct |
27 |
Correct |
281 ms |
16248 KB |
Output is correct |
28 |
Correct |
351 ms |
27128 KB |
Output is correct |
29 |
Correct |
341 ms |
26360 KB |
Output is correct |
30 |
Correct |
274 ms |
15992 KB |
Output is correct |
31 |
Correct |
274 ms |
16160 KB |
Output is correct |
32 |
Correct |
310 ms |
16088 KB |
Output is correct |
33 |
Correct |
385 ms |
14968 KB |
Output is correct |
34 |
Correct |
247 ms |
14840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
5 ms |
5024 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5120 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5120 KB |
Output is correct |
33 |
Correct |
7 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
266 ms |
10488 KB |
Output is correct |
40 |
Correct |
252 ms |
10616 KB |
Output is correct |
41 |
Correct |
293 ms |
10876 KB |
Output is correct |
42 |
Correct |
288 ms |
10872 KB |
Output is correct |
43 |
Correct |
122 ms |
10744 KB |
Output is correct |
44 |
Correct |
111 ms |
10676 KB |
Output is correct |
45 |
Correct |
182 ms |
13304 KB |
Output is correct |
46 |
Correct |
140 ms |
16760 KB |
Output is correct |
47 |
Correct |
281 ms |
16248 KB |
Output is correct |
48 |
Correct |
351 ms |
27128 KB |
Output is correct |
49 |
Correct |
341 ms |
26360 KB |
Output is correct |
50 |
Correct |
274 ms |
15992 KB |
Output is correct |
51 |
Correct |
274 ms |
16160 KB |
Output is correct |
52 |
Correct |
310 ms |
16088 KB |
Output is correct |
53 |
Correct |
385 ms |
14968 KB |
Output is correct |
54 |
Correct |
247 ms |
14840 KB |
Output is correct |
55 |
Correct |
26 ms |
5864 KB |
Output is correct |
56 |
Correct |
20 ms |
5760 KB |
Output is correct |
57 |
Correct |
189 ms |
11228 KB |
Output is correct |
58 |
Correct |
62 ms |
10732 KB |
Output is correct |
59 |
Correct |
362 ms |
20080 KB |
Output is correct |
60 |
Correct |
1168 ms |
36072 KB |
Output is correct |
61 |
Correct |
378 ms |
16248 KB |
Output is correct |
62 |
Correct |
414 ms |
16120 KB |
Output is correct |
63 |
Correct |
503 ms |
16120 KB |
Output is correct |
64 |
Correct |
1372 ms |
21228 KB |
Output is correct |
65 |
Correct |
316 ms |
15992 KB |
Output is correct |
66 |
Correct |
755 ms |
24444 KB |
Output is correct |
67 |
Correct |
195 ms |
16356 KB |
Output is correct |
68 |
Correct |
559 ms |
19704 KB |
Output is correct |
69 |
Correct |
563 ms |
19832 KB |
Output is correct |
70 |
Correct |
527 ms |
19192 KB |
Output is correct |