#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 2e5 + 5;
vector<pair<ll, ll>> g[MX];
int par[MX];
set<pair<ll, ll>> st[MX]; // (total length, num of edge)
ll sum[MX], dep[MX];
int find(int v) {
return par[v] == v ? v : par[v] = find(par[v]);
}
ll N, K;
ll res = 1e9;
void merge(int u, int v) {
int tu = u, tv = v;
u = find(u), v = find(v);
if(st[u].size() > st[v].size()) {
swap(u, v);
}
par[u] = v;
for(auto [len, e] : st[u]) {
// calc answer
ll k = K - len + 2 * sum[tv];
auto cur = st[v].lower_bound({k, 0});
if(cur != st[v].end() && cur->first == k) {
res = min(res, e + cur->second - 2 * dep[tv]);
}
}
for(auto [len, e] : st[u]) {
st[v].insert({len, e});
}
st[u].clear();
}
void dfs(int v, int p) {
for(auto [u, w] : g[v]) {
if(u == p) continue;
dep[u] = dep[v] + 1;
sum[u] = sum[v] + w;
dfs(u, v);
merge(u, v);
}
find(v);
// straight line
auto it = st[par[v]].lower_bound({K + sum[v], 0});
if(it != st[par[v]].end() && it->first == K + sum[v]) {
res = min(res, it->second - dep[v]);
}
// add path
st[par[v]].insert({sum[v], dep[v]});
}
int best_path(int n, int k, int h[][2], int w[]) {
N = n;
K = k;
for(int i = 0; i < N - 1; i++) {
g[h[i][0]].push_back({h[i][1], w[i]});
g[h[i][1]].push_back({h[i][0], w[i]});
}
for(int i = 0; i < N; i++) par[i] = i;
dfs(0, -1);
if(res == 1e9) res = -1;
return res;
}
// int h[MX][2], w[MX];
// int main() {
// ios_base::sync_with_stdio(0); cin.tie(0);
// // h[0][0] = 0; h[0][1] = 1;
// // h[1][0] = 1; h[1][1] = 2;
// // h[2][0] = 1; h[2][1] = 3;
// // w[0] = 1;
// // w[1] = 2;
// // w[2] = 4;
// // h[0][0] = 0, h[0][1] = 1;
// // h[1][0] = 1, h[1][1] = 2;
// // w[0] = 1;
// // w[1] = 1;
// cin >> N >> K;
// for(int i = 0; i < N - 1; i++) {
// cin >> h[i][0] >> h[i][1] >> w[i];
// }
// // for(int i = 0; i < N - 1; i++) cin >> w[i];
// cout << best_path(N, K, h, w) << '\n';
// }
Compilation message
race.cpp: In function 'void merge(int, int)':
race.cpp:22:11: warning: unused variable 'tu' [-Wunused-variable]
22 | int tu = u, tv = v;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14372 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14444 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14360 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14424 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
10 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14452 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14372 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14444 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14360 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14424 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
10 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14452 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
6 ms |
14364 KB |
Output is correct |
21 |
Correct |
8 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14548 KB |
Output is correct |
23 |
Correct |
8 ms |
14548 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14548 KB |
Output is correct |
26 |
Correct |
10 ms |
14556 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
7 ms |
14568 KB |
Output is correct |
30 |
Correct |
8 ms |
14536 KB |
Output is correct |
31 |
Correct |
7 ms |
14456 KB |
Output is correct |
32 |
Correct |
9 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14568 KB |
Output is correct |
34 |
Correct |
9 ms |
14488 KB |
Output is correct |
35 |
Correct |
7 ms |
14496 KB |
Output is correct |
36 |
Correct |
7 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14372 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14444 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14360 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14424 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
10 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14452 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
88 ms |
23108 KB |
Output is correct |
20 |
Correct |
106 ms |
23224 KB |
Output is correct |
21 |
Correct |
100 ms |
23004 KB |
Output is correct |
22 |
Correct |
95 ms |
22812 KB |
Output is correct |
23 |
Correct |
112 ms |
23760 KB |
Output is correct |
24 |
Correct |
97 ms |
22396 KB |
Output is correct |
25 |
Correct |
114 ms |
38552 KB |
Output is correct |
26 |
Correct |
61 ms |
40704 KB |
Output is correct |
27 |
Correct |
143 ms |
37588 KB |
Output is correct |
28 |
Correct |
172 ms |
67424 KB |
Output is correct |
29 |
Correct |
180 ms |
66848 KB |
Output is correct |
30 |
Correct |
143 ms |
37660 KB |
Output is correct |
31 |
Correct |
143 ms |
37516 KB |
Output is correct |
32 |
Correct |
172 ms |
37572 KB |
Output is correct |
33 |
Correct |
143 ms |
33628 KB |
Output is correct |
34 |
Correct |
248 ms |
47220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14336 KB |
Output is correct |
4 |
Correct |
7 ms |
14372 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14444 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14360 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14424 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
10 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14452 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
6 ms |
14364 KB |
Output is correct |
21 |
Correct |
8 ms |
14548 KB |
Output is correct |
22 |
Correct |
8 ms |
14548 KB |
Output is correct |
23 |
Correct |
8 ms |
14548 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14548 KB |
Output is correct |
26 |
Correct |
10 ms |
14556 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
7 ms |
14568 KB |
Output is correct |
30 |
Correct |
8 ms |
14536 KB |
Output is correct |
31 |
Correct |
7 ms |
14456 KB |
Output is correct |
32 |
Correct |
9 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14568 KB |
Output is correct |
34 |
Correct |
9 ms |
14488 KB |
Output is correct |
35 |
Correct |
7 ms |
14496 KB |
Output is correct |
36 |
Correct |
7 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
39 |
Correct |
88 ms |
23108 KB |
Output is correct |
40 |
Correct |
106 ms |
23224 KB |
Output is correct |
41 |
Correct |
100 ms |
23004 KB |
Output is correct |
42 |
Correct |
95 ms |
22812 KB |
Output is correct |
43 |
Correct |
112 ms |
23760 KB |
Output is correct |
44 |
Correct |
97 ms |
22396 KB |
Output is correct |
45 |
Correct |
114 ms |
38552 KB |
Output is correct |
46 |
Correct |
61 ms |
40704 KB |
Output is correct |
47 |
Correct |
143 ms |
37588 KB |
Output is correct |
48 |
Correct |
172 ms |
67424 KB |
Output is correct |
49 |
Correct |
180 ms |
66848 KB |
Output is correct |
50 |
Correct |
143 ms |
37660 KB |
Output is correct |
51 |
Correct |
143 ms |
37516 KB |
Output is correct |
52 |
Correct |
172 ms |
37572 KB |
Output is correct |
53 |
Correct |
143 ms |
33628 KB |
Output is correct |
54 |
Correct |
248 ms |
47220 KB |
Output is correct |
55 |
Correct |
16 ms |
15952 KB |
Output is correct |
56 |
Correct |
12 ms |
15316 KB |
Output is correct |
57 |
Correct |
49 ms |
24392 KB |
Output is correct |
58 |
Correct |
40 ms |
23228 KB |
Output is correct |
59 |
Correct |
58 ms |
40728 KB |
Output is correct |
60 |
Correct |
165 ms |
66892 KB |
Output is correct |
61 |
Correct |
156 ms |
41672 KB |
Output is correct |
62 |
Correct |
138 ms |
37708 KB |
Output is correct |
63 |
Correct |
184 ms |
37732 KB |
Output is correct |
64 |
Correct |
304 ms |
46268 KB |
Output is correct |
65 |
Correct |
291 ms |
47528 KB |
Output is correct |
66 |
Correct |
186 ms |
65356 KB |
Output is correct |
67 |
Correct |
121 ms |
33484 KB |
Output is correct |
68 |
Correct |
237 ms |
46668 KB |
Output is correct |
69 |
Correct |
252 ms |
51580 KB |
Output is correct |
70 |
Correct |
211 ms |
45140 KB |
Output is correct |