#include "bits/stdc++.h"
#include <race.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 2e5 + 3;
const int K = 1e6 + 3;
int n, k;
vector<pii> g[N];
int sz[N], dep[N], h[N];
int mn[K];
int res = 0x3f3f3f3f;
bool del[N];
int dfs_sz(int u, int pre){
sz[u] = 1;
for(auto [v, w] : g[u])
if(v != pre and !del[v])
sz[u] += dfs_sz(v, u);
return sz[u];
}
int find_centroid(int u, int pre, int n){
for(auto [v, w] : g[u])
if(v != pre and !del[v])
if(2 * sz[v] > n)
return find_centroid(v, u, n);
return u;
}
void dfs(int u, int pre, bool upd){
if(dep[u] > k)
return;
if(upd)
mn[dep[u]] = min(mn[dep[u]], h[u]);
else
res = min(res, h[u] + mn[k - dep[u]]);
for(auto [v, w] : g[u]){
if(v == pre or del[v])
continue;
dep[v] = dep[u] + w;
h[v] = h[u] + 1;
dfs(v, u, upd);
}
}
void clean(int u, int pre){
if(dep[u] > k)
return;
mn[dep[u]] = 0x3f3f3f3f;
for(auto [v, w] : g[u])
if(v != pre and !del[v])
clean(v, u);
}
void calc(int u, int pre){
int c = find_centroid(u, pre, dfs_sz(u, pre));
debug(u, c);
del[c] = 1;
dep[c] = h[c] = 0;
for(auto [v, w] : g[c]){
if(v != pre and !del[v]){
dep[v] = dep[c] + w;
h[v] = h[c] + 1;
dfs(v, c, 0);
dfs(v, c, 1);
}
}
for(auto [v, w] : g[c])
if(v != pre and !del[v])
clean(v, c);
for(auto [v, w] : g[c])
if(v != pre and !del[v])
calc(v, c);
}
void solve(){
cin >> n >> k;
for(int i = 1; i < n; ++i){
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(mn, 0x3f3f3f3f, sizeof(mn));
mn[0] = 0;
calc(0, -1);
debug(res);
cout << (res > n ? -1 : res);
}
int best_path(int _n, int _k, int H[][2], int L[]){
n = _n, k = _k;
memset(mn, 0x3f3f3f3f, sizeof(mn));
for(int i = 0; i < n - 1; ++i){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
mn[0] = 0;
calc(0, -1);
return (res > n ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15036 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
2 ms |
15192 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15036 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
3 ms |
15448 KB |
Output is correct |
10 |
Correct |
2 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
2 ms |
15196 KB |
Output is correct |
15 |
Correct |
2 ms |
15196 KB |
Output is correct |
16 |
Correct |
2 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15036 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
2 ms |
15192 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15036 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
3 ms |
15448 KB |
Output is correct |
10 |
Correct |
2 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
2 ms |
15196 KB |
Output is correct |
15 |
Correct |
2 ms |
15196 KB |
Output is correct |
16 |
Correct |
2 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
2 ms |
15196 KB |
Output is correct |
20 |
Correct |
2 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15252 KB |
Output is correct |
24 |
Correct |
3 ms |
15448 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15292 KB |
Output is correct |
27 |
Correct |
3 ms |
15448 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
3 ms |
15244 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
3 ms |
15196 KB |
Output is correct |
34 |
Correct |
3 ms |
15196 KB |
Output is correct |
35 |
Correct |
3 ms |
15196 KB |
Output is correct |
36 |
Correct |
3 ms |
15196 KB |
Output is correct |
37 |
Correct |
3 ms |
15196 KB |
Output is correct |
38 |
Correct |
3 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15036 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
2 ms |
15192 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15036 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
3 ms |
15448 KB |
Output is correct |
10 |
Correct |
2 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
2 ms |
15196 KB |
Output is correct |
15 |
Correct |
2 ms |
15196 KB |
Output is correct |
16 |
Correct |
2 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
90 ms |
22452 KB |
Output is correct |
20 |
Correct |
90 ms |
22456 KB |
Output is correct |
21 |
Correct |
100 ms |
22364 KB |
Output is correct |
22 |
Correct |
86 ms |
22576 KB |
Output is correct |
23 |
Correct |
49 ms |
22612 KB |
Output is correct |
24 |
Correct |
41 ms |
22676 KB |
Output is correct |
25 |
Correct |
94 ms |
25220 KB |
Output is correct |
26 |
Correct |
77 ms |
28180 KB |
Output is correct |
27 |
Correct |
109 ms |
27784 KB |
Output is correct |
28 |
Correct |
180 ms |
39064 KB |
Output is correct |
29 |
Correct |
172 ms |
38484 KB |
Output is correct |
30 |
Correct |
105 ms |
27836 KB |
Output is correct |
31 |
Correct |
108 ms |
27732 KB |
Output is correct |
32 |
Correct |
125 ms |
27836 KB |
Output is correct |
33 |
Correct |
144 ms |
26760 KB |
Output is correct |
34 |
Correct |
117 ms |
27668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15036 KB |
Output is correct |
3 |
Correct |
2 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
2 ms |
15192 KB |
Output is correct |
6 |
Correct |
2 ms |
15196 KB |
Output is correct |
7 |
Correct |
3 ms |
15036 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
3 ms |
15448 KB |
Output is correct |
10 |
Correct |
2 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
2 ms |
15196 KB |
Output is correct |
13 |
Correct |
3 ms |
15196 KB |
Output is correct |
14 |
Correct |
2 ms |
15196 KB |
Output is correct |
15 |
Correct |
2 ms |
15196 KB |
Output is correct |
16 |
Correct |
2 ms |
15196 KB |
Output is correct |
17 |
Correct |
2 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
2 ms |
15196 KB |
Output is correct |
20 |
Correct |
2 ms |
15196 KB |
Output is correct |
21 |
Correct |
3 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15252 KB |
Output is correct |
24 |
Correct |
3 ms |
15448 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15292 KB |
Output is correct |
27 |
Correct |
3 ms |
15448 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
3 ms |
15244 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
3 ms |
15196 KB |
Output is correct |
34 |
Correct |
3 ms |
15196 KB |
Output is correct |
35 |
Correct |
3 ms |
15196 KB |
Output is correct |
36 |
Correct |
3 ms |
15196 KB |
Output is correct |
37 |
Correct |
3 ms |
15196 KB |
Output is correct |
38 |
Correct |
3 ms |
15196 KB |
Output is correct |
39 |
Correct |
90 ms |
22452 KB |
Output is correct |
40 |
Correct |
90 ms |
22456 KB |
Output is correct |
41 |
Correct |
100 ms |
22364 KB |
Output is correct |
42 |
Correct |
86 ms |
22576 KB |
Output is correct |
43 |
Correct |
49 ms |
22612 KB |
Output is correct |
44 |
Correct |
41 ms |
22676 KB |
Output is correct |
45 |
Correct |
94 ms |
25220 KB |
Output is correct |
46 |
Correct |
77 ms |
28180 KB |
Output is correct |
47 |
Correct |
109 ms |
27784 KB |
Output is correct |
48 |
Correct |
180 ms |
39064 KB |
Output is correct |
49 |
Correct |
172 ms |
38484 KB |
Output is correct |
50 |
Correct |
105 ms |
27836 KB |
Output is correct |
51 |
Correct |
108 ms |
27732 KB |
Output is correct |
52 |
Correct |
125 ms |
27836 KB |
Output is correct |
53 |
Correct |
144 ms |
26760 KB |
Output is correct |
54 |
Correct |
117 ms |
27668 KB |
Output is correct |
55 |
Correct |
10 ms |
15708 KB |
Output is correct |
56 |
Correct |
10 ms |
15708 KB |
Output is correct |
57 |
Correct |
61 ms |
22696 KB |
Output is correct |
58 |
Correct |
28 ms |
22224 KB |
Output is correct |
59 |
Correct |
84 ms |
28256 KB |
Output is correct |
60 |
Correct |
307 ms |
38484 KB |
Output is correct |
61 |
Correct |
122 ms |
27988 KB |
Output is correct |
62 |
Correct |
144 ms |
28240 KB |
Output is correct |
63 |
Correct |
169 ms |
28024 KB |
Output is correct |
64 |
Correct |
321 ms |
28092 KB |
Output is correct |
65 |
Correct |
135 ms |
29012 KB |
Output is correct |
66 |
Correct |
243 ms |
36260 KB |
Output is correct |
67 |
Correct |
72 ms |
28612 KB |
Output is correct |
68 |
Correct |
173 ms |
28756 KB |
Output is correct |
69 |
Correct |
161 ms |
28752 KB |
Output is correct |
70 |
Correct |
154 ms |
28384 KB |
Output is correct |