#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
int best_path(int n, int k, int H[][2], int l[]) {
vector<vector<pi>> e(n + 5);
for (int i = 0; i < n - 1; i++) {
e[H[i][0]].pb({H[i][1], i}); e[H[i][1]].pb({H[i][0], i});
}
vector<map<ll, int>> comp(n + 5);
vector<int> sz(n + 5, 1), len(n + 5, 0);
vector<ll> dist(n + 5, 0);
function<void(int, int)> dfs_size = [&](int f, int p) {
for (auto [to, id] : e[f]) {
if (to == p) continue;
len[to] = len[f] + 1;
dist[to] = dist[f] + l[id];
dfs_size(to, f);
sz[f] += sz[to];
}
};
int ans = IINF;
function<void(int, int)> dfs = [&](int f, int p) {
int lrg = -1;
for (auto [to, id] : e[f]) {
if (to == p) continue;
if (lrg == -1 || sz[to] > sz[lrg]) {
lrg = to;
}
}
for (auto [to, id] : e[f]) {
if (to == p || to == lrg) continue;
dfs(to, f);
}
if (lrg != -1) {
dfs(lrg, f);
swap(comp[f], comp[lrg]);
}
if (comp[f].count(dist[f])) {
mins(comp[f][dist[f]], len[f]);
} else {
comp[f][dist[f]] = len[f];
}
for (auto [to, id] : e[f]) {
if (to == p || to == lrg) continue;
for (auto &[x, y] : comp[to]) {
//dist[u] + dist[v] - 2 * dist[lca] = k
//dist[u] = k + 2 * dist[lca] - dist[v]
ll need = k + 2 * dist[f] - x;
if (comp[f].count(need)) {
mins(ans, y + comp[f][need] - 2 * len[f]);
}
}
for (auto &[x, y] : comp[to]) {
if (comp[f].count(x))
mins(comp[f][x], y);
else comp[f][x] = y;
}
}
if (comp[f].count(k + dist[f])) {
mins(ans, comp[f][k + dist[f]] - len[f]);
}
};
dfs_size(0, -1);
dfs(0, -1);
if (ans == IINF) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
576 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
572 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
572 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
576 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
568 KB |
Output is correct |
36 |
Correct |
1 ms |
572 KB |
Output is correct |
37 |
Correct |
1 ms |
444 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
159 ms |
25108 KB |
Output is correct |
20 |
Correct |
138 ms |
25824 KB |
Output is correct |
21 |
Correct |
123 ms |
25804 KB |
Output is correct |
22 |
Correct |
127 ms |
26036 KB |
Output is correct |
23 |
Correct |
183 ms |
39492 KB |
Output is correct |
24 |
Correct |
148 ms |
32236 KB |
Output is correct |
25 |
Correct |
101 ms |
24788 KB |
Output is correct |
26 |
Correct |
53 ms |
32928 KB |
Output is correct |
27 |
Correct |
228 ms |
41200 KB |
Output is correct |
28 |
Correct |
273 ms |
77108 KB |
Output is correct |
29 |
Correct |
272 ms |
75260 KB |
Output is correct |
30 |
Correct |
243 ms |
41152 KB |
Output is correct |
31 |
Correct |
201 ms |
41164 KB |
Output is correct |
32 |
Correct |
273 ms |
41324 KB |
Output is correct |
33 |
Correct |
240 ms |
40140 KB |
Output is correct |
34 |
Correct |
341 ms |
71960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
576 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
572 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
572 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
576 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
568 KB |
Output is correct |
36 |
Correct |
1 ms |
572 KB |
Output is correct |
37 |
Correct |
1 ms |
444 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
159 ms |
25108 KB |
Output is correct |
40 |
Correct |
138 ms |
25824 KB |
Output is correct |
41 |
Correct |
123 ms |
25804 KB |
Output is correct |
42 |
Correct |
127 ms |
26036 KB |
Output is correct |
43 |
Correct |
183 ms |
39492 KB |
Output is correct |
44 |
Correct |
148 ms |
32236 KB |
Output is correct |
45 |
Correct |
101 ms |
24788 KB |
Output is correct |
46 |
Correct |
53 ms |
32928 KB |
Output is correct |
47 |
Correct |
228 ms |
41200 KB |
Output is correct |
48 |
Correct |
273 ms |
77108 KB |
Output is correct |
49 |
Correct |
272 ms |
75260 KB |
Output is correct |
50 |
Correct |
243 ms |
41152 KB |
Output is correct |
51 |
Correct |
201 ms |
41164 KB |
Output is correct |
52 |
Correct |
273 ms |
41324 KB |
Output is correct |
53 |
Correct |
240 ms |
40140 KB |
Output is correct |
54 |
Correct |
341 ms |
71960 KB |
Output is correct |
55 |
Correct |
13 ms |
3944 KB |
Output is correct |
56 |
Correct |
8 ms |
2616 KB |
Output is correct |
57 |
Correct |
79 ms |
24720 KB |
Output is correct |
58 |
Correct |
55 ms |
21180 KB |
Output is correct |
59 |
Correct |
87 ms |
39204 KB |
Output is correct |
60 |
Correct |
258 ms |
77288 KB |
Output is correct |
61 |
Correct |
244 ms |
46084 KB |
Output is correct |
62 |
Correct |
211 ms |
42528 KB |
Output is correct |
63 |
Correct |
267 ms |
42604 KB |
Output is correct |
64 |
Correct |
491 ms |
93152 KB |
Output is correct |
65 |
Correct |
465 ms |
93900 KB |
Output is correct |
66 |
Correct |
305 ms |
72764 KB |
Output is correct |
67 |
Correct |
227 ms |
43172 KB |
Output is correct |
68 |
Correct |
364 ms |
61180 KB |
Output is correct |
69 |
Correct |
348 ms |
61896 KB |
Output is correct |
70 |
Correct |
341 ms |
58468 KB |
Output is correct |