#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ff first
#define ss second
vector<pair<ll, ll> > adj[200005];
map<ll, ll> sum_to_dist[200005];
ll dist[200005], sum[200005];
int N, K;
ll ans;
void dfs(int u, int p, ll s, int d) {
sum_to_dist[u][s] = d;
sum[u] = s;
dist[u] = d;
for (auto v : adj[u]) if (p != v.ff) dfs(v.ff, u, s + v.ss, d + 1);
}
void merge_dfs(int u, int p) {
ll target = K + 2 * sum[u];
for (auto v : adj[u]) {
if (v.ff == p) continue;
merge_dfs(v.ff, u);
if (sum_to_dist[v.ff].size() > sum_to_dist[u].size()) swap(sum_to_dist[v.ff], sum_to_dist[u]);
for (auto p : sum_to_dist[v.ff]) if (sum_to_dist[u].find(target - p.ff) != sum_to_dist[u].end()) ans = min(ans, sum_to_dist[u][target - p.ff] + p.ss - 2 * dist[u]);
for (auto p : sum_to_dist[v.ff]) {
if (sum_to_dist[u].find(p.ff) == sum_to_dist[u].end()) sum_to_dist[u].insert(p);
else sum_to_dist[u][p.ff] = min(sum_to_dist[u][p.ff], p.ss);
}
}
}
int best_path(int n, int k, int h[][2], int l[]) {
if (k == 1) return 0;
N = n; K = k; ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = h[i][0], v = h[i][1];
adj[u].pb(make_pair(v, l[i]));
adj[v].pb(make_pair(u, l[i]));
}
dfs(0, -1, 0, 0);
merge_dfs(0, -1);
return ans == INT_MAX ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
5 ms |
21336 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
4 ms |
21176 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21340 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
5 ms |
21336 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
4 ms |
21176 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21340 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21272 KB |
Output is correct |
21 |
Correct |
5 ms |
21340 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
5 ms |
21596 KB |
Output is correct |
24 |
Correct |
6 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
5 ms |
21596 KB |
Output is correct |
27 |
Correct |
5 ms |
21340 KB |
Output is correct |
28 |
Correct |
5 ms |
21592 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
6 ms |
21552 KB |
Output is correct |
31 |
Correct |
5 ms |
21596 KB |
Output is correct |
32 |
Correct |
5 ms |
21340 KB |
Output is correct |
33 |
Correct |
6 ms |
21632 KB |
Output is correct |
34 |
Correct |
5 ms |
21340 KB |
Output is correct |
35 |
Correct |
5 ms |
21364 KB |
Output is correct |
36 |
Correct |
5 ms |
21340 KB |
Output is correct |
37 |
Correct |
5 ms |
21536 KB |
Output is correct |
38 |
Correct |
6 ms |
21556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
5 ms |
21336 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
4 ms |
21176 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21340 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
116 ms |
44220 KB |
Output is correct |
20 |
Correct |
130 ms |
45444 KB |
Output is correct |
21 |
Correct |
104 ms |
44476 KB |
Output is correct |
22 |
Correct |
113 ms |
44840 KB |
Output is correct |
23 |
Correct |
135 ms |
57792 KB |
Output is correct |
24 |
Correct |
132 ms |
47516 KB |
Output is correct |
25 |
Correct |
95 ms |
46160 KB |
Output is correct |
26 |
Correct |
53 ms |
53844 KB |
Output is correct |
27 |
Correct |
176 ms |
54864 KB |
Output is correct |
28 |
Correct |
260 ms |
97124 KB |
Output is correct |
29 |
Correct |
275 ms |
95464 KB |
Output is correct |
30 |
Correct |
185 ms |
55532 KB |
Output is correct |
31 |
Correct |
168 ms |
55584 KB |
Output is correct |
32 |
Correct |
285 ms |
55376 KB |
Output is correct |
33 |
Correct |
270 ms |
60364 KB |
Output is correct |
34 |
Correct |
336 ms |
92684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
21080 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
5 ms |
21336 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21080 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21080 KB |
Output is correct |
8 |
Correct |
4 ms |
21080 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
21340 KB |
Output is correct |
12 |
Correct |
4 ms |
21176 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
4 ms |
21084 KB |
Output is correct |
17 |
Correct |
4 ms |
21340 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21272 KB |
Output is correct |
21 |
Correct |
5 ms |
21340 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
5 ms |
21596 KB |
Output is correct |
24 |
Correct |
6 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
5 ms |
21596 KB |
Output is correct |
27 |
Correct |
5 ms |
21340 KB |
Output is correct |
28 |
Correct |
5 ms |
21592 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
6 ms |
21552 KB |
Output is correct |
31 |
Correct |
5 ms |
21596 KB |
Output is correct |
32 |
Correct |
5 ms |
21340 KB |
Output is correct |
33 |
Correct |
6 ms |
21632 KB |
Output is correct |
34 |
Correct |
5 ms |
21340 KB |
Output is correct |
35 |
Correct |
5 ms |
21364 KB |
Output is correct |
36 |
Correct |
5 ms |
21340 KB |
Output is correct |
37 |
Correct |
5 ms |
21536 KB |
Output is correct |
38 |
Correct |
6 ms |
21556 KB |
Output is correct |
39 |
Correct |
116 ms |
44220 KB |
Output is correct |
40 |
Correct |
130 ms |
45444 KB |
Output is correct |
41 |
Correct |
104 ms |
44476 KB |
Output is correct |
42 |
Correct |
113 ms |
44840 KB |
Output is correct |
43 |
Correct |
135 ms |
57792 KB |
Output is correct |
44 |
Correct |
132 ms |
47516 KB |
Output is correct |
45 |
Correct |
95 ms |
46160 KB |
Output is correct |
46 |
Correct |
53 ms |
53844 KB |
Output is correct |
47 |
Correct |
176 ms |
54864 KB |
Output is correct |
48 |
Correct |
260 ms |
97124 KB |
Output is correct |
49 |
Correct |
275 ms |
95464 KB |
Output is correct |
50 |
Correct |
185 ms |
55532 KB |
Output is correct |
51 |
Correct |
168 ms |
55584 KB |
Output is correct |
52 |
Correct |
285 ms |
55376 KB |
Output is correct |
53 |
Correct |
270 ms |
60364 KB |
Output is correct |
54 |
Correct |
336 ms |
92684 KB |
Output is correct |
55 |
Correct |
17 ms |
24412 KB |
Output is correct |
56 |
Correct |
10 ms |
22876 KB |
Output is correct |
57 |
Correct |
67 ms |
41924 KB |
Output is correct |
58 |
Correct |
43 ms |
35532 KB |
Output is correct |
59 |
Correct |
92 ms |
60212 KB |
Output is correct |
60 |
Correct |
274 ms |
96040 KB |
Output is correct |
61 |
Correct |
219 ms |
58384 KB |
Output is correct |
62 |
Correct |
182 ms |
55380 KB |
Output is correct |
63 |
Correct |
239 ms |
55376 KB |
Output is correct |
64 |
Correct |
394 ms |
108624 KB |
Output is correct |
65 |
Correct |
406 ms |
108476 KB |
Output is correct |
66 |
Correct |
295 ms |
89328 KB |
Output is correct |
67 |
Correct |
154 ms |
49080 KB |
Output is correct |
68 |
Correct |
314 ms |
71572 KB |
Output is correct |
69 |
Correct |
345 ms |
73980 KB |
Output is correct |
70 |
Correct |
316 ms |
70236 KB |
Output is correct |