//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 5e5+100;
int K, sz[MAXN];
vector<pii> g[MAXN];
bool vis[MAXN];
void centroid(int v, int p, int csz, int& ans){
sz[v] = 1; bool is = true;
for(auto [u, w] : g[v]){
if(u == p || vis[u]) continue;
centroid(u, v, csz, ans);
sz[v] += sz[u];
if(sz[u] * 2 > csz){
is = false;
}
}
if((csz - sz[v]) * 2 <= csz && is){
ans = v;
}
}
const int MAXK = 1e6+100;
int arr[MAXK];
void dfs(int v, int p, int d, int len, vector<pii>& vch){
if(d > K) return;
vch.pb({d, len});
for(auto [u, w] : g[v]){
if(u == p || vis[u])
continue;
dfs(u, v, d + w, len + 1, vch);
}
}
int solve(int v, int p, int csz){
centroid(v, p, csz, v);
int ans = INT_MAX;
vector<int> vrt;
for(int i = 0; i < SZ(g[v]); i++){
int u = g[v][i].ff, w = g[v][i].ss;
if(u == p || vis[u]) continue;
vector<pii> vch;
dfs(u, v, w, 1, vch);
arr[0] = 0;
for(auto [d, len] : vch){
if(arr[K - d] != INT_MAX){
ans = min(ans, arr[K - d] + len);
}
vrt.pb(d);
}
for(auto [d, len] : vch){
arr[d] = min(arr[d], len);
}
}
for(auto d : vrt){
arr[d] = INT_MAX;
}
vis[v] = true;
for(auto [u, w] : g[v]){
if(u == p || vis[u])
continue;
ans = min(ans, solve(u, v, sz[u]));
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
:: K = K;
for(int i = 0; i < N - 1; i++){
int u = H[i][0], v = H[i][1];
g[u].pb({v, L[i]});
g[v].pb({u, L[i]});
}
fill(arr, arr + MAXK, INT_MAX);
int ans = solve(0, -1, N);
if(ans == INT_MAX){
ans = -1;
}
return ans;
}
Compilation message
race.cpp: In function 'void centroid(int, int, int, int&)':
race.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [u, w] : g[v]){
| ^
race.cpp: In function 'void dfs(int, int, int, int, std::vector<std::pair<int, int> >&)':
race.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [u, w] : g[v]){
| ^
race.cpp: In function 'int solve(int, int, int)':
race.cpp:58:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [d, len] : vch){
| ^
race.cpp:64:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for(auto [d, len] : vch){
| ^
race.cpp:72:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for(auto [u, w] : g[v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
10 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
10 ms |
15980 KB |
Output is correct |
9 |
Correct |
10 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
10 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
11 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
10 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
10 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
10 ms |
15980 KB |
Output is correct |
9 |
Correct |
10 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
10 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
11 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
10 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
10 ms |
15980 KB |
Output is correct |
21 |
Correct |
11 ms |
16108 KB |
Output is correct |
22 |
Correct |
12 ms |
16108 KB |
Output is correct |
23 |
Correct |
12 ms |
16108 KB |
Output is correct |
24 |
Correct |
12 ms |
16108 KB |
Output is correct |
25 |
Correct |
12 ms |
16108 KB |
Output is correct |
26 |
Correct |
11 ms |
16108 KB |
Output is correct |
27 |
Correct |
11 ms |
16108 KB |
Output is correct |
28 |
Correct |
12 ms |
16108 KB |
Output is correct |
29 |
Correct |
13 ms |
16108 KB |
Output is correct |
30 |
Correct |
12 ms |
16108 KB |
Output is correct |
31 |
Correct |
11 ms |
16108 KB |
Output is correct |
32 |
Correct |
12 ms |
16108 KB |
Output is correct |
33 |
Correct |
14 ms |
16108 KB |
Output is correct |
34 |
Correct |
12 ms |
16108 KB |
Output is correct |
35 |
Correct |
12 ms |
16108 KB |
Output is correct |
36 |
Correct |
13 ms |
16108 KB |
Output is correct |
37 |
Correct |
11 ms |
16108 KB |
Output is correct |
38 |
Correct |
12 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
10 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
10 ms |
15980 KB |
Output is correct |
9 |
Correct |
10 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
10 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
11 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
10 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
184 ms |
21868 KB |
Output is correct |
20 |
Correct |
178 ms |
22252 KB |
Output is correct |
21 |
Correct |
202 ms |
22716 KB |
Output is correct |
22 |
Correct |
186 ms |
23148 KB |
Output is correct |
23 |
Correct |
116 ms |
22252 KB |
Output is correct |
24 |
Correct |
84 ms |
22252 KB |
Output is correct |
25 |
Correct |
179 ms |
26832 KB |
Output is correct |
26 |
Correct |
137 ms |
32484 KB |
Output is correct |
27 |
Correct |
234 ms |
27500 KB |
Output is correct |
28 |
Correct |
330 ms |
44452 KB |
Output is correct |
29 |
Correct |
346 ms |
42988 KB |
Output is correct |
30 |
Correct |
230 ms |
26988 KB |
Output is correct |
31 |
Correct |
241 ms |
26988 KB |
Output is correct |
32 |
Correct |
274 ms |
26988 KB |
Output is correct |
33 |
Correct |
309 ms |
26092 KB |
Output is correct |
34 |
Correct |
233 ms |
25836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
10 ms |
15980 KB |
Output is correct |
7 |
Correct |
10 ms |
15980 KB |
Output is correct |
8 |
Correct |
10 ms |
15980 KB |
Output is correct |
9 |
Correct |
10 ms |
15980 KB |
Output is correct |
10 |
Correct |
11 ms |
15980 KB |
Output is correct |
11 |
Correct |
10 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
10 ms |
15980 KB |
Output is correct |
14 |
Correct |
10 ms |
15980 KB |
Output is correct |
15 |
Correct |
11 ms |
15980 KB |
Output is correct |
16 |
Correct |
10 ms |
15980 KB |
Output is correct |
17 |
Correct |
10 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
10 ms |
15980 KB |
Output is correct |
21 |
Correct |
11 ms |
16108 KB |
Output is correct |
22 |
Correct |
12 ms |
16108 KB |
Output is correct |
23 |
Correct |
12 ms |
16108 KB |
Output is correct |
24 |
Correct |
12 ms |
16108 KB |
Output is correct |
25 |
Correct |
12 ms |
16108 KB |
Output is correct |
26 |
Correct |
11 ms |
16108 KB |
Output is correct |
27 |
Correct |
11 ms |
16108 KB |
Output is correct |
28 |
Correct |
12 ms |
16108 KB |
Output is correct |
29 |
Correct |
13 ms |
16108 KB |
Output is correct |
30 |
Correct |
12 ms |
16108 KB |
Output is correct |
31 |
Correct |
11 ms |
16108 KB |
Output is correct |
32 |
Correct |
12 ms |
16108 KB |
Output is correct |
33 |
Correct |
14 ms |
16108 KB |
Output is correct |
34 |
Correct |
12 ms |
16108 KB |
Output is correct |
35 |
Correct |
12 ms |
16108 KB |
Output is correct |
36 |
Correct |
13 ms |
16108 KB |
Output is correct |
37 |
Correct |
11 ms |
16108 KB |
Output is correct |
38 |
Correct |
12 ms |
16108 KB |
Output is correct |
39 |
Correct |
184 ms |
21868 KB |
Output is correct |
40 |
Correct |
178 ms |
22252 KB |
Output is correct |
41 |
Correct |
202 ms |
22716 KB |
Output is correct |
42 |
Correct |
186 ms |
23148 KB |
Output is correct |
43 |
Correct |
116 ms |
22252 KB |
Output is correct |
44 |
Correct |
84 ms |
22252 KB |
Output is correct |
45 |
Correct |
179 ms |
26832 KB |
Output is correct |
46 |
Correct |
137 ms |
32484 KB |
Output is correct |
47 |
Correct |
234 ms |
27500 KB |
Output is correct |
48 |
Correct |
330 ms |
44452 KB |
Output is correct |
49 |
Correct |
346 ms |
42988 KB |
Output is correct |
50 |
Correct |
230 ms |
26988 KB |
Output is correct |
51 |
Correct |
241 ms |
26988 KB |
Output is correct |
52 |
Correct |
274 ms |
26988 KB |
Output is correct |
53 |
Correct |
309 ms |
26092 KB |
Output is correct |
54 |
Correct |
233 ms |
25836 KB |
Output is correct |
55 |
Correct |
21 ms |
16620 KB |
Output is correct |
56 |
Correct |
21 ms |
16620 KB |
Output is correct |
57 |
Correct |
123 ms |
23000 KB |
Output is correct |
58 |
Correct |
54 ms |
22496 KB |
Output is correct |
59 |
Correct |
140 ms |
31972 KB |
Output is correct |
60 |
Correct |
530 ms |
46796 KB |
Output is correct |
61 |
Correct |
252 ms |
27208 KB |
Output is correct |
62 |
Correct |
257 ms |
27996 KB |
Output is correct |
63 |
Correct |
341 ms |
27868 KB |
Output is correct |
64 |
Correct |
641 ms |
29440 KB |
Output is correct |
65 |
Correct |
316 ms |
26932 KB |
Output is correct |
66 |
Correct |
455 ms |
39916 KB |
Output is correct |
67 |
Correct |
169 ms |
28892 KB |
Output is correct |
68 |
Correct |
329 ms |
28252 KB |
Output is correct |
69 |
Correct |
326 ms |
28252 KB |
Output is correct |
70 |
Correct |
311 ms |
27736 KB |
Output is correct |