#include <bits/stdc++.h>
using namespace std;
// #ifdef ONLINE_JUDGE
// #define debug(...)
// #else
// #include "debug.h"
// #endif
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define pii pair<long long, long long>
long long ANS = LLONG_MAX;
void dfs_sze(long long u, long long par, vector<vector<pii>> &adj, vector<long long> &sze, vector<long long> &len, vector<long long> &depth) {
sze[u] = 1;
for (auto v : adj[u]) {
if (v.f != par) {
len[v.f] = len[u] + v.s;
depth[v.f] = depth[u] + 1;
dfs_sze(v.f, u, adj, sze, len, depth);
sze[u] += sze[v.f];
}
}
}
void small_to_large_merging(long long K, long long u, long long par, vector<long long> &sze, vector<long long> &len, vector<long long> &depth, vector<vector<pii>> &adj, vector<multiset<pii>> &sets) {
multiset<pii> smalls1, smalls2;
for (auto x : adj[u]) {
if (x.f != par) {
small_to_large_merging(K, x.f, u, sze, len, depth, adj, sets);
for (auto y : sets[x.f]) {
long long len1 = y.f - len[u];
long long dep = y.s - depth[u];
pii find = {K - len1, -1ll};
auto it = smalls2.lower_bound(find);
if (it != smalls2.end()) {
if (it->f == find.f) {
ANS = min(ANS, dep + it->s);
}
}
}
for (auto y : sets[x.f]) {
smalls1.insert(y);
smalls2.insert({y.f - len[u], y.s - depth[u]});
}
}
}
sets[u] = smalls1;
smalls1.clear();
smalls2.clear();
sets[u].insert({len[u], depth[u]});
pii find = {K + len[u], -1ll};
auto it = sets[u].lower_bound(find);
if (it != sets[u].end()) {
if (it->f == find.f) {
ANS = min(ANS, it->s - depth[u]);
}
}
}
int best_path (int N, int k, int H[][2], int L[]) {
ANS = LLONG_MAX;
long long n = N, K = k;
vector<vector<pii>> adj(n);
for (long long i = 0; i < n - 1; i++) {
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
// debug(adj);
vector<long long> sze(n, 0);
vector<long long> len(n, 0);
vector<long long> depth(n, 0);
dfs_sze(0, -1, adj, sze, len, depth);
// debug(sze, len, depth);
vector<multiset<pii>> sets(n);
small_to_large_merging(K, 0, -1, sze, len, depth, adj, sets);
if (ANS == LLONG_MAX) {
return -1;
}
return ANS;
}
// void solve() {
// long long n, K;
// cin >> n >> K;
// long long H[n - 1][2];
// long long L[n - 1];
// for (long long i = 0; i < n - 1; i++) {
// cin >> H[i][0] >> H[i][1] >> L[i];
// }
// long long ans;
// cin >> ans;
// cout << best_path(n, K, H, L) << ' ' << ans << endl;
// }
// signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
// long long zz = 1;
// // cin >> zz;
// for (long long i = 1; i <= zz; i++) {
// // cout << "Case #" << i << ": ";
// solve();
// }
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2752 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2792 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2752 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2792 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
3780 KB |
Output is correct |
22 |
Correct |
5 ms |
3788 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
3544 KB |
Output is correct |
25 |
Correct |
3 ms |
3164 KB |
Output is correct |
26 |
Correct |
3 ms |
3420 KB |
Output is correct |
27 |
Correct |
3 ms |
3164 KB |
Output is correct |
28 |
Correct |
4 ms |
3528 KB |
Output is correct |
29 |
Correct |
3 ms |
3164 KB |
Output is correct |
30 |
Correct |
4 ms |
3364 KB |
Output is correct |
31 |
Correct |
3 ms |
3420 KB |
Output is correct |
32 |
Correct |
3 ms |
3420 KB |
Output is correct |
33 |
Correct |
5 ms |
3676 KB |
Output is correct |
34 |
Correct |
23 ms |
10204 KB |
Output is correct |
35 |
Correct |
37 ms |
12380 KB |
Output is correct |
36 |
Correct |
31 ms |
14068 KB |
Output is correct |
37 |
Correct |
17 ms |
8540 KB |
Output is correct |
38 |
Correct |
10 ms |
6720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2752 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2792 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1121 ms |
229400 KB |
Output is correct |
20 |
Correct |
1305 ms |
238416 KB |
Output is correct |
21 |
Correct |
900 ms |
181332 KB |
Output is correct |
22 |
Correct |
998 ms |
148092 KB |
Output is correct |
23 |
Correct |
1555 ms |
203016 KB |
Output is correct |
24 |
Correct |
722 ms |
109908 KB |
Output is correct |
25 |
Runtime error |
944 ms |
262144 KB |
Execution killed with signal 9 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2752 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2792 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
3780 KB |
Output is correct |
22 |
Correct |
5 ms |
3788 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
3544 KB |
Output is correct |
25 |
Correct |
3 ms |
3164 KB |
Output is correct |
26 |
Correct |
3 ms |
3420 KB |
Output is correct |
27 |
Correct |
3 ms |
3164 KB |
Output is correct |
28 |
Correct |
4 ms |
3528 KB |
Output is correct |
29 |
Correct |
3 ms |
3164 KB |
Output is correct |
30 |
Correct |
4 ms |
3364 KB |
Output is correct |
31 |
Correct |
3 ms |
3420 KB |
Output is correct |
32 |
Correct |
3 ms |
3420 KB |
Output is correct |
33 |
Correct |
5 ms |
3676 KB |
Output is correct |
34 |
Correct |
23 ms |
10204 KB |
Output is correct |
35 |
Correct |
37 ms |
12380 KB |
Output is correct |
36 |
Correct |
31 ms |
14068 KB |
Output is correct |
37 |
Correct |
17 ms |
8540 KB |
Output is correct |
38 |
Correct |
10 ms |
6720 KB |
Output is correct |
39 |
Correct |
1121 ms |
229400 KB |
Output is correct |
40 |
Correct |
1305 ms |
238416 KB |
Output is correct |
41 |
Correct |
900 ms |
181332 KB |
Output is correct |
42 |
Correct |
998 ms |
148092 KB |
Output is correct |
43 |
Correct |
1555 ms |
203016 KB |
Output is correct |
44 |
Correct |
722 ms |
109908 KB |
Output is correct |
45 |
Runtime error |
944 ms |
262144 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |