#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define pb push_back
#define fi first
#define se second
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
const int MAXN = 2e5 + 5;
vector<pii> adj[MAXN];
map<ll, ll> poss[MAXN];
ll off[MAXN], cost_off[MAXN];
ll res = 1e18;
int k;
void dfs(ll v, ll p){
poss[v][0] = 0;
for(auto [u, d]: adj[v]){
if(u == p) continue;
dfs(u, v);
off[u] += d;
cost_off[u]++;
if(poss[u].size() > poss[v].size()) {
swap(off[u], off[v]);
swap(cost_off[u], cost_off[v]);
swap(poss[u], poss[v]);
}
set<ll> new_entries;
map<ll, ll> old_vals;
for(auto [dis, cost]: poss[u]){
ll real_dis = dis + off[u];
if(real_dis > k) continue;
ll miss = k - real_dis - off[v];
if(poss[v].count(miss) && !new_entries.count(miss)){
ll old_cost = poss[v][miss];
if(old_vals.count(miss)) {
old_cost = old_vals[miss];
}
res = min(old_cost + cost_off[u] + cost_off[v] + cost, res);
}
real_dis -= off[v];
if(poss[v].count(real_dis)) {
old_vals[real_dis] = poss[v][real_dis];
poss[v][real_dis] = min(poss[v][real_dis], cost + cost_off[u] - cost_off[v]);
} else {
poss[v][real_dis] = cost + cost_off[u] - cost_off[v];
new_entries.insert(real_dis);
}
}
poss[u] = {};
}
}
int best_path(int N, int K, int H[][2], int L[]) {
rep(i, 0, N) {
adj[i] = {};
poss[i] = {};
off[i] = 0;
}
k = K;
rep(i, 0, N - 1) {
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
dfs(0, -1);
if(res < 1e18) return res;
else return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21340 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
3 ms |
21340 KB |
Output is correct |
6 |
Correct |
3 ms |
21340 KB |
Output is correct |
7 |
Correct |
3 ms |
21340 KB |
Output is correct |
8 |
Correct |
3 ms |
21340 KB |
Output is correct |
9 |
Correct |
3 ms |
21340 KB |
Output is correct |
10 |
Correct |
4 ms |
21340 KB |
Output is correct |
11 |
Correct |
3 ms |
21172 KB |
Output is correct |
12 |
Correct |
3 ms |
21340 KB |
Output is correct |
13 |
Correct |
3 ms |
21340 KB |
Output is correct |
14 |
Correct |
3 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
21340 KB |
Output is correct |
16 |
Correct |
3 ms |
21340 KB |
Output is correct |
17 |
Correct |
3 ms |
21352 KB |
Output is correct |
18 |
Correct |
3 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21340 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
3 ms |
21340 KB |
Output is correct |
6 |
Correct |
3 ms |
21340 KB |
Output is correct |
7 |
Correct |
3 ms |
21340 KB |
Output is correct |
8 |
Correct |
3 ms |
21340 KB |
Output is correct |
9 |
Correct |
3 ms |
21340 KB |
Output is correct |
10 |
Correct |
4 ms |
21340 KB |
Output is correct |
11 |
Correct |
3 ms |
21172 KB |
Output is correct |
12 |
Correct |
3 ms |
21340 KB |
Output is correct |
13 |
Correct |
3 ms |
21340 KB |
Output is correct |
14 |
Correct |
3 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
21340 KB |
Output is correct |
16 |
Correct |
3 ms |
21340 KB |
Output is correct |
17 |
Correct |
3 ms |
21352 KB |
Output is correct |
18 |
Correct |
3 ms |
21340 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
3 ms |
21300 KB |
Output is correct |
21 |
Correct |
4 ms |
21340 KB |
Output is correct |
22 |
Correct |
3 ms |
21340 KB |
Output is correct |
23 |
Correct |
3 ms |
21340 KB |
Output is correct |
24 |
Correct |
4 ms |
21340 KB |
Output is correct |
25 |
Correct |
4 ms |
21340 KB |
Output is correct |
26 |
Correct |
4 ms |
21340 KB |
Output is correct |
27 |
Correct |
3 ms |
21340 KB |
Output is correct |
28 |
Correct |
4 ms |
21340 KB |
Output is correct |
29 |
Correct |
4 ms |
21340 KB |
Output is correct |
30 |
Correct |
4 ms |
21340 KB |
Output is correct |
31 |
Correct |
4 ms |
21340 KB |
Output is correct |
32 |
Correct |
4 ms |
21340 KB |
Output is correct |
33 |
Correct |
4 ms |
21340 KB |
Output is correct |
34 |
Correct |
3 ms |
21340 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
3 ms |
21340 KB |
Output is correct |
37 |
Correct |
3 ms |
21340 KB |
Output is correct |
38 |
Correct |
3 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21340 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
3 ms |
21340 KB |
Output is correct |
6 |
Correct |
3 ms |
21340 KB |
Output is correct |
7 |
Correct |
3 ms |
21340 KB |
Output is correct |
8 |
Correct |
3 ms |
21340 KB |
Output is correct |
9 |
Correct |
3 ms |
21340 KB |
Output is correct |
10 |
Correct |
4 ms |
21340 KB |
Output is correct |
11 |
Correct |
3 ms |
21172 KB |
Output is correct |
12 |
Correct |
3 ms |
21340 KB |
Output is correct |
13 |
Correct |
3 ms |
21340 KB |
Output is correct |
14 |
Correct |
3 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
21340 KB |
Output is correct |
16 |
Correct |
3 ms |
21340 KB |
Output is correct |
17 |
Correct |
3 ms |
21352 KB |
Output is correct |
18 |
Correct |
3 ms |
21340 KB |
Output is correct |
19 |
Correct |
76 ms |
28500 KB |
Output is correct |
20 |
Correct |
72 ms |
28496 KB |
Output is correct |
21 |
Correct |
75 ms |
28688 KB |
Output is correct |
22 |
Correct |
70 ms |
28504 KB |
Output is correct |
23 |
Correct |
55 ms |
29020 KB |
Output is correct |
24 |
Correct |
50 ms |
28244 KB |
Output is correct |
25 |
Correct |
64 ms |
48980 KB |
Output is correct |
26 |
Correct |
58 ms |
67156 KB |
Output is correct |
27 |
Correct |
113 ms |
34456 KB |
Output is correct |
28 |
Correct |
222 ms |
110672 KB |
Output is correct |
29 |
Correct |
210 ms |
106056 KB |
Output is correct |
30 |
Correct |
95 ms |
34268 KB |
Output is correct |
31 |
Correct |
92 ms |
34256 KB |
Output is correct |
32 |
Correct |
109 ms |
34644 KB |
Output is correct |
33 |
Correct |
111 ms |
34268 KB |
Output is correct |
34 |
Correct |
81 ms |
33936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21340 KB |
Output is correct |
2 |
Correct |
3 ms |
21340 KB |
Output is correct |
3 |
Correct |
3 ms |
21340 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
3 ms |
21340 KB |
Output is correct |
6 |
Correct |
3 ms |
21340 KB |
Output is correct |
7 |
Correct |
3 ms |
21340 KB |
Output is correct |
8 |
Correct |
3 ms |
21340 KB |
Output is correct |
9 |
Correct |
3 ms |
21340 KB |
Output is correct |
10 |
Correct |
4 ms |
21340 KB |
Output is correct |
11 |
Correct |
3 ms |
21172 KB |
Output is correct |
12 |
Correct |
3 ms |
21340 KB |
Output is correct |
13 |
Correct |
3 ms |
21340 KB |
Output is correct |
14 |
Correct |
3 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
21340 KB |
Output is correct |
16 |
Correct |
3 ms |
21340 KB |
Output is correct |
17 |
Correct |
3 ms |
21352 KB |
Output is correct |
18 |
Correct |
3 ms |
21340 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
3 ms |
21300 KB |
Output is correct |
21 |
Correct |
4 ms |
21340 KB |
Output is correct |
22 |
Correct |
3 ms |
21340 KB |
Output is correct |
23 |
Correct |
3 ms |
21340 KB |
Output is correct |
24 |
Correct |
4 ms |
21340 KB |
Output is correct |
25 |
Correct |
4 ms |
21340 KB |
Output is correct |
26 |
Correct |
4 ms |
21340 KB |
Output is correct |
27 |
Correct |
3 ms |
21340 KB |
Output is correct |
28 |
Correct |
4 ms |
21340 KB |
Output is correct |
29 |
Correct |
4 ms |
21340 KB |
Output is correct |
30 |
Correct |
4 ms |
21340 KB |
Output is correct |
31 |
Correct |
4 ms |
21340 KB |
Output is correct |
32 |
Correct |
4 ms |
21340 KB |
Output is correct |
33 |
Correct |
4 ms |
21340 KB |
Output is correct |
34 |
Correct |
3 ms |
21340 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
3 ms |
21340 KB |
Output is correct |
37 |
Correct |
3 ms |
21340 KB |
Output is correct |
38 |
Correct |
3 ms |
21340 KB |
Output is correct |
39 |
Correct |
76 ms |
28500 KB |
Output is correct |
40 |
Correct |
72 ms |
28496 KB |
Output is correct |
41 |
Correct |
75 ms |
28688 KB |
Output is correct |
42 |
Correct |
70 ms |
28504 KB |
Output is correct |
43 |
Correct |
55 ms |
29020 KB |
Output is correct |
44 |
Correct |
50 ms |
28244 KB |
Output is correct |
45 |
Correct |
64 ms |
48980 KB |
Output is correct |
46 |
Correct |
58 ms |
67156 KB |
Output is correct |
47 |
Correct |
113 ms |
34456 KB |
Output is correct |
48 |
Correct |
222 ms |
110672 KB |
Output is correct |
49 |
Correct |
210 ms |
106056 KB |
Output is correct |
50 |
Correct |
95 ms |
34268 KB |
Output is correct |
51 |
Correct |
92 ms |
34256 KB |
Output is correct |
52 |
Correct |
109 ms |
34644 KB |
Output is correct |
53 |
Correct |
111 ms |
34268 KB |
Output is correct |
54 |
Correct |
81 ms |
33936 KB |
Output is correct |
55 |
Correct |
13 ms |
22104 KB |
Output is correct |
56 |
Correct |
8 ms |
21852 KB |
Output is correct |
57 |
Correct |
44 ms |
29020 KB |
Output is correct |
58 |
Correct |
36 ms |
28100 KB |
Output is correct |
59 |
Correct |
80 ms |
67176 KB |
Output is correct |
60 |
Correct |
211 ms |
110780 KB |
Output is correct |
61 |
Correct |
132 ms |
36056 KB |
Output is correct |
62 |
Correct |
136 ms |
36756 KB |
Output is correct |
63 |
Correct |
133 ms |
36752 KB |
Output is correct |
64 |
Correct |
356 ms |
44368 KB |
Output is correct |
65 |
Correct |
95 ms |
36948 KB |
Output is correct |
66 |
Correct |
214 ms |
95312 KB |
Output is correct |
67 |
Correct |
93 ms |
36768 KB |
Output is correct |
68 |
Correct |
223 ms |
45380 KB |
Output is correct |
69 |
Correct |
249 ms |
49492 KB |
Output is correct |
70 |
Correct |
201 ms |
45412 KB |
Output is correct |