//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
#include "race.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
//#define int ll
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"
const int MAXN = 2e5 + 5;
const int oo = 1e18 + 5;
int n, k, h[MAXN][2], l[MAXN];
vector <pii> g[MAXN];
int sz[MAXN], ANS = oo;
bool del[MAXN];
set <pii> st;
void calcSZ(int v, int pr = -1) {
sz[v] = 1;
for (pii x : g[v]) {
int to = x.F;
if (to == pr || del[to]) { continue; }
calcSZ(to, v);
sz[v] += sz[to];
}
}
int findCentroid(int v, int pr = -1) {
for (pii x : g[v]) {
int to = x.F;
if (to == pr || del[to]) { continue; }
if (sz[to] > sz[v] / 2) {
sz[v] -= sz[to];
sz[to] += sz[v];
return findCentroid(to, v);
}
}
return v;
}
void update(int v, int pr, int lvl, int d) {
st.insert({d, lvl});
for (pii x : g[v]) {
int to = x.F, w = x.S;
if (to == pr || del[to]) { continue; }
update(to, v, lvl + 1, d + w);
}
}
void query(int v, int pr, int lvl, int d) {
auto it = st.lower_bound({k - d, 0ll});
if (it != st.end()) {
int d2 = it -> F, lvl2 = it -> S;
if (d + d2 == k) {
ANS = min(ANS, lvl + lvl2);
}
}
for (pii x : g[v]) {
int to = x.F, w = x.S;
if (to == pr || del[to]) { continue; }
query(to, v, lvl + 1, d + w);
}
}
void travel(int root) {
calcSZ(root);
int c = findCentroid(root);
st.insert({0, 0});
for (pii x : g[c]) {
int to = x.F, w = x.S;
if (del[to]) { continue; }
query(to, c, 1, w);
update(to, c, 1, w);
}
st.clear();
del[c] = 1;
for (pii x : g[c]) {
int to = x.F;
if (del[to]) { continue; }
travel(to);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n-1; ++i) {
h[i][0] = H[i][0];
h[i][1] = H[i][1];
}
for (int i = 0; i < n-1; ++i) {
l[i] = L[i];
int a = h[i][0], b = h[i][1], w = l[i];
g[a].pb({b, w});
g[b].pb({a, w});
}
travel(0);
if (ANS == oo) { return -1; }
return ANS;
}
Compilation message
race.cpp:44:21: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
44 | const int oo = 1e18 + 5;
| ~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
23 |
Correct |
5 ms |
5228 KB |
Output is correct |
24 |
Correct |
5 ms |
5228 KB |
Output is correct |
25 |
Correct |
5 ms |
5228 KB |
Output is correct |
26 |
Correct |
5 ms |
5228 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
5 ms |
5228 KB |
Output is correct |
29 |
Correct |
5 ms |
5228 KB |
Output is correct |
30 |
Correct |
7 ms |
5228 KB |
Output is correct |
31 |
Correct |
6 ms |
5228 KB |
Output is correct |
32 |
Correct |
5 ms |
5228 KB |
Output is correct |
33 |
Correct |
5 ms |
5228 KB |
Output is correct |
34 |
Correct |
5 ms |
5228 KB |
Output is correct |
35 |
Correct |
5 ms |
5228 KB |
Output is correct |
36 |
Correct |
5 ms |
5228 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
333 ms |
13036 KB |
Output is correct |
20 |
Correct |
343 ms |
13036 KB |
Output is correct |
21 |
Correct |
319 ms |
13036 KB |
Output is correct |
22 |
Correct |
268 ms |
13036 KB |
Output is correct |
23 |
Correct |
314 ms |
13548 KB |
Output is correct |
24 |
Correct |
139 ms |
13292 KB |
Output is correct |
25 |
Correct |
386 ms |
21236 KB |
Output is correct |
26 |
Correct |
239 ms |
24812 KB |
Output is correct |
27 |
Correct |
386 ms |
23788 KB |
Output is correct |
28 |
Correct |
1008 ms |
45172 KB |
Output is correct |
29 |
Correct |
1020 ms |
44088 KB |
Output is correct |
30 |
Correct |
366 ms |
23788 KB |
Output is correct |
31 |
Correct |
361 ms |
23788 KB |
Output is correct |
32 |
Correct |
457 ms |
23660 KB |
Output is correct |
33 |
Correct |
507 ms |
20316 KB |
Output is correct |
34 |
Correct |
950 ms |
30476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5228 KB |
Output is correct |
23 |
Correct |
5 ms |
5228 KB |
Output is correct |
24 |
Correct |
5 ms |
5228 KB |
Output is correct |
25 |
Correct |
5 ms |
5228 KB |
Output is correct |
26 |
Correct |
5 ms |
5228 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
5 ms |
5228 KB |
Output is correct |
29 |
Correct |
5 ms |
5228 KB |
Output is correct |
30 |
Correct |
7 ms |
5228 KB |
Output is correct |
31 |
Correct |
6 ms |
5228 KB |
Output is correct |
32 |
Correct |
5 ms |
5228 KB |
Output is correct |
33 |
Correct |
5 ms |
5228 KB |
Output is correct |
34 |
Correct |
5 ms |
5228 KB |
Output is correct |
35 |
Correct |
5 ms |
5228 KB |
Output is correct |
36 |
Correct |
5 ms |
5228 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5100 KB |
Output is correct |
39 |
Correct |
333 ms |
13036 KB |
Output is correct |
40 |
Correct |
343 ms |
13036 KB |
Output is correct |
41 |
Correct |
319 ms |
13036 KB |
Output is correct |
42 |
Correct |
268 ms |
13036 KB |
Output is correct |
43 |
Correct |
314 ms |
13548 KB |
Output is correct |
44 |
Correct |
139 ms |
13292 KB |
Output is correct |
45 |
Correct |
386 ms |
21236 KB |
Output is correct |
46 |
Correct |
239 ms |
24812 KB |
Output is correct |
47 |
Correct |
386 ms |
23788 KB |
Output is correct |
48 |
Correct |
1008 ms |
45172 KB |
Output is correct |
49 |
Correct |
1020 ms |
44088 KB |
Output is correct |
50 |
Correct |
366 ms |
23788 KB |
Output is correct |
51 |
Correct |
361 ms |
23788 KB |
Output is correct |
52 |
Correct |
457 ms |
23660 KB |
Output is correct |
53 |
Correct |
507 ms |
20316 KB |
Output is correct |
54 |
Correct |
950 ms |
30476 KB |
Output is correct |
55 |
Correct |
24 ms |
6300 KB |
Output is correct |
56 |
Correct |
19 ms |
5868 KB |
Output is correct |
57 |
Correct |
159 ms |
13164 KB |
Output is correct |
58 |
Correct |
53 ms |
12896 KB |
Output is correct |
59 |
Correct |
278 ms |
24940 KB |
Output is correct |
60 |
Correct |
1018 ms |
44276 KB |
Output is correct |
61 |
Correct |
405 ms |
26872 KB |
Output is correct |
62 |
Correct |
362 ms |
24044 KB |
Output is correct |
63 |
Correct |
443 ms |
23832 KB |
Output is correct |
64 |
Correct |
1292 ms |
30224 KB |
Output is correct |
65 |
Correct |
1026 ms |
31448 KB |
Output is correct |
66 |
Correct |
990 ms |
41452 KB |
Output is correct |
67 |
Correct |
161 ms |
21980 KB |
Output is correct |
68 |
Correct |
615 ms |
31212 KB |
Output is correct |
69 |
Correct |
621 ms |
31468 KB |
Output is correct |
70 |
Correct |
562 ms |
30060 KB |
Output is correct |