#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int N = 2e5 + 5;
const int K = 1e6 + 5;
const int inf = 1e9;
int n, k;
int dp[K], val[N], dep[N], cnt[N], sz[N];
bool del[N];
vector<pii> g[N];
vector<int> node, tree;
int ans = inf;
int dfs_size(int u, int p = 0) {
sz[u] = 1;
for(auto [v, _] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
return sz[u];
}
int find_centroid(int u, int p, int num) {
for(auto [v, _] : g[u]) if(v != p && !del[v] && sz[v] > num / 2) return find_centroid(v, u, num);
return u;
}
void dfs_centroid(int u, int p) {
if(val[u] > k) return;
dep[u] = dep[p] + 1;
node.push_back(u);
tree.push_back(u);
ans = min(ans, dep[u] + dp[k - val[u]]);
for(auto [v, w] : g[u]) if(v != p && !del[v]) {
val[v] = val[u] + w;
dfs_centroid(v, u);
}
}
void decomposition(int u) {
int centroid = find_centroid(u, 0, dfs_size(u));
del[centroid] = 1;
dep[centroid] = val[centroid] = dp[0] = 0;
tree.push_back(centroid);
for(auto [v, w] : g[centroid]) if(!del[v]) {
val[v] = w;
dfs_centroid(v, centroid);
for(int x : node) dp[val[x]] = min(dp[val[x]], dep[x]);
node.clear();
}
for(int x : tree) dp[val[x]] = inf;
tree.clear();
for(auto [v, _] : g[centroid]) if(!del[v]) {
decomposition(v);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
for(int i = 0; i < N - 1; ++i) {
int u = H[i][0], v = H[i][1], w = L[i];
++u; ++v;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
memset(dp, 0x3f, sizeof(dp));
decomposition(1);
return ans == inf ? -1 : ans;
}
// void solve() {
// cin >> n >> k;
// for(int i = 1, u, v, w; i < n; ++i) {
// cin >> u >> v >> w;
// ++u; ++v;
// g[u].emplace_back(v, w);
// g[v].emplace_back(u, w);
// }
// memset(dp, 0x3f, sizeof(dp));
// decomposition(1);
// cout << (ans == inf ? -1 : ans) << '\n';
// }
// signed main() {
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// int test = 1;
// // cin >> test;
// while(test--) solve();
// return 0;
// }
// https://oj.uz/problem/view/IOI11_race
Compilation message
race.cpp: In function 'int dfs_size(int, int)':
race.cpp:18:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for(auto [v, _] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
| ^
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [v, _] : g[u]) if(v != p && !del[v] && sz[v] > num / 2) return find_centroid(v, u, num);
| ^
race.cpp: In function 'void dfs_centroid(int, int)':
race.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [v, w] : g[u]) if(v != p && !del[v]) {
| ^
race.cpp: In function 'void decomposition(int)':
race.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto [v, w] : g[centroid]) if(!del[v]) {
| ^
race.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto [v, _] : g[centroid]) if(!del[v]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
3 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15808 KB |
Output is correct |
13 |
Correct |
3 ms |
15964 KB |
Output is correct |
14 |
Correct |
3 ms |
15840 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15964 KB |
Output is correct |
17 |
Correct |
3 ms |
15960 KB |
Output is correct |
18 |
Correct |
3 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
3 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15808 KB |
Output is correct |
13 |
Correct |
3 ms |
15964 KB |
Output is correct |
14 |
Correct |
3 ms |
15840 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15964 KB |
Output is correct |
17 |
Correct |
3 ms |
15960 KB |
Output is correct |
18 |
Correct |
3 ms |
15964 KB |
Output is correct |
19 |
Correct |
3 ms |
15964 KB |
Output is correct |
20 |
Correct |
4 ms |
15964 KB |
Output is correct |
21 |
Correct |
3 ms |
15964 KB |
Output is correct |
22 |
Correct |
4 ms |
15960 KB |
Output is correct |
23 |
Correct |
3 ms |
15960 KB |
Output is correct |
24 |
Correct |
3 ms |
15964 KB |
Output is correct |
25 |
Correct |
3 ms |
15964 KB |
Output is correct |
26 |
Correct |
3 ms |
15964 KB |
Output is correct |
27 |
Correct |
3 ms |
15964 KB |
Output is correct |
28 |
Correct |
3 ms |
15964 KB |
Output is correct |
29 |
Correct |
3 ms |
15960 KB |
Output is correct |
30 |
Correct |
3 ms |
15960 KB |
Output is correct |
31 |
Correct |
3 ms |
15960 KB |
Output is correct |
32 |
Correct |
3 ms |
15964 KB |
Output is correct |
33 |
Correct |
3 ms |
15964 KB |
Output is correct |
34 |
Correct |
3 ms |
15964 KB |
Output is correct |
35 |
Correct |
4 ms |
15960 KB |
Output is correct |
36 |
Correct |
3 ms |
15960 KB |
Output is correct |
37 |
Correct |
3 ms |
15964 KB |
Output is correct |
38 |
Correct |
3 ms |
16052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
3 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15808 KB |
Output is correct |
13 |
Correct |
3 ms |
15964 KB |
Output is correct |
14 |
Correct |
3 ms |
15840 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15964 KB |
Output is correct |
17 |
Correct |
3 ms |
15960 KB |
Output is correct |
18 |
Correct |
3 ms |
15964 KB |
Output is correct |
19 |
Correct |
68 ms |
21852 KB |
Output is correct |
20 |
Correct |
81 ms |
21960 KB |
Output is correct |
21 |
Correct |
71 ms |
22352 KB |
Output is correct |
22 |
Correct |
65 ms |
22536 KB |
Output is correct |
23 |
Correct |
49 ms |
22100 KB |
Output is correct |
24 |
Correct |
38 ms |
21844 KB |
Output is correct |
25 |
Correct |
75 ms |
24436 KB |
Output is correct |
26 |
Correct |
80 ms |
28428 KB |
Output is correct |
27 |
Correct |
103 ms |
25760 KB |
Output is correct |
28 |
Correct |
155 ms |
36944 KB |
Output is correct |
29 |
Correct |
145 ms |
35920 KB |
Output is correct |
30 |
Correct |
100 ms |
25852 KB |
Output is correct |
31 |
Correct |
104 ms |
25848 KB |
Output is correct |
32 |
Correct |
133 ms |
25720 KB |
Output is correct |
33 |
Correct |
132 ms |
24504 KB |
Output is correct |
34 |
Correct |
104 ms |
24400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Correct |
3 ms |
15964 KB |
Output is correct |
3 |
Correct |
3 ms |
15964 KB |
Output is correct |
4 |
Correct |
3 ms |
15964 KB |
Output is correct |
5 |
Correct |
3 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
3 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
3 ms |
15964 KB |
Output is correct |
11 |
Correct |
3 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15808 KB |
Output is correct |
13 |
Correct |
3 ms |
15964 KB |
Output is correct |
14 |
Correct |
3 ms |
15840 KB |
Output is correct |
15 |
Correct |
3 ms |
15964 KB |
Output is correct |
16 |
Correct |
3 ms |
15964 KB |
Output is correct |
17 |
Correct |
3 ms |
15960 KB |
Output is correct |
18 |
Correct |
3 ms |
15964 KB |
Output is correct |
19 |
Correct |
3 ms |
15964 KB |
Output is correct |
20 |
Correct |
4 ms |
15964 KB |
Output is correct |
21 |
Correct |
3 ms |
15964 KB |
Output is correct |
22 |
Correct |
4 ms |
15960 KB |
Output is correct |
23 |
Correct |
3 ms |
15960 KB |
Output is correct |
24 |
Correct |
3 ms |
15964 KB |
Output is correct |
25 |
Correct |
3 ms |
15964 KB |
Output is correct |
26 |
Correct |
3 ms |
15964 KB |
Output is correct |
27 |
Correct |
3 ms |
15964 KB |
Output is correct |
28 |
Correct |
3 ms |
15964 KB |
Output is correct |
29 |
Correct |
3 ms |
15960 KB |
Output is correct |
30 |
Correct |
3 ms |
15960 KB |
Output is correct |
31 |
Correct |
3 ms |
15960 KB |
Output is correct |
32 |
Correct |
3 ms |
15964 KB |
Output is correct |
33 |
Correct |
3 ms |
15964 KB |
Output is correct |
34 |
Correct |
3 ms |
15964 KB |
Output is correct |
35 |
Correct |
4 ms |
15960 KB |
Output is correct |
36 |
Correct |
3 ms |
15960 KB |
Output is correct |
37 |
Correct |
3 ms |
15964 KB |
Output is correct |
38 |
Correct |
3 ms |
16052 KB |
Output is correct |
39 |
Correct |
68 ms |
21852 KB |
Output is correct |
40 |
Correct |
81 ms |
21960 KB |
Output is correct |
41 |
Correct |
71 ms |
22352 KB |
Output is correct |
42 |
Correct |
65 ms |
22536 KB |
Output is correct |
43 |
Correct |
49 ms |
22100 KB |
Output is correct |
44 |
Correct |
38 ms |
21844 KB |
Output is correct |
45 |
Correct |
75 ms |
24436 KB |
Output is correct |
46 |
Correct |
80 ms |
28428 KB |
Output is correct |
47 |
Correct |
103 ms |
25760 KB |
Output is correct |
48 |
Correct |
155 ms |
36944 KB |
Output is correct |
49 |
Correct |
145 ms |
35920 KB |
Output is correct |
50 |
Correct |
100 ms |
25852 KB |
Output is correct |
51 |
Correct |
104 ms |
25848 KB |
Output is correct |
52 |
Correct |
133 ms |
25720 KB |
Output is correct |
53 |
Correct |
132 ms |
24504 KB |
Output is correct |
54 |
Correct |
104 ms |
24400 KB |
Output is correct |
55 |
Correct |
7 ms |
16476 KB |
Output is correct |
56 |
Correct |
8 ms |
16552 KB |
Output is correct |
57 |
Correct |
60 ms |
22888 KB |
Output is correct |
58 |
Correct |
33 ms |
23052 KB |
Output is correct |
59 |
Correct |
66 ms |
28424 KB |
Output is correct |
60 |
Correct |
229 ms |
37828 KB |
Output is correct |
61 |
Correct |
118 ms |
26044 KB |
Output is correct |
62 |
Correct |
110 ms |
26840 KB |
Output is correct |
63 |
Correct |
166 ms |
27136 KB |
Output is correct |
64 |
Correct |
207 ms |
27000 KB |
Output is correct |
65 |
Correct |
142 ms |
25460 KB |
Output is correct |
66 |
Correct |
214 ms |
34168 KB |
Output is correct |
67 |
Correct |
84 ms |
27840 KB |
Output is correct |
68 |
Correct |
142 ms |
26596 KB |
Output is correct |
69 |
Correct |
145 ms |
26584 KB |
Output is correct |
70 |
Correct |
172 ms |
26576 KB |
Output is correct |