#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define pii pair<int, int>
bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }
bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }
struct Highway{
int a, b, w;
};
const int N = 2e5 + 5;
int n, K, ans = 1e9;
ll c[N];
int d[N];
Highway h[N];
map<int, int> mp[N];
vector<pii> adj[N];
void dfs(int node, int prev, ll sum = 0, int dist = 0){
c[node] = sum;
d[node] = dist;
mp[node][sum] = dist;
for(auto x : adj[node]){
if(x.f == prev) continue;
dfs(x.f, node, sum + x.s, dist + 1);
}
}
void dfs2(int node, int prev){
ll t = K + 2 * c[node];
for(auto x : adj[node]){
if(x.f == prev) continue;
dfs2(x.f, node);
if(sz(mp[x.f]) > sz(mp[node])) {
swap(mp[x.f], mp[node]);
}
for(auto y : mp[x.f]){
if(mp[node].count(t - y.f)){
ckmin(ans, mp[node][t - y.f] + y.s - 2 * d[node]);
}
}
for(auto y : mp[x.f]){
if(!mp[node].count(y.f)){
mp[node][y.f] = y.s;
}
else{
ckmin(mp[node][y.f], y.s);
}
}
}
}
int best_path(int n, int k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
K = k;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb({v, weights[i]});
adj[v].pb({u, weights[i]});
}
dfs(0, -1);
dfs2(0, -1);
return (ans == 1e9 ? -1 : ans);
}
// int32_t main() {
// cin.tie(0)->sync_with_stdio(0);
// cin >> n >> k;
// for(int i = 0; i < n - 1; i++){
// cin >> h[i].a >> h[i].b;
// }
// for(int i = 0; i < n - 1; i++){
// cin >> h[i].w;
// }
// for(int i = 0; i < n - 1; i++){
// adj[h[i].a].pb({h[i].b, h[i].w});
// adj[h[i].b].pb({h[i].a, h[i].w});
// }
// dfs(0, -1);
// dfs2(0, -1);
// cout << (ans < 1e9 ? ans : -1) << '\n';
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14484 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14452 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
8 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14404 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14592 KB |
Output is correct |
16 |
Correct |
6 ms |
14540 KB |
Output is correct |
17 |
Correct |
8 ms |
14428 KB |
Output is correct |
18 |
Correct |
7 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14484 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14452 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
8 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14404 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14592 KB |
Output is correct |
16 |
Correct |
6 ms |
14540 KB |
Output is correct |
17 |
Correct |
8 ms |
14428 KB |
Output is correct |
18 |
Correct |
7 ms |
14428 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
9 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14680 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
8 ms |
14684 KB |
Output is correct |
24 |
Correct |
8 ms |
14684 KB |
Output is correct |
25 |
Correct |
8 ms |
14684 KB |
Output is correct |
26 |
Correct |
10 ms |
14712 KB |
Output is correct |
27 |
Correct |
8 ms |
14684 KB |
Output is correct |
28 |
Correct |
8 ms |
14684 KB |
Output is correct |
29 |
Correct |
8 ms |
14684 KB |
Output is correct |
30 |
Correct |
7 ms |
14612 KB |
Output is correct |
31 |
Correct |
8 ms |
14684 KB |
Output is correct |
32 |
Correct |
8 ms |
14684 KB |
Output is correct |
33 |
Correct |
7 ms |
14680 KB |
Output is correct |
34 |
Correct |
7 ms |
14584 KB |
Output is correct |
35 |
Correct |
7 ms |
14684 KB |
Output is correct |
36 |
Correct |
7 ms |
14684 KB |
Output is correct |
37 |
Correct |
6 ms |
14684 KB |
Output is correct |
38 |
Correct |
7 ms |
14728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14484 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14452 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
8 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14404 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14592 KB |
Output is correct |
16 |
Correct |
6 ms |
14540 KB |
Output is correct |
17 |
Correct |
8 ms |
14428 KB |
Output is correct |
18 |
Correct |
7 ms |
14428 KB |
Output is correct |
19 |
Correct |
91 ms |
33612 KB |
Output is correct |
20 |
Correct |
102 ms |
33616 KB |
Output is correct |
21 |
Correct |
84 ms |
33220 KB |
Output is correct |
22 |
Correct |
85 ms |
33048 KB |
Output is correct |
23 |
Correct |
126 ms |
42576 KB |
Output is correct |
24 |
Correct |
94 ms |
35668 KB |
Output is correct |
25 |
Correct |
75 ms |
34640 KB |
Output is correct |
26 |
Correct |
52 ms |
41596 KB |
Output is correct |
27 |
Correct |
135 ms |
43856 KB |
Output is correct |
28 |
Correct |
250 ms |
78416 KB |
Output is correct |
29 |
Correct |
222 ms |
76736 KB |
Output is correct |
30 |
Correct |
130 ms |
43864 KB |
Output is correct |
31 |
Correct |
129 ms |
43860 KB |
Output is correct |
32 |
Correct |
211 ms |
43940 KB |
Output is correct |
33 |
Correct |
187 ms |
47360 KB |
Output is correct |
34 |
Correct |
269 ms |
72276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
6 ms |
14428 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14484 KB |
Output is correct |
6 |
Correct |
6 ms |
14428 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14452 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
8 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14404 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14592 KB |
Output is correct |
16 |
Correct |
6 ms |
14540 KB |
Output is correct |
17 |
Correct |
8 ms |
14428 KB |
Output is correct |
18 |
Correct |
7 ms |
14428 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
9 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14680 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
8 ms |
14684 KB |
Output is correct |
24 |
Correct |
8 ms |
14684 KB |
Output is correct |
25 |
Correct |
8 ms |
14684 KB |
Output is correct |
26 |
Correct |
10 ms |
14712 KB |
Output is correct |
27 |
Correct |
8 ms |
14684 KB |
Output is correct |
28 |
Correct |
8 ms |
14684 KB |
Output is correct |
29 |
Correct |
8 ms |
14684 KB |
Output is correct |
30 |
Correct |
7 ms |
14612 KB |
Output is correct |
31 |
Correct |
8 ms |
14684 KB |
Output is correct |
32 |
Correct |
8 ms |
14684 KB |
Output is correct |
33 |
Correct |
7 ms |
14680 KB |
Output is correct |
34 |
Correct |
7 ms |
14584 KB |
Output is correct |
35 |
Correct |
7 ms |
14684 KB |
Output is correct |
36 |
Correct |
7 ms |
14684 KB |
Output is correct |
37 |
Correct |
6 ms |
14684 KB |
Output is correct |
38 |
Correct |
7 ms |
14728 KB |
Output is correct |
39 |
Correct |
91 ms |
33612 KB |
Output is correct |
40 |
Correct |
102 ms |
33616 KB |
Output is correct |
41 |
Correct |
84 ms |
33220 KB |
Output is correct |
42 |
Correct |
85 ms |
33048 KB |
Output is correct |
43 |
Correct |
126 ms |
42576 KB |
Output is correct |
44 |
Correct |
94 ms |
35668 KB |
Output is correct |
45 |
Correct |
75 ms |
34640 KB |
Output is correct |
46 |
Correct |
52 ms |
41596 KB |
Output is correct |
47 |
Correct |
135 ms |
43856 KB |
Output is correct |
48 |
Correct |
250 ms |
78416 KB |
Output is correct |
49 |
Correct |
222 ms |
76736 KB |
Output is correct |
50 |
Correct |
130 ms |
43864 KB |
Output is correct |
51 |
Correct |
129 ms |
43860 KB |
Output is correct |
52 |
Correct |
211 ms |
43940 KB |
Output is correct |
53 |
Correct |
187 ms |
47360 KB |
Output is correct |
54 |
Correct |
269 ms |
72276 KB |
Output is correct |
55 |
Correct |
16 ms |
16988 KB |
Output is correct |
56 |
Correct |
12 ms |
16216 KB |
Output is correct |
57 |
Correct |
55 ms |
31572 KB |
Output is correct |
58 |
Correct |
38 ms |
26568 KB |
Output is correct |
59 |
Correct |
84 ms |
46420 KB |
Output is correct |
60 |
Correct |
215 ms |
77280 KB |
Output is correct |
61 |
Correct |
164 ms |
46676 KB |
Output is correct |
62 |
Correct |
135 ms |
43848 KB |
Output is correct |
63 |
Correct |
249 ms |
44112 KB |
Output is correct |
64 |
Correct |
384 ms |
85328 KB |
Output is correct |
65 |
Correct |
370 ms |
84048 KB |
Output is correct |
66 |
Correct |
259 ms |
73300 KB |
Output is correct |
67 |
Correct |
126 ms |
39924 KB |
Output is correct |
68 |
Correct |
276 ms |
56816 KB |
Output is correct |
69 |
Correct |
284 ms |
60752 KB |
Output is correct |
70 |
Correct |
262 ms |
55248 KB |
Output is correct |