#include <bits/stdc++.h>
#include "race.h"
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
const int oo = 1e9 + 7;
int n, k;
vector<ii> adj[200001];
int sub[200001];
int res = oo;
int calc[1000001], mx;
bool rem[200001];
/// CENTROID DECOMPOSITION :
int dfs_size(int v, int p) {
sub[v] = 1;
for(auto u : adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
sub[v] += dfs_size(u.ff, v);
}
return sub[v];
}
int centroid(int v, int p, int _n) {
for(auto u : adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
if(sub[u.ff] >= _n)
return centroid(u.ff, v, _n);
}
return v;
}
void dfs(int v, int p, bool state, int curr, int len) {
if(len > k) return;
mx = max(mx, len);
if(state) calc[len] = min(calc[len], curr);
else res = min(res, curr + calc[k - len]);
for(auto u : adj[v]) {
if(u.ff == p || rem[u.ff]) continue;
dfs(u.ff, v, state, curr + 1, len + u.ss);
}
}
void build(int v = 0) {
int _n = dfs_size(v, -1) >> 1;
int c = centroid(v, -1, _n);
rem[c] = 1; mx = 0;
for(auto u : adj[c]) {
if(rem[u.ff]) continue;
dfs(u.ff, c, 0, 1, u.ss);
dfs(u.ff, c, 1, 1, u.ss);
}
fill(calc + 1, calc + mx + 1, oo);
for(auto u : adj[c]) {
if(rem[u.ff]) continue;
build(u.ff);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for(int l = 0; l < n - 1; l++) {
int u = H[l][0], v = H[l][1], w = L[l];
adj[u].pb({v, w}); adj[v].pb({u, w});
}
fill(calc + 1, calc + 1000001, oo);
build();
return (res != oo ? res : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8844 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8844 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8840 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8844 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8844 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8840 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
5 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
116 ms |
8900 KB |
Output is correct |
23 |
Correct |
82 ms |
8996 KB |
Output is correct |
24 |
Correct |
114 ms |
9008 KB |
Output is correct |
25 |
Correct |
20 ms |
8916 KB |
Output is correct |
26 |
Correct |
44 ms |
8988 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
18 ms |
9024 KB |
Output is correct |
29 |
Correct |
19 ms |
8988 KB |
Output is correct |
30 |
Correct |
19 ms |
9020 KB |
Output is correct |
31 |
Correct |
19 ms |
9020 KB |
Output is correct |
32 |
Correct |
19 ms |
9016 KB |
Output is correct |
33 |
Correct |
27 ms |
8992 KB |
Output is correct |
34 |
Correct |
25 ms |
8916 KB |
Output is correct |
35 |
Correct |
30 ms |
9020 KB |
Output is correct |
36 |
Correct |
30 ms |
8984 KB |
Output is correct |
37 |
Correct |
10 ms |
8916 KB |
Output is correct |
38 |
Correct |
9 ms |
8976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8844 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8844 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8840 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
122 ms |
17032 KB |
Output is correct |
20 |
Correct |
119 ms |
17032 KB |
Output is correct |
21 |
Correct |
136 ms |
17044 KB |
Output is correct |
22 |
Correct |
131 ms |
17024 KB |
Output is correct |
23 |
Correct |
69 ms |
17464 KB |
Output is correct |
24 |
Correct |
58 ms |
16716 KB |
Output is correct |
25 |
Correct |
118 ms |
20000 KB |
Output is correct |
26 |
Correct |
83 ms |
22860 KB |
Output is correct |
27 |
Correct |
155 ms |
25788 KB |
Output is correct |
28 |
Correct |
246 ms |
37104 KB |
Output is correct |
29 |
Correct |
251 ms |
36252 KB |
Output is correct |
30 |
Correct |
160 ms |
25872 KB |
Output is correct |
31 |
Correct |
164 ms |
25792 KB |
Output is correct |
32 |
Correct |
184 ms |
25992 KB |
Output is correct |
33 |
Correct |
194 ms |
24620 KB |
Output is correct |
34 |
Correct |
177 ms |
25548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8844 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8844 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8840 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
5 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
116 ms |
8900 KB |
Output is correct |
23 |
Correct |
82 ms |
8996 KB |
Output is correct |
24 |
Correct |
114 ms |
9008 KB |
Output is correct |
25 |
Correct |
20 ms |
8916 KB |
Output is correct |
26 |
Correct |
44 ms |
8988 KB |
Output is correct |
27 |
Correct |
5 ms |
8916 KB |
Output is correct |
28 |
Correct |
18 ms |
9024 KB |
Output is correct |
29 |
Correct |
19 ms |
8988 KB |
Output is correct |
30 |
Correct |
19 ms |
9020 KB |
Output is correct |
31 |
Correct |
19 ms |
9020 KB |
Output is correct |
32 |
Correct |
19 ms |
9016 KB |
Output is correct |
33 |
Correct |
27 ms |
8992 KB |
Output is correct |
34 |
Correct |
25 ms |
8916 KB |
Output is correct |
35 |
Correct |
30 ms |
9020 KB |
Output is correct |
36 |
Correct |
30 ms |
8984 KB |
Output is correct |
37 |
Correct |
10 ms |
8916 KB |
Output is correct |
38 |
Correct |
9 ms |
8976 KB |
Output is correct |
39 |
Correct |
122 ms |
17032 KB |
Output is correct |
40 |
Correct |
119 ms |
17032 KB |
Output is correct |
41 |
Correct |
136 ms |
17044 KB |
Output is correct |
42 |
Correct |
131 ms |
17024 KB |
Output is correct |
43 |
Correct |
69 ms |
17464 KB |
Output is correct |
44 |
Correct |
58 ms |
16716 KB |
Output is correct |
45 |
Correct |
118 ms |
20000 KB |
Output is correct |
46 |
Correct |
83 ms |
22860 KB |
Output is correct |
47 |
Correct |
155 ms |
25788 KB |
Output is correct |
48 |
Correct |
246 ms |
37104 KB |
Output is correct |
49 |
Correct |
251 ms |
36252 KB |
Output is correct |
50 |
Correct |
160 ms |
25872 KB |
Output is correct |
51 |
Correct |
164 ms |
25792 KB |
Output is correct |
52 |
Correct |
184 ms |
25992 KB |
Output is correct |
53 |
Correct |
194 ms |
24620 KB |
Output is correct |
54 |
Correct |
177 ms |
25548 KB |
Output is correct |
55 |
Correct |
15 ms |
9748 KB |
Output is correct |
56 |
Correct |
14 ms |
9732 KB |
Output is correct |
57 |
Correct |
64 ms |
17484 KB |
Output is correct |
58 |
Correct |
40 ms |
16328 KB |
Output is correct |
59 |
Correct |
109 ms |
22792 KB |
Output is correct |
60 |
Correct |
415 ms |
36444 KB |
Output is correct |
61 |
Correct |
174 ms |
25928 KB |
Output is correct |
62 |
Correct |
179 ms |
25956 KB |
Output is correct |
63 |
Correct |
229 ms |
25932 KB |
Output is correct |
64 |
Correct |
908 ms |
25872 KB |
Output is correct |
65 |
Correct |
208 ms |
26284 KB |
Output is correct |
66 |
Correct |
571 ms |
34496 KB |
Output is correct |
67 |
Correct |
103 ms |
24996 KB |
Output is correct |
68 |
Correct |
310 ms |
26132 KB |
Output is correct |
69 |
Correct |
322 ms |
25932 KB |
Output is correct |
70 |
Correct |
308 ms |
25292 KB |
Output is correct |