/*
++[---------->+<]>.-[------>+<]>-.++++++++.+++++.-[->+++++<]>-.-[--->++<]>--.+.[--->+<]>---.[->+++<]>++.++++++.-------.++++++.--[--->+<]>.-[->+++<]>-..+++++++++++++.[----->++<]>.------------.+[----->+<]>.--------.+++++++++++++.-------------.--[--->+<]>-.---[->++++<]>-.++++[->+++<]>.--[--->+<]>-.[->+++<]>++.-.+++.---.-[->+++<]>.-[--->++<]>+.++++.------.[--->+<]>---.+[----->+<]>.------------.+++++++.-------.--[--->+<]>---.+++[->+++<]>++..+++++++++.---------.-[->+++<]>.+[----->+<]>+.-------------.+++++++.+.----[->+++<]>-.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long big;
typedef long double ludo;
#define pbb pair<big, big>
#define pii pair<int, int>
#define fe first
#define se second
#define maxheap priority_queue
#define mset multiset
#define uset unordered_set
#define umap unordered_map
#define fr(i, s, e) for(int i = s; i < e; i++)
#define revfr(i, s, e) for(int i = s - 1; i >= e; i--)
#define getv(v, n) for(int i = 0; i < n; i++) cin >> v[i];
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL)
#define debug(text) if(do_debug) {cout << (#text) << ": " << text << endl;}
#define nl "\n"
// making some things easily accessible
// push_back pop_back emplace_back make_pair lower_bound upper_bound reserve resize reverse
const big mod = 1000000007;
// const big mod = 998244353;
const big infinity = 1000000000000000000;
const int inf = 1e9 + 5;
bool do_debug = false;
const int maxn = 2e5 + 5;
int n_nodes, n_edges, rl;
vector<array<int, 2>> adj[maxn];
// int depth[maxn];
// vector<int> rev[maxn];
// vector<int> path;
big ans = infinity;
struct ds {
map<big, big> m;
big extra_w = 0;
big extra_l = 0;
};
ds state[maxn];
void merge(ds& a, ds& b) {
if(a.m.size() < b.m.size()) swap(a, b);
for(auto p : b.m) {
big cur_w = p.fe + b.extra_w;
big cur_l = p.se + b.extra_l;
if(cur_w > rl) break;
big rq = rl - cur_w;
big look_for = rq - a.extra_w;
if(a.m.find(look_for) != a.m.end()) {
ans = min(ans, cur_l + a.m[look_for] + a.extra_l);
}
// if(cur_w == rl) {
// ans = min(ans, cur_l + a.extra_l);
// } else {
// big rq = rl - cur_w;
// big look_for = rq - a.extra_w;
// if(a.m.find(look_for) != a.m.end()) {
// ans = min(ans, a.m[look_for] + a.extra_l);
// }
// }
}
for(auto p : b.m) {
big cur_w = p.fe + b.extra_w - a.extra_w;
big cur_l = p.se + b.extra_l - a.extra_l;
if(a.m.find(cur_w) != a.m.end()) {
a.m[cur_w] = min(a.m[cur_w], cur_l);
} else {
a.m[cur_w] = cur_l;
}
}
}
void dfs(int node, int parent) {
state[node].m[0] = 0;
for(auto p : adj[node]) {
int next = p[0], w = p[1];
if(next == parent) continue;
dfs(next, node);
state[next].extra_w += w;
state[next].extra_l += 1;
merge(state[node], state[next]);
}
}
int best_path(int n, int k, int h[][2], int l[]) {
n_nodes = n;
rl = k;
fr(i, 0, n - 1) {
int u = h[i][0], v = h[i][1], w = l[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(0, 0);
if(ans < infinity) return ans;
else return -1;
}
// int main() {
// int n, k;
// cin >> n >> k;
// int h[n - 1][2];
// int l[n - 1];
// fr(i, 0, n - 1) {
// cin >> h[i][0] >> h[i][1];
// }
// fr(i, 0, n - 1) cin >> l[i];
// cout << best_path(n, k, h, l);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17484 KB |
Output is correct |
2 |
Correct |
10 ms |
17484 KB |
Output is correct |
3 |
Correct |
9 ms |
17484 KB |
Output is correct |
4 |
Correct |
9 ms |
17528 KB |
Output is correct |
5 |
Correct |
10 ms |
17464 KB |
Output is correct |
6 |
Correct |
10 ms |
17484 KB |
Output is correct |
7 |
Correct |
10 ms |
17488 KB |
Output is correct |
8 |
Correct |
10 ms |
17484 KB |
Output is correct |
9 |
Correct |
10 ms |
17524 KB |
Output is correct |
10 |
Correct |
9 ms |
17484 KB |
Output is correct |
11 |
Correct |
10 ms |
17484 KB |
Output is correct |
12 |
Correct |
9 ms |
17484 KB |
Output is correct |
13 |
Correct |
9 ms |
17484 KB |
Output is correct |
14 |
Correct |
9 ms |
17548 KB |
Output is correct |
15 |
Correct |
10 ms |
17528 KB |
Output is correct |
16 |
Correct |
10 ms |
17488 KB |
Output is correct |
17 |
Correct |
10 ms |
17484 KB |
Output is correct |
18 |
Correct |
10 ms |
17524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17484 KB |
Output is correct |
2 |
Correct |
10 ms |
17484 KB |
Output is correct |
3 |
Correct |
9 ms |
17484 KB |
Output is correct |
4 |
Correct |
9 ms |
17528 KB |
Output is correct |
5 |
Correct |
10 ms |
17464 KB |
Output is correct |
6 |
Correct |
10 ms |
17484 KB |
Output is correct |
7 |
Correct |
10 ms |
17488 KB |
Output is correct |
8 |
Correct |
10 ms |
17484 KB |
Output is correct |
9 |
Correct |
10 ms |
17524 KB |
Output is correct |
10 |
Correct |
9 ms |
17484 KB |
Output is correct |
11 |
Correct |
10 ms |
17484 KB |
Output is correct |
12 |
Correct |
9 ms |
17484 KB |
Output is correct |
13 |
Correct |
9 ms |
17484 KB |
Output is correct |
14 |
Correct |
9 ms |
17548 KB |
Output is correct |
15 |
Correct |
10 ms |
17528 KB |
Output is correct |
16 |
Correct |
10 ms |
17488 KB |
Output is correct |
17 |
Correct |
10 ms |
17484 KB |
Output is correct |
18 |
Correct |
10 ms |
17524 KB |
Output is correct |
19 |
Correct |
9 ms |
17524 KB |
Output is correct |
20 |
Correct |
10 ms |
17536 KB |
Output is correct |
21 |
Correct |
10 ms |
17744 KB |
Output is correct |
22 |
Correct |
11 ms |
17736 KB |
Output is correct |
23 |
Correct |
11 ms |
17740 KB |
Output is correct |
24 |
Correct |
12 ms |
17740 KB |
Output is correct |
25 |
Correct |
14 ms |
17740 KB |
Output is correct |
26 |
Correct |
13 ms |
17764 KB |
Output is correct |
27 |
Correct |
10 ms |
17652 KB |
Output is correct |
28 |
Correct |
11 ms |
17784 KB |
Output is correct |
29 |
Correct |
13 ms |
17740 KB |
Output is correct |
30 |
Correct |
11 ms |
17792 KB |
Output is correct |
31 |
Correct |
11 ms |
17812 KB |
Output is correct |
32 |
Correct |
10 ms |
17740 KB |
Output is correct |
33 |
Correct |
11 ms |
17792 KB |
Output is correct |
34 |
Correct |
11 ms |
17680 KB |
Output is correct |
35 |
Correct |
11 ms |
17664 KB |
Output is correct |
36 |
Correct |
10 ms |
17612 KB |
Output is correct |
37 |
Correct |
11 ms |
17740 KB |
Output is correct |
38 |
Correct |
12 ms |
17664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17484 KB |
Output is correct |
2 |
Correct |
10 ms |
17484 KB |
Output is correct |
3 |
Correct |
9 ms |
17484 KB |
Output is correct |
4 |
Correct |
9 ms |
17528 KB |
Output is correct |
5 |
Correct |
10 ms |
17464 KB |
Output is correct |
6 |
Correct |
10 ms |
17484 KB |
Output is correct |
7 |
Correct |
10 ms |
17488 KB |
Output is correct |
8 |
Correct |
10 ms |
17484 KB |
Output is correct |
9 |
Correct |
10 ms |
17524 KB |
Output is correct |
10 |
Correct |
9 ms |
17484 KB |
Output is correct |
11 |
Correct |
10 ms |
17484 KB |
Output is correct |
12 |
Correct |
9 ms |
17484 KB |
Output is correct |
13 |
Correct |
9 ms |
17484 KB |
Output is correct |
14 |
Correct |
9 ms |
17548 KB |
Output is correct |
15 |
Correct |
10 ms |
17528 KB |
Output is correct |
16 |
Correct |
10 ms |
17488 KB |
Output is correct |
17 |
Correct |
10 ms |
17484 KB |
Output is correct |
18 |
Correct |
10 ms |
17524 KB |
Output is correct |
19 |
Correct |
151 ms |
39264 KB |
Output is correct |
20 |
Correct |
147 ms |
39296 KB |
Output is correct |
21 |
Correct |
141 ms |
39036 KB |
Output is correct |
22 |
Correct |
155 ms |
38632 KB |
Output is correct |
23 |
Correct |
184 ms |
51352 KB |
Output is correct |
24 |
Correct |
150 ms |
42072 KB |
Output is correct |
25 |
Correct |
131 ms |
34204 KB |
Output is correct |
26 |
Correct |
69 ms |
37260 KB |
Output is correct |
27 |
Correct |
237 ms |
49348 KB |
Output is correct |
28 |
Correct |
387 ms |
69896 KB |
Output is correct |
29 |
Correct |
337 ms |
69700 KB |
Output is correct |
30 |
Correct |
235 ms |
49428 KB |
Output is correct |
31 |
Correct |
244 ms |
49344 KB |
Output is correct |
32 |
Correct |
319 ms |
49500 KB |
Output is correct |
33 |
Correct |
284 ms |
54080 KB |
Output is correct |
34 |
Correct |
395 ms |
87012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17484 KB |
Output is correct |
2 |
Correct |
10 ms |
17484 KB |
Output is correct |
3 |
Correct |
9 ms |
17484 KB |
Output is correct |
4 |
Correct |
9 ms |
17528 KB |
Output is correct |
5 |
Correct |
10 ms |
17464 KB |
Output is correct |
6 |
Correct |
10 ms |
17484 KB |
Output is correct |
7 |
Correct |
10 ms |
17488 KB |
Output is correct |
8 |
Correct |
10 ms |
17484 KB |
Output is correct |
9 |
Correct |
10 ms |
17524 KB |
Output is correct |
10 |
Correct |
9 ms |
17484 KB |
Output is correct |
11 |
Correct |
10 ms |
17484 KB |
Output is correct |
12 |
Correct |
9 ms |
17484 KB |
Output is correct |
13 |
Correct |
9 ms |
17484 KB |
Output is correct |
14 |
Correct |
9 ms |
17548 KB |
Output is correct |
15 |
Correct |
10 ms |
17528 KB |
Output is correct |
16 |
Correct |
10 ms |
17488 KB |
Output is correct |
17 |
Correct |
10 ms |
17484 KB |
Output is correct |
18 |
Correct |
10 ms |
17524 KB |
Output is correct |
19 |
Correct |
9 ms |
17524 KB |
Output is correct |
20 |
Correct |
10 ms |
17536 KB |
Output is correct |
21 |
Correct |
10 ms |
17744 KB |
Output is correct |
22 |
Correct |
11 ms |
17736 KB |
Output is correct |
23 |
Correct |
11 ms |
17740 KB |
Output is correct |
24 |
Correct |
12 ms |
17740 KB |
Output is correct |
25 |
Correct |
14 ms |
17740 KB |
Output is correct |
26 |
Correct |
13 ms |
17764 KB |
Output is correct |
27 |
Correct |
10 ms |
17652 KB |
Output is correct |
28 |
Correct |
11 ms |
17784 KB |
Output is correct |
29 |
Correct |
13 ms |
17740 KB |
Output is correct |
30 |
Correct |
11 ms |
17792 KB |
Output is correct |
31 |
Correct |
11 ms |
17812 KB |
Output is correct |
32 |
Correct |
10 ms |
17740 KB |
Output is correct |
33 |
Correct |
11 ms |
17792 KB |
Output is correct |
34 |
Correct |
11 ms |
17680 KB |
Output is correct |
35 |
Correct |
11 ms |
17664 KB |
Output is correct |
36 |
Correct |
10 ms |
17612 KB |
Output is correct |
37 |
Correct |
11 ms |
17740 KB |
Output is correct |
38 |
Correct |
12 ms |
17664 KB |
Output is correct |
39 |
Correct |
151 ms |
39264 KB |
Output is correct |
40 |
Correct |
147 ms |
39296 KB |
Output is correct |
41 |
Correct |
141 ms |
39036 KB |
Output is correct |
42 |
Correct |
155 ms |
38632 KB |
Output is correct |
43 |
Correct |
184 ms |
51352 KB |
Output is correct |
44 |
Correct |
150 ms |
42072 KB |
Output is correct |
45 |
Correct |
131 ms |
34204 KB |
Output is correct |
46 |
Correct |
69 ms |
37260 KB |
Output is correct |
47 |
Correct |
237 ms |
49348 KB |
Output is correct |
48 |
Correct |
387 ms |
69896 KB |
Output is correct |
49 |
Correct |
337 ms |
69700 KB |
Output is correct |
50 |
Correct |
235 ms |
49428 KB |
Output is correct |
51 |
Correct |
244 ms |
49344 KB |
Output is correct |
52 |
Correct |
319 ms |
49500 KB |
Output is correct |
53 |
Correct |
284 ms |
54080 KB |
Output is correct |
54 |
Correct |
395 ms |
87012 KB |
Output is correct |
55 |
Correct |
26 ms |
20576 KB |
Output is correct |
56 |
Correct |
18 ms |
19272 KB |
Output is correct |
57 |
Correct |
95 ms |
36680 KB |
Output is correct |
58 |
Correct |
80 ms |
29912 KB |
Output is correct |
59 |
Correct |
111 ms |
43460 KB |
Output is correct |
60 |
Correct |
350 ms |
69468 KB |
Output is correct |
61 |
Correct |
266 ms |
52900 KB |
Output is correct |
62 |
Correct |
243 ms |
49384 KB |
Output is correct |
63 |
Correct |
320 ms |
49476 KB |
Output is correct |
64 |
Correct |
528 ms |
104684 KB |
Output is correct |
65 |
Correct |
502 ms |
102768 KB |
Output is correct |
66 |
Correct |
375 ms |
69284 KB |
Output is correct |
67 |
Correct |
210 ms |
44000 KB |
Output is correct |
68 |
Correct |
435 ms |
66548 KB |
Output is correct |
69 |
Correct |
457 ms |
71340 KB |
Output is correct |
70 |
Correct |
410 ms |
64460 KB |
Output is correct |