// ඞ
#pragma GCC optimize("Ofast")
//#pragma GCC target("popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "debug.h"
#define dbg(...) debug_out(splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define dbg(...)
#endif
//#define int long long
#define itn int
#define ALL(v) (v).begin(), (v).end()
#define mem(a, x) memset(a , x , sizeof(a))
#define f first
#define s second
#define mb make_pair
#define pb push_back
#define popCnt __builtin_popcountll
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef pair <int, int> pii;
typedef vector<pair <int, int>> vpii;
typedef vector<vector<char>> vvc;
const int mod = 1e9 + 7;
const int oo = 0x5f5f5f5f;
const long double PI = acos(-1.0L);
int ceiledLog2 (int x){return __lg(x) + (__builtin_popcount(x) != 1);}
ll toLL (int first, int second, int mx){return (1LL*first*mx) + second;}
template <class T, class U> T GCD (T a, U b) {return (!b ? a : GCD(b, a%b));}
template <class T, class U> T LCM (T a, U b) {return ((a/GCD(a, b)) * b);}
template <class T> bool isSquare (T n) {T sq = sqrt(n); return (sq*sq)==n;}
template <typename T> T Unique(T x){sort(ALL(x));x.resize(distance(x.begin(),unique(ALL(x))));return x;}
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//bool sortBySecond (pii &a, pii &b) {return a.second < b.second;} //asc
int ys[] = {-1, -1, 0, 1, 1, 1, 0, -1}; //edges [0, 2, 4, 6]
int xs[] = {0, 1, 1, 1, 0, -1, -1, -1};
//-----------------------------------------------------------------------------//
//const int N = ?e5 + 5;
int n,k;
vector<vpii> g;
int ans=2e9;
struct B_CH_SACK {
int n, timer = 0;
vector<int> lev,sz, b_ch;
vector<ll>to;
vector<int> order, tin, tout;
map<ll, int> f;
B_CH_SACK(int _n = 0) {
n = _n;
if (n) {
sz.resize(n, 1);
to.resize(n);
lev.resize(n);
b_ch.resize(n, -1);
tin.resize(n);
tout.resize(n);
dfs(0, -1);
solve(0, -1, false);
}
}
void dfs(int u, int p, ll t=0, int d=0) {
tin[u] = timer++;
order.push_back(u);
to[u]=t;
lev[u]=d;
int mx = 0, idx = -1;
for (auto &[v,c] : g[u]) {
if (v == p) continue;
dfs(v, u, t+c,d+1);
sz[u] += sz[v];
if (sz[v] > mx) {
mx = sz[v];
idx = v;
}
}
b_ch[u] = idx;
tout[u] = timer-1;
}
void solve(int u, int p, bool keep) {
for (auto &[v,c] : g[u]) {
if (v == p || v == b_ch[u]) continue;
solve(v, u, false);
}
if (b_ch[u] != -1) {
solve(b_ch[u], u, true);
}
f[to[u]]=lev[u];
for (auto &[v,c] : g[u]) {
if (v == p || v == b_ch[u]) continue;
for (int i = tin[v]; i <= tout[v]; i++) {
int x = order[i];
ll d=to[x]-to[u];
if (d>k) continue;
ll dif=k-d;
dif+=to[u];
if (f.count(dif)){
ans=min(ans, f[dif]-lev[u] + lev[x]-lev[u]);
}
}
for (int i = tin[v]; i <= tout[v]; i++) {
int x = order[i];
if (f.count(to[x]))
f[to[x]]=min(f[to[x]],lev[x]);
else
f[to[x]]=lev[x];
}
}
ll need=to[u]+k;
if (f.count(need)){
ans=min(ans, f[need]-lev[u]);
}
if (!keep) {
f.clear();
}
}
} sack;
int best_path(int N, int K, int h[][2], int l[]){
n=N;
k=K;
g=vector<vpii>(n);
for (int i = 0; i < n-1; ++i) {
g[h[i][0]].pb({h[i][1], l[i]});
g[h[i][1]].pb({h[i][0], l[i]});
}
sack=B_CH_SACK(n);
if (ans > 1e8)
return -1;
return ans;
}
Compilation message
race.cpp: In member function 'void B_CH_SACK::dfs(int, int, ll, int)':
race.cpp:91:28: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
91 | for (auto &[v,c] : g[u]) {
| ^
race.cpp: In member function 'void B_CH_SACK::solve(int, int, bool)':
race.cpp:106:28: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | for (auto &[v,c] : g[u]) {
| ^
race.cpp:116:28: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | for (auto &[v,c] : g[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
25 |
Correct |
1 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2648 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2648 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
101 ms |
15332 KB |
Output is correct |
20 |
Correct |
96 ms |
15316 KB |
Output is correct |
21 |
Correct |
107 ms |
15348 KB |
Output is correct |
22 |
Correct |
107 ms |
15568 KB |
Output is correct |
23 |
Correct |
140 ms |
15668 KB |
Output is correct |
24 |
Correct |
96 ms |
15568 KB |
Output is correct |
25 |
Correct |
64 ms |
23248 KB |
Output is correct |
26 |
Correct |
42 ms |
30276 KB |
Output is correct |
27 |
Correct |
137 ms |
25808 KB |
Output is correct |
28 |
Correct |
194 ms |
68020 KB |
Output is correct |
29 |
Correct |
183 ms |
66252 KB |
Output is correct |
30 |
Correct |
122 ms |
25808 KB |
Output is correct |
31 |
Correct |
123 ms |
25772 KB |
Output is correct |
32 |
Correct |
151 ms |
25804 KB |
Output is correct |
33 |
Correct |
157 ms |
24772 KB |
Output is correct |
34 |
Correct |
240 ms |
37380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2648 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
25 |
Correct |
1 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2648 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
36 |
Correct |
1 ms |
2648 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2648 KB |
Output is correct |
39 |
Correct |
101 ms |
15332 KB |
Output is correct |
40 |
Correct |
96 ms |
15316 KB |
Output is correct |
41 |
Correct |
107 ms |
15348 KB |
Output is correct |
42 |
Correct |
107 ms |
15568 KB |
Output is correct |
43 |
Correct |
140 ms |
15668 KB |
Output is correct |
44 |
Correct |
96 ms |
15568 KB |
Output is correct |
45 |
Correct |
64 ms |
23248 KB |
Output is correct |
46 |
Correct |
42 ms |
30276 KB |
Output is correct |
47 |
Correct |
137 ms |
25808 KB |
Output is correct |
48 |
Correct |
194 ms |
68020 KB |
Output is correct |
49 |
Correct |
183 ms |
66252 KB |
Output is correct |
50 |
Correct |
122 ms |
25808 KB |
Output is correct |
51 |
Correct |
123 ms |
25772 KB |
Output is correct |
52 |
Correct |
151 ms |
25804 KB |
Output is correct |
53 |
Correct |
157 ms |
24772 KB |
Output is correct |
54 |
Correct |
240 ms |
37380 KB |
Output is correct |
55 |
Correct |
10 ms |
3672 KB |
Output is correct |
56 |
Correct |
6 ms |
3416 KB |
Output is correct |
57 |
Correct |
65 ms |
14284 KB |
Output is correct |
58 |
Correct |
41 ms |
14540 KB |
Output is correct |
59 |
Correct |
59 ms |
35024 KB |
Output is correct |
60 |
Correct |
167 ms |
63940 KB |
Output is correct |
61 |
Correct |
168 ms |
25036 KB |
Output is correct |
62 |
Correct |
122 ms |
23496 KB |
Output is correct |
63 |
Correct |
151 ms |
23496 KB |
Output is correct |
64 |
Correct |
350 ms |
29120 KB |
Output is correct |
65 |
Correct |
327 ms |
35524 KB |
Output is correct |
66 |
Correct |
168 ms |
58000 KB |
Output is correct |
67 |
Correct |
102 ms |
24252 KB |
Output is correct |
68 |
Correct |
231 ms |
33728 KB |
Output is correct |
69 |
Correct |
243 ms |
34000 KB |
Output is correct |
70 |
Correct |
235 ms |
32504 KB |
Output is correct |