#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 2e5+10;
const int mxK = 1e6+10;
const int INF = 1e9+10;
int ans = INF, k;
vector<array<int, 2>> adj[mxN];
bool vis[mxN];
int sz[mxN];
void get_sz(int node, int p) {
sz[node] = 1;
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_sz(it, node);
sz[node] += sz[it];
}
}
int get_c(int node, int p, int n) {
for(auto [it, w] : adj[node]) {
if(vis[it] || it == p) continue;
if(sz[it] * 2 > n) return get_c(it, node, n);
}
return node;
}
void get_min(int node, int p, bool filling, int dist, int s, unordered_map<int, int>& d) {
if(s > k) return ;
if(filling) {
if(!d.count(s)) d[s] = dist;
else d[s] = min(d[s], dist);
}
else {
if(d.count(k-s)) ans = min(ans, dist+d[k-s]);
}
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_min(it, node, filling, dist+1, s+w, d);
}
}
void build(int node) {
get_sz(node, -1);
int c = get_c(node, -1, sz[node]);
vis[c] = true;
unordered_map<int, int> m;
m[0] = 0;
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
get_min(it, c, 0, 1, w, m);
get_min(it, c, 1, 1, w, m);
}
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
build(it);
}
}
int best_path(int n, int K, int h[][2], int l[]) {
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]});
}
k = K;
build(0);
return (ans == INF ? -1 : ans);
}
/*
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << best_path(4, 3, {{0, 1}, {1, 2}, {1, 3}},
{1, 2, 4});
}*/
Compilation message
race.cpp: In function 'void get_sz(int, int)':
race.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'int get_c(int, int, int)':
race.cpp:25:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
25 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'void get_min(int, int, bool, int, int, std::unordered_map<int, int>&)':
race.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [it, w] : adj[node]) {
| ^
race.cpp: In function 'void build(int)':
race.cpp:53:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | for(auto [it, w] : adj[c]) {
| ^
race.cpp:58:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [it, w] : adj[c]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9552 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9552 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9820 KB |
Output is correct |
26 |
Correct |
2 ms |
9560 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
2 ms |
9752 KB |
Output is correct |
30 |
Correct |
2 ms |
9752 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9816 KB |
Output is correct |
34 |
Correct |
2 ms |
9820 KB |
Output is correct |
35 |
Correct |
2 ms |
9820 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9832 KB |
Output is correct |
38 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9552 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
93 ms |
15648 KB |
Output is correct |
20 |
Correct |
99 ms |
15652 KB |
Output is correct |
21 |
Correct |
100 ms |
15456 KB |
Output is correct |
22 |
Correct |
90 ms |
15680 KB |
Output is correct |
23 |
Correct |
51 ms |
15708 KB |
Output is correct |
24 |
Correct |
45 ms |
15704 KB |
Output is correct |
25 |
Correct |
88 ms |
19032 KB |
Output is correct |
26 |
Correct |
98 ms |
22868 KB |
Output is correct |
27 |
Correct |
119 ms |
19536 KB |
Output is correct |
28 |
Correct |
174 ms |
33616 KB |
Output is correct |
29 |
Correct |
159 ms |
32656 KB |
Output is correct |
30 |
Correct |
156 ms |
19540 KB |
Output is correct |
31 |
Correct |
154 ms |
19384 KB |
Output is correct |
32 |
Correct |
159 ms |
19796 KB |
Output is correct |
33 |
Correct |
146 ms |
18464 KB |
Output is correct |
34 |
Correct |
107 ms |
18256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
1 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
1 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
1 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
1 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9552 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
1 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
2 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9564 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
2 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
9820 KB |
Output is correct |
26 |
Correct |
2 ms |
9560 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9820 KB |
Output is correct |
29 |
Correct |
2 ms |
9752 KB |
Output is correct |
30 |
Correct |
2 ms |
9752 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9816 KB |
Output is correct |
34 |
Correct |
2 ms |
9820 KB |
Output is correct |
35 |
Correct |
2 ms |
9820 KB |
Output is correct |
36 |
Correct |
2 ms |
9820 KB |
Output is correct |
37 |
Correct |
2 ms |
9832 KB |
Output is correct |
38 |
Correct |
2 ms |
9820 KB |
Output is correct |
39 |
Correct |
93 ms |
15648 KB |
Output is correct |
40 |
Correct |
99 ms |
15652 KB |
Output is correct |
41 |
Correct |
100 ms |
15456 KB |
Output is correct |
42 |
Correct |
90 ms |
15680 KB |
Output is correct |
43 |
Correct |
51 ms |
15708 KB |
Output is correct |
44 |
Correct |
45 ms |
15704 KB |
Output is correct |
45 |
Correct |
88 ms |
19032 KB |
Output is correct |
46 |
Correct |
98 ms |
22868 KB |
Output is correct |
47 |
Correct |
119 ms |
19536 KB |
Output is correct |
48 |
Correct |
174 ms |
33616 KB |
Output is correct |
49 |
Correct |
159 ms |
32656 KB |
Output is correct |
50 |
Correct |
156 ms |
19540 KB |
Output is correct |
51 |
Correct |
154 ms |
19384 KB |
Output is correct |
52 |
Correct |
159 ms |
19796 KB |
Output is correct |
53 |
Correct |
146 ms |
18464 KB |
Output is correct |
54 |
Correct |
107 ms |
18256 KB |
Output is correct |
55 |
Correct |
10 ms |
10328 KB |
Output is correct |
56 |
Correct |
8 ms |
10016 KB |
Output is correct |
57 |
Correct |
80 ms |
15708 KB |
Output is correct |
58 |
Correct |
29 ms |
15824 KB |
Output is correct |
59 |
Correct |
146 ms |
28776 KB |
Output is correct |
60 |
Correct |
505 ms |
49788 KB |
Output is correct |
61 |
Correct |
163 ms |
21052 KB |
Output is correct |
62 |
Correct |
192 ms |
21752 KB |
Output is correct |
63 |
Correct |
187 ms |
21820 KB |
Output is correct |
64 |
Correct |
399 ms |
30144 KB |
Output is correct |
65 |
Correct |
117 ms |
22400 KB |
Output is correct |
66 |
Correct |
341 ms |
31452 KB |
Output is correct |
67 |
Correct |
70 ms |
22976 KB |
Output is correct |
68 |
Correct |
221 ms |
27748 KB |
Output is correct |
69 |
Correct |
248 ms |
25968 KB |
Output is correct |
70 |
Correct |
212 ms |
28004 KB |
Output is correct |