#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ln '\n'
const int N = 2e5 + 5;
const int LG = 20;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int n, k;
int h[N][2], l[N];
int sz[N], ans = INF;
vector<pair<int, int>> adj[N];
bitset<N> removed;
map<ll, int> mn;
void init(){
cin >> n >> k;
for (int i = 0; i < n-1; i++) cin >> h[i][0] >> h[i][1] >> l[i];
}
int get_size(int u, int p){
sz[u] = 1;
for (auto& [v, e]: adj[u]){
if (v == p || removed[v]) continue;
sz[u] += get_size(v, u);
}
return sz[u];
}
int get_centroid(int u, int p, int s){
for (auto& [v, e]: adj[u]){
if (v == p || removed[v]) continue;
if (sz[v] > s / 2) return get_centroid(v, u, s);
}
return u;
}
void dfs(int u, int p, int dep, int cur_len, int m){
// cout << u << ' ' << p << ' ' << dep << ' ' << cur_len << ' ' << m << ln;
if (cur_len > k) return;
if (mn.find(k-cur_len) == mn.end()) mn[k-cur_len] = INF;
if (mn.find(cur_len) == mn.end()) mn[cur_len] = INF;
if (m == 1) {ans = min(ans, dep + mn[k-cur_len]);}
else mn[cur_len] = min(mn[cur_len], dep);
for (auto& [v, e]: adj[u]){
if (v == p || removed[v]) continue;
dfs(v, u, dep+1, cur_len + e, m);
}
}
void build(int u){
int c = get_centroid(u, -1, get_size(u, -1));
// cout << "centroid " << c << ln;
removed[c] = 1;
for (auto& [v, e]: adj[c]){
if (removed[v]) continue;
dfs(v, c, 1, e, 1);
dfs(v, c, 1, e, 0);
}
// cout << ans << ln;
mn.clear();
mn[0] = 0;
for (auto& [v, e]: adj[c]){
if (removed[v]) continue;
build(v);
}
}
int solve(){
for (int i = 0; i < n-1; i++){
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
mn[0] = 0;
build(1);
return ((ans == INF)? -1: ans);
}
void init(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for (int i = 0; i < n-1; i++){
h[i][0] = H[i][0];
h[i][1] = H[i][1];
l[i] = L[i];
}
}
int best_path(int N, int K, int H[][2], int L[])
{
init(N, K, H, L);
return solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5048 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4972 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5048 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4972 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
6 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5028 KB |
Output is correct |
28 |
Correct |
5 ms |
5160 KB |
Output is correct |
29 |
Correct |
5 ms |
5216 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5160 KB |
Output is correct |
32 |
Correct |
5 ms |
5240 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5212 KB |
Output is correct |
38 |
Correct |
6 ms |
5220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5048 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4972 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
224 ms |
12784 KB |
Output is correct |
20 |
Correct |
222 ms |
12776 KB |
Output is correct |
21 |
Correct |
250 ms |
12804 KB |
Output is correct |
22 |
Correct |
222 ms |
12892 KB |
Output is correct |
23 |
Correct |
103 ms |
13116 KB |
Output is correct |
24 |
Correct |
81 ms |
13004 KB |
Output is correct |
25 |
Correct |
175 ms |
17896 KB |
Output is correct |
26 |
Correct |
121 ms |
18500 KB |
Output is correct |
27 |
Correct |
215 ms |
20848 KB |
Output is correct |
28 |
Correct |
249 ms |
31484 KB |
Output is correct |
29 |
Correct |
242 ms |
27320 KB |
Output is correct |
30 |
Correct |
218 ms |
20952 KB |
Output is correct |
31 |
Correct |
224 ms |
20908 KB |
Output is correct |
32 |
Correct |
229 ms |
21052 KB |
Output is correct |
33 |
Correct |
317 ms |
19720 KB |
Output is correct |
34 |
Correct |
138 ms |
20656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5048 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4972 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
6 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5028 KB |
Output is correct |
28 |
Correct |
5 ms |
5160 KB |
Output is correct |
29 |
Correct |
5 ms |
5216 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5160 KB |
Output is correct |
32 |
Correct |
5 ms |
5240 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5212 KB |
Output is correct |
38 |
Correct |
6 ms |
5220 KB |
Output is correct |
39 |
Correct |
224 ms |
12784 KB |
Output is correct |
40 |
Correct |
222 ms |
12776 KB |
Output is correct |
41 |
Correct |
250 ms |
12804 KB |
Output is correct |
42 |
Correct |
222 ms |
12892 KB |
Output is correct |
43 |
Correct |
103 ms |
13116 KB |
Output is correct |
44 |
Correct |
81 ms |
13004 KB |
Output is correct |
45 |
Correct |
175 ms |
17896 KB |
Output is correct |
46 |
Correct |
121 ms |
18500 KB |
Output is correct |
47 |
Correct |
215 ms |
20848 KB |
Output is correct |
48 |
Correct |
249 ms |
31484 KB |
Output is correct |
49 |
Correct |
242 ms |
27320 KB |
Output is correct |
50 |
Correct |
218 ms |
20952 KB |
Output is correct |
51 |
Correct |
224 ms |
20908 KB |
Output is correct |
52 |
Correct |
229 ms |
21052 KB |
Output is correct |
53 |
Correct |
317 ms |
19720 KB |
Output is correct |
54 |
Correct |
138 ms |
20656 KB |
Output is correct |
55 |
Correct |
37 ms |
6064 KB |
Output is correct |
56 |
Correct |
23 ms |
5764 KB |
Output is correct |
57 |
Correct |
215 ms |
13008 KB |
Output is correct |
58 |
Correct |
45 ms |
12748 KB |
Output is correct |
59 |
Correct |
782 ms |
27376 KB |
Output is correct |
60 |
Correct |
2422 ms |
53964 KB |
Output is correct |
61 |
Correct |
375 ms |
21412 KB |
Output is correct |
62 |
Correct |
470 ms |
21496 KB |
Output is correct |
63 |
Correct |
600 ms |
21496 KB |
Output is correct |
64 |
Correct |
2301 ms |
33448 KB |
Output is correct |
65 |
Correct |
134 ms |
21840 KB |
Output is correct |
66 |
Correct |
1255 ms |
31200 KB |
Output is correct |
67 |
Correct |
133 ms |
21788 KB |
Output is correct |
68 |
Correct |
753 ms |
31324 KB |
Output is correct |
69 |
Correct |
740 ms |
31180 KB |
Output is correct |
70 |
Correct |
677 ms |
30332 KB |
Output is correct |