#include<bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
const int MAXN = 500010;
vector<pair<int,int>> v[MAXN];
map<ll,multiset<int>> w2e;
int get(ll h){
return *w2e[h].begin();
}
void erase(ll h, int x){
w2e[h].erase(w2e[h].find(x));
if(w2e[h].size() == 0) w2e.erase(h);
}
void add(ll h, int x){
w2e[h].insert(x);
}
int k, ans;
namespace sack{
int T;
int t_in[MAXN], t_out[MAXN], t2node[MAXN];
int sz[MAXN];
int depth[MAXN];
ll wdepth[MAXN];
void precalc(int x, int par=-1, int d=0, ll dw=0){
t2node[T] = x; t_in[x] = T++; sz[x] = 1;
depth[x] = d, wdepth[x] = dw;
for(auto [y, w] : v[x]) if(y != par) {
precalc(y, x, d + 1, dw + w);
sz[x] += sz[y];
}
t_out[x] = T;
}
void solve(int x, int par=-1, bool keep=0){
int hc = -1; // heavy_child
for(auto [y, w] : v[x]) if(y != par) {
if(hc == -1 || sz[y] > sz[hc]) hc = y;
}
if(hc != -1){
// solve but dont keep light nodes
for(auto [y, w] : v[x]) if(y != par && y != hc)
solve(y, x, false);
// solve and keep heavy nodes
solve(hc, x, true);
}
for(auto [y, w] : v[x]) if(y != par && y != hc) {
// maybe solve queries (light -> (heavy + past_lights))
for(int t = t_in[y]; t < t_out[y]; ++t){
int z = t2node[t];
// query z
ll goal = wdepth[x] * 2 + k;
ll missing = goal - wdepth[z];
int my_edges = depth[z] - depth[x];
// cout << "oi " << x << " " << z << "\n";
// cout << goal << " " << missing << "\n";
if(w2e.count(missing)){
int friend_edges = get(missing) - depth[x];
// cout << "in " << friend_edges << " " << my_edges << endl;
ans = min(ans, my_edges + friend_edges);
}
}
// add light nodes after solving, avoid info leakage
for(int t = t_in[y]; t < t_out[y]; ++t){
int z = t2node[t];
// add z
add(wdepth[z], depth[z]);
}
}
// solve queries (x -> all subtrees)
if(w2e.count(wdepth[x] + k)){
ans = min(ans, get(wdepth[x] + k) - depth[x]);
}
// add x for querying or for keeping as heavy child
add(wdepth[x], depth[x]);
if(!keep){
for(int t = t_in[x]; t < t_out[x]; ++t){
int z = t2node[t];
// rmv z
erase(wdepth[z], depth[z]);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
for(int i = 0; i < N; ++i) v[i].clear();
for(int i = 0; i + 1 < N; ++i){
int a = H[i][0], b = H[i][1];
int c = L[i];
v[a].push_back({b, c});
v[b].push_back({a, c});
}
ans = INT_MAX;
k = K;
sack::precalc(0);
sack::solve(0);
if(ans == INT_MAX) return -1;
else return ans;
}
// int32_t main(){
// int n, k; cin >> n >> k;
// int h[n-1][2];
// int l[n-1];
// for(int i = 0; i < n-1; ++i){
// cin >> h[i][0] >> h[i][1] >> l[i];
// }
// cout << best_path(n, k, h, l);
// int x; cin >> x;
// cout << " " << x << "\n";
// }
Compilation message
race.cpp: In function 'void sack::precalc(int, int, int, long long int)':
race.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [y, w] : v[x]) if(y != par) {
| ^
race.cpp: In function 'void sack::solve(int, int, bool)':
race.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [y, w] : v[x]) if(y != par) {
| ^
race.cpp:52:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto [y, w] : v[x]) if(y != par && y != hc)
| ^
race.cpp:58:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [y, w] : v[x]) if(y != par && y != hc) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
8 ms |
12048 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
7 ms |
12108 KB |
Output is correct |
15 |
Correct |
8 ms |
12116 KB |
Output is correct |
16 |
Correct |
8 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
8 ms |
12048 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
7 ms |
12108 KB |
Output is correct |
15 |
Correct |
8 ms |
12116 KB |
Output is correct |
16 |
Correct |
8 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
7 ms |
12116 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
12116 KB |
Output is correct |
21 |
Correct |
12 ms |
12228 KB |
Output is correct |
22 |
Correct |
9 ms |
12244 KB |
Output is correct |
23 |
Correct |
9 ms |
12264 KB |
Output is correct |
24 |
Correct |
9 ms |
12244 KB |
Output is correct |
25 |
Correct |
9 ms |
12288 KB |
Output is correct |
26 |
Correct |
8 ms |
12248 KB |
Output is correct |
27 |
Correct |
8 ms |
12244 KB |
Output is correct |
28 |
Correct |
9 ms |
12244 KB |
Output is correct |
29 |
Correct |
9 ms |
12244 KB |
Output is correct |
30 |
Correct |
9 ms |
12444 KB |
Output is correct |
31 |
Correct |
9 ms |
12244 KB |
Output is correct |
32 |
Correct |
9 ms |
12320 KB |
Output is correct |
33 |
Correct |
8 ms |
12244 KB |
Output is correct |
34 |
Correct |
8 ms |
12236 KB |
Output is correct |
35 |
Correct |
8 ms |
12244 KB |
Output is correct |
36 |
Correct |
8 ms |
12312 KB |
Output is correct |
37 |
Correct |
8 ms |
12244 KB |
Output is correct |
38 |
Correct |
8 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
8 ms |
12048 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
7 ms |
12108 KB |
Output is correct |
15 |
Correct |
8 ms |
12116 KB |
Output is correct |
16 |
Correct |
8 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
7 ms |
12116 KB |
Output is correct |
19 |
Correct |
270 ms |
24384 KB |
Output is correct |
20 |
Correct |
317 ms |
25732 KB |
Output is correct |
21 |
Correct |
299 ms |
25744 KB |
Output is correct |
22 |
Correct |
259 ms |
25760 KB |
Output is correct |
23 |
Correct |
358 ms |
26240 KB |
Output is correct |
24 |
Correct |
249 ms |
25884 KB |
Output is correct |
25 |
Correct |
113 ms |
33920 KB |
Output is correct |
26 |
Correct |
76 ms |
40772 KB |
Output is correct |
27 |
Correct |
285 ms |
39968 KB |
Output is correct |
28 |
Correct |
286 ms |
88552 KB |
Output is correct |
29 |
Correct |
293 ms |
86220 KB |
Output is correct |
30 |
Correct |
301 ms |
40040 KB |
Output is correct |
31 |
Correct |
284 ms |
39964 KB |
Output is correct |
32 |
Correct |
391 ms |
40316 KB |
Output is correct |
33 |
Correct |
359 ms |
39064 KB |
Output is correct |
34 |
Correct |
679 ms |
58272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
7 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12024 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
8 ms |
12048 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
12116 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12116 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
7 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12116 KB |
Output is correct |
14 |
Correct |
7 ms |
12108 KB |
Output is correct |
15 |
Correct |
8 ms |
12116 KB |
Output is correct |
16 |
Correct |
8 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
7 ms |
12116 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
12116 KB |
Output is correct |
21 |
Correct |
12 ms |
12228 KB |
Output is correct |
22 |
Correct |
9 ms |
12244 KB |
Output is correct |
23 |
Correct |
9 ms |
12264 KB |
Output is correct |
24 |
Correct |
9 ms |
12244 KB |
Output is correct |
25 |
Correct |
9 ms |
12288 KB |
Output is correct |
26 |
Correct |
8 ms |
12248 KB |
Output is correct |
27 |
Correct |
8 ms |
12244 KB |
Output is correct |
28 |
Correct |
9 ms |
12244 KB |
Output is correct |
29 |
Correct |
9 ms |
12244 KB |
Output is correct |
30 |
Correct |
9 ms |
12444 KB |
Output is correct |
31 |
Correct |
9 ms |
12244 KB |
Output is correct |
32 |
Correct |
9 ms |
12320 KB |
Output is correct |
33 |
Correct |
8 ms |
12244 KB |
Output is correct |
34 |
Correct |
8 ms |
12236 KB |
Output is correct |
35 |
Correct |
8 ms |
12244 KB |
Output is correct |
36 |
Correct |
8 ms |
12312 KB |
Output is correct |
37 |
Correct |
8 ms |
12244 KB |
Output is correct |
38 |
Correct |
8 ms |
12244 KB |
Output is correct |
39 |
Correct |
270 ms |
24384 KB |
Output is correct |
40 |
Correct |
317 ms |
25732 KB |
Output is correct |
41 |
Correct |
299 ms |
25744 KB |
Output is correct |
42 |
Correct |
259 ms |
25760 KB |
Output is correct |
43 |
Correct |
358 ms |
26240 KB |
Output is correct |
44 |
Correct |
249 ms |
25884 KB |
Output is correct |
45 |
Correct |
113 ms |
33920 KB |
Output is correct |
46 |
Correct |
76 ms |
40772 KB |
Output is correct |
47 |
Correct |
285 ms |
39968 KB |
Output is correct |
48 |
Correct |
286 ms |
88552 KB |
Output is correct |
49 |
Correct |
293 ms |
86220 KB |
Output is correct |
50 |
Correct |
301 ms |
40040 KB |
Output is correct |
51 |
Correct |
284 ms |
39964 KB |
Output is correct |
52 |
Correct |
391 ms |
40316 KB |
Output is correct |
53 |
Correct |
359 ms |
39064 KB |
Output is correct |
54 |
Correct |
679 ms |
58272 KB |
Output is correct |
55 |
Correct |
27 ms |
13900 KB |
Output is correct |
56 |
Correct |
22 ms |
13376 KB |
Output is correct |
57 |
Correct |
241 ms |
25976 KB |
Output is correct |
58 |
Correct |
102 ms |
25676 KB |
Output is correct |
59 |
Correct |
104 ms |
50132 KB |
Output is correct |
60 |
Correct |
300 ms |
86868 KB |
Output is correct |
61 |
Correct |
338 ms |
42700 KB |
Output is correct |
62 |
Correct |
313 ms |
40212 KB |
Output is correct |
63 |
Correct |
407 ms |
40320 KB |
Output is correct |
64 |
Correct |
925 ms |
49480 KB |
Output is correct |
65 |
Correct |
1006 ms |
59152 KB |
Output is correct |
66 |
Correct |
405 ms |
81212 KB |
Output is correct |
67 |
Correct |
313 ms |
40572 KB |
Output is correct |
68 |
Correct |
546 ms |
56236 KB |
Output is correct |
69 |
Correct |
515 ms |
56772 KB |
Output is correct |
70 |
Correct |
497 ms |
54380 KB |
Output is correct |