#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define pb push_back
#define f first
#define s second
const int maxn = 2e5 + 1;
vector<pii> adj[maxn];
map<ll, ll> info[maxn];
ll dist[maxn], sum[maxn]; // define these relative to root
int N, K;
ll ret;
void precomp(int u, int p, ll c, int h) {
info[u][c] = h;
sum[u] = c;
dist[u] = h;
for (auto n : adj[u]) {
if (p == n.f) { continue; }
precomp(n.f, u, c + n.s, h + 1);
}
}
void small_to_large_merge(int u, int p) {
ll target = K + 2 * sum[u];
for (auto n : adj[u]) {
if (n.f == p) { continue; }
small_to_large_merge(n.f, u);
if (info[n.f].size() > info[u].size()) { swap(info[n.f], info[u]); }
for (auto i : info[n.f]) {
if (info[u].find(target - i.f) != info[u].end()) {
ret = min(ret, info[u][target - i.f] + i.s - 2 * dist[u]);
}
}
for (auto i : info[n.f]) {
if (info[u].find(i.f) == info[u].end()) {
info[u].insert(i);
} else {
info[u][i.f] = min(info[u][i.f], i.s);
}
}
}
}
int best_path(int n, int k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
N = n;
K = k;
ret = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].pb(pii(v, weights[i]));
adj[v].pb(pii(u, weights[i]));
}
precomp(0, -1, 0, 0);
small_to_large_merge(0, -1);
return ret == INT_MAX ? -1 : ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21336 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21340 KB |
Output is correct |
8 |
Correct |
4 ms |
21340 KB |
Output is correct |
9 |
Correct |
5 ms |
21336 KB |
Output is correct |
10 |
Correct |
5 ms |
21340 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21336 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21340 KB |
Output is correct |
8 |
Correct |
4 ms |
21340 KB |
Output is correct |
9 |
Correct |
5 ms |
21336 KB |
Output is correct |
10 |
Correct |
5 ms |
21340 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21336 KB |
Output is correct |
21 |
Correct |
5 ms |
21344 KB |
Output is correct |
22 |
Correct |
7 ms |
21596 KB |
Output is correct |
23 |
Correct |
6 ms |
21592 KB |
Output is correct |
24 |
Correct |
5 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
21596 KB |
Output is correct |
27 |
Correct |
5 ms |
21340 KB |
Output is correct |
28 |
Correct |
5 ms |
21596 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
5 ms |
21596 KB |
Output is correct |
31 |
Correct |
5 ms |
21604 KB |
Output is correct |
32 |
Correct |
7 ms |
21348 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 |
21564 KB |
Output is correct |
36 |
Correct |
5 ms |
21336 KB |
Output is correct |
37 |
Correct |
5 ms |
21336 KB |
Output is correct |
38 |
Correct |
5 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21336 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21340 KB |
Output is correct |
8 |
Correct |
4 ms |
21340 KB |
Output is correct |
9 |
Correct |
5 ms |
21336 KB |
Output is correct |
10 |
Correct |
5 ms |
21340 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
19 |
Correct |
112 ms |
44280 KB |
Output is correct |
20 |
Correct |
144 ms |
45220 KB |
Output is correct |
21 |
Correct |
110 ms |
44500 KB |
Output is correct |
22 |
Correct |
118 ms |
44848 KB |
Output is correct |
23 |
Correct |
147 ms |
57804 KB |
Output is correct |
24 |
Correct |
110 ms |
47628 KB |
Output is correct |
25 |
Correct |
106 ms |
46248 KB |
Output is correct |
26 |
Correct |
54 ms |
54096 KB |
Output is correct |
27 |
Correct |
171 ms |
54864 KB |
Output is correct |
28 |
Correct |
301 ms |
97052 KB |
Output is correct |
29 |
Correct |
276 ms |
95456 KB |
Output is correct |
30 |
Correct |
188 ms |
55288 KB |
Output is correct |
31 |
Correct |
205 ms |
55396 KB |
Output is correct |
32 |
Correct |
258 ms |
55496 KB |
Output is correct |
33 |
Correct |
272 ms |
60544 KB |
Output is correct |
34 |
Correct |
307 ms |
92756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
21336 KB |
Output is correct |
2 |
Correct |
5 ms |
21340 KB |
Output is correct |
3 |
Correct |
4 ms |
21340 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21340 KB |
Output is correct |
8 |
Correct |
4 ms |
21340 KB |
Output is correct |
9 |
Correct |
5 ms |
21336 KB |
Output is correct |
10 |
Correct |
5 ms |
21340 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21080 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
5 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21340 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
21336 KB |
Output is correct |
21 |
Correct |
5 ms |
21344 KB |
Output is correct |
22 |
Correct |
7 ms |
21596 KB |
Output is correct |
23 |
Correct |
6 ms |
21592 KB |
Output is correct |
24 |
Correct |
5 ms |
21592 KB |
Output is correct |
25 |
Correct |
5 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
21596 KB |
Output is correct |
27 |
Correct |
5 ms |
21340 KB |
Output is correct |
28 |
Correct |
5 ms |
21596 KB |
Output is correct |
29 |
Correct |
5 ms |
21596 KB |
Output is correct |
30 |
Correct |
5 ms |
21596 KB |
Output is correct |
31 |
Correct |
5 ms |
21604 KB |
Output is correct |
32 |
Correct |
7 ms |
21348 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 |
21564 KB |
Output is correct |
36 |
Correct |
5 ms |
21336 KB |
Output is correct |
37 |
Correct |
5 ms |
21336 KB |
Output is correct |
38 |
Correct |
5 ms |
21340 KB |
Output is correct |
39 |
Correct |
112 ms |
44280 KB |
Output is correct |
40 |
Correct |
144 ms |
45220 KB |
Output is correct |
41 |
Correct |
110 ms |
44500 KB |
Output is correct |
42 |
Correct |
118 ms |
44848 KB |
Output is correct |
43 |
Correct |
147 ms |
57804 KB |
Output is correct |
44 |
Correct |
110 ms |
47628 KB |
Output is correct |
45 |
Correct |
106 ms |
46248 KB |
Output is correct |
46 |
Correct |
54 ms |
54096 KB |
Output is correct |
47 |
Correct |
171 ms |
54864 KB |
Output is correct |
48 |
Correct |
301 ms |
97052 KB |
Output is correct |
49 |
Correct |
276 ms |
95456 KB |
Output is correct |
50 |
Correct |
188 ms |
55288 KB |
Output is correct |
51 |
Correct |
205 ms |
55396 KB |
Output is correct |
52 |
Correct |
258 ms |
55496 KB |
Output is correct |
53 |
Correct |
272 ms |
60544 KB |
Output is correct |
54 |
Correct |
307 ms |
92756 KB |
Output is correct |
55 |
Correct |
18 ms |
24412 KB |
Output is correct |
56 |
Correct |
10 ms |
22876 KB |
Output is correct |
57 |
Correct |
62 ms |
42064 KB |
Output is correct |
58 |
Correct |
43 ms |
35432 KB |
Output is correct |
59 |
Correct |
104 ms |
60280 KB |
Output is correct |
60 |
Correct |
280 ms |
96024 KB |
Output is correct |
61 |
Correct |
211 ms |
58068 KB |
Output is correct |
62 |
Correct |
161 ms |
55380 KB |
Output is correct |
63 |
Correct |
248 ms |
55380 KB |
Output is correct |
64 |
Correct |
407 ms |
108560 KB |
Output is correct |
65 |
Correct |
415 ms |
108756 KB |
Output is correct |
66 |
Correct |
294 ms |
89260 KB |
Output is correct |
67 |
Correct |
189 ms |
49916 KB |
Output is correct |
68 |
Correct |
337 ms |
71356 KB |
Output is correct |
69 |
Correct |
362 ms |
73824 KB |
Output is correct |
70 |
Correct |
333 ms |
69752 KB |
Output is correct |