#include "race.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define sync \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __gnu_pbds;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 5;
vv(pp(ll, ll)) g[N];
ll depth[N], depth1[N], ans[N], k;
multiset<pp(ll, ll)> sub[N];
void init(ll v, ll par)
{
ans[v] = INF;
for (auto [to, w] : g[v])
if (to != par)
depth[to] = depth[v] + 1, depth1[to] = depth1[v] + w, init(to, v);
}
void dfs(ll v, ll par)
{
sub[v].ins(mpr(depth1[v], depth[v]));
multiset<pp(ll, ll)>::iterator it;
for (auto [to, w] : g[v])
{
if (to == par)
continue;
dfs(to, v);
if (sub[to].size() > sub[v].size())
swap(sub[to], sub[v]);
ans[v] = min(ans[v], ans[to]);
for (auto [dis, dep] : sub[to])
{
it = sub[v].lower_bound(mpr(k - (dis - depth1[v] * 2), -100));
if (it != sub[v].end() and it->F + dis - depth1[v] * 2 == k)
ans[v] = min(ans[v], it->S + dep - depth[v] * 2);
}
for (auto i : sub[to])
sub[v].ins(i);
}
}
int best_path(int n, int k1, int e[][2], int w[])
{
k = k1;
for (ll i = 0; i < n - 1; i++)
g[e[i][0]].pb(mpr(e[i][1], w[i])), g[e[i][1]].pb(mpr(e[i][0], w[i]));
init(0, 0);
dfs(0, 0);
return ans[0] == INF ? -1 : ans[0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14460 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
10 ms |
14376 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
10 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14460 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
10 ms |
14376 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
10 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14396 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14728 KB |
Output is correct |
22 |
Correct |
10 ms |
14692 KB |
Output is correct |
23 |
Correct |
10 ms |
14684 KB |
Output is correct |
24 |
Correct |
9 ms |
14716 KB |
Output is correct |
25 |
Correct |
10 ms |
14676 KB |
Output is correct |
26 |
Correct |
13 ms |
14680 KB |
Output is correct |
27 |
Correct |
11 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14684 KB |
Output is correct |
30 |
Correct |
10 ms |
14672 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14764 KB |
Output is correct |
33 |
Correct |
12 ms |
14676 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
10 ms |
14600 KB |
Output is correct |
36 |
Correct |
9 ms |
14676 KB |
Output is correct |
37 |
Correct |
11 ms |
14660 KB |
Output is correct |
38 |
Correct |
11 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14460 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
10 ms |
14376 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
10 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14396 KB |
Output is correct |
19 |
Correct |
224 ms |
64212 KB |
Output is correct |
20 |
Correct |
227 ms |
63344 KB |
Output is correct |
21 |
Correct |
217 ms |
63816 KB |
Output is correct |
22 |
Correct |
221 ms |
58568 KB |
Output is correct |
23 |
Correct |
249 ms |
75792 KB |
Output is correct |
24 |
Correct |
194 ms |
59680 KB |
Output is correct |
25 |
Correct |
148 ms |
45004 KB |
Output is correct |
26 |
Correct |
81 ms |
49080 KB |
Output is correct |
27 |
Correct |
290 ms |
72304 KB |
Output is correct |
28 |
Correct |
322 ms |
83932 KB |
Output is correct |
29 |
Correct |
290 ms |
83096 KB |
Output is correct |
30 |
Correct |
293 ms |
72344 KB |
Output is correct |
31 |
Correct |
280 ms |
72460 KB |
Output is correct |
32 |
Correct |
433 ms |
72396 KB |
Output is correct |
33 |
Correct |
404 ms |
92948 KB |
Output is correct |
34 |
Correct |
369 ms |
91676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14460 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
10 ms |
14376 KB |
Output is correct |
6 |
Correct |
9 ms |
14416 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
10 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
10 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14416 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14396 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14728 KB |
Output is correct |
22 |
Correct |
10 ms |
14692 KB |
Output is correct |
23 |
Correct |
10 ms |
14684 KB |
Output is correct |
24 |
Correct |
9 ms |
14716 KB |
Output is correct |
25 |
Correct |
10 ms |
14676 KB |
Output is correct |
26 |
Correct |
13 ms |
14680 KB |
Output is correct |
27 |
Correct |
11 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14684 KB |
Output is correct |
30 |
Correct |
10 ms |
14672 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14764 KB |
Output is correct |
33 |
Correct |
12 ms |
14676 KB |
Output is correct |
34 |
Correct |
10 ms |
14676 KB |
Output is correct |
35 |
Correct |
10 ms |
14600 KB |
Output is correct |
36 |
Correct |
9 ms |
14676 KB |
Output is correct |
37 |
Correct |
11 ms |
14660 KB |
Output is correct |
38 |
Correct |
11 ms |
14672 KB |
Output is correct |
39 |
Correct |
224 ms |
64212 KB |
Output is correct |
40 |
Correct |
227 ms |
63344 KB |
Output is correct |
41 |
Correct |
217 ms |
63816 KB |
Output is correct |
42 |
Correct |
221 ms |
58568 KB |
Output is correct |
43 |
Correct |
249 ms |
75792 KB |
Output is correct |
44 |
Correct |
194 ms |
59680 KB |
Output is correct |
45 |
Correct |
148 ms |
45004 KB |
Output is correct |
46 |
Correct |
81 ms |
49080 KB |
Output is correct |
47 |
Correct |
290 ms |
72304 KB |
Output is correct |
48 |
Correct |
322 ms |
83932 KB |
Output is correct |
49 |
Correct |
290 ms |
83096 KB |
Output is correct |
50 |
Correct |
293 ms |
72344 KB |
Output is correct |
51 |
Correct |
280 ms |
72460 KB |
Output is correct |
52 |
Correct |
433 ms |
72396 KB |
Output is correct |
53 |
Correct |
404 ms |
92948 KB |
Output is correct |
54 |
Correct |
369 ms |
91676 KB |
Output is correct |
55 |
Correct |
21 ms |
18252 KB |
Output is correct |
56 |
Correct |
23 ms |
18696 KB |
Output is correct |
57 |
Correct |
207 ms |
78832 KB |
Output is correct |
58 |
Correct |
92 ms |
36120 KB |
Output is correct |
59 |
Correct |
109 ms |
48972 KB |
Output is correct |
60 |
Correct |
272 ms |
83148 KB |
Output is correct |
61 |
Correct |
296 ms |
72592 KB |
Output is correct |
62 |
Correct |
354 ms |
72304 KB |
Output is correct |
63 |
Correct |
395 ms |
72456 KB |
Output is correct |
64 |
Correct |
481 ms |
117684 KB |
Output is correct |
65 |
Correct |
506 ms |
107352 KB |
Output is correct |
66 |
Correct |
308 ms |
81144 KB |
Output is correct |
67 |
Correct |
303 ms |
59396 KB |
Output is correct |
68 |
Correct |
369 ms |
72280 KB |
Output is correct |
69 |
Correct |
463 ms |
77612 KB |
Output is correct |
70 |
Correct |
361 ms |
69676 KB |
Output is correct |