#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 |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14380 KB |
Output is correct |
8 |
Correct |
7 ms |
14400 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14400 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14408 KB |
Output is correct |
15 |
Correct |
6 ms |
14348 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14360 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14380 KB |
Output is correct |
8 |
Correct |
7 ms |
14400 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14400 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14408 KB |
Output is correct |
15 |
Correct |
6 ms |
14348 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14360 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14392 KB |
Output is correct |
21 |
Correct |
9 ms |
14664 KB |
Output is correct |
22 |
Correct |
7 ms |
14772 KB |
Output is correct |
23 |
Correct |
8 ms |
14668 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14716 KB |
Output is correct |
26 |
Correct |
8 ms |
14668 KB |
Output is correct |
27 |
Correct |
7 ms |
14596 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14664 KB |
Output is correct |
31 |
Correct |
8 ms |
14664 KB |
Output is correct |
32 |
Correct |
8 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14672 KB |
Output is correct |
34 |
Correct |
8 ms |
14676 KB |
Output is correct |
35 |
Correct |
8 ms |
14672 KB |
Output is correct |
36 |
Correct |
8 ms |
14660 KB |
Output is correct |
37 |
Correct |
9 ms |
14696 KB |
Output is correct |
38 |
Correct |
9 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14380 KB |
Output is correct |
8 |
Correct |
7 ms |
14400 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14400 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14408 KB |
Output is correct |
15 |
Correct |
6 ms |
14348 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14360 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
129 ms |
39108 KB |
Output is correct |
20 |
Correct |
127 ms |
39140 KB |
Output is correct |
21 |
Correct |
113 ms |
38896 KB |
Output is correct |
22 |
Correct |
127 ms |
38440 KB |
Output is correct |
23 |
Correct |
152 ms |
51412 KB |
Output is correct |
24 |
Correct |
123 ms |
41420 KB |
Output is correct |
25 |
Correct |
110 ms |
40000 KB |
Output is correct |
26 |
Correct |
54 ms |
48232 KB |
Output is correct |
27 |
Correct |
198 ms |
52588 KB |
Output is correct |
28 |
Correct |
283 ms |
94808 KB |
Output is correct |
29 |
Correct |
320 ms |
93004 KB |
Output is correct |
30 |
Correct |
175 ms |
52576 KB |
Output is correct |
31 |
Correct |
174 ms |
52628 KB |
Output is correct |
32 |
Correct |
302 ms |
52776 KB |
Output is correct |
33 |
Correct |
281 ms |
57648 KB |
Output is correct |
34 |
Correct |
385 ms |
90416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14380 KB |
Output is correct |
8 |
Correct |
7 ms |
14400 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14400 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14408 KB |
Output is correct |
15 |
Correct |
6 ms |
14348 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14360 KB |
Output is correct |
18 |
Correct |
7 ms |
14404 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14392 KB |
Output is correct |
21 |
Correct |
9 ms |
14664 KB |
Output is correct |
22 |
Correct |
7 ms |
14772 KB |
Output is correct |
23 |
Correct |
8 ms |
14668 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
14716 KB |
Output is correct |
26 |
Correct |
8 ms |
14668 KB |
Output is correct |
27 |
Correct |
7 ms |
14596 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14664 KB |
Output is correct |
31 |
Correct |
8 ms |
14664 KB |
Output is correct |
32 |
Correct |
8 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14672 KB |
Output is correct |
34 |
Correct |
8 ms |
14676 KB |
Output is correct |
35 |
Correct |
8 ms |
14672 KB |
Output is correct |
36 |
Correct |
8 ms |
14660 KB |
Output is correct |
37 |
Correct |
9 ms |
14696 KB |
Output is correct |
38 |
Correct |
9 ms |
14664 KB |
Output is correct |
39 |
Correct |
129 ms |
39108 KB |
Output is correct |
40 |
Correct |
127 ms |
39140 KB |
Output is correct |
41 |
Correct |
113 ms |
38896 KB |
Output is correct |
42 |
Correct |
127 ms |
38440 KB |
Output is correct |
43 |
Correct |
152 ms |
51412 KB |
Output is correct |
44 |
Correct |
123 ms |
41420 KB |
Output is correct |
45 |
Correct |
110 ms |
40000 KB |
Output is correct |
46 |
Correct |
54 ms |
48232 KB |
Output is correct |
47 |
Correct |
198 ms |
52588 KB |
Output is correct |
48 |
Correct |
283 ms |
94808 KB |
Output is correct |
49 |
Correct |
320 ms |
93004 KB |
Output is correct |
50 |
Correct |
175 ms |
52576 KB |
Output is correct |
51 |
Correct |
174 ms |
52628 KB |
Output is correct |
52 |
Correct |
302 ms |
52776 KB |
Output is correct |
53 |
Correct |
281 ms |
57648 KB |
Output is correct |
54 |
Correct |
385 ms |
90416 KB |
Output is correct |
55 |
Correct |
20 ms |
17868 KB |
Output is correct |
56 |
Correct |
21 ms |
16508 KB |
Output is correct |
57 |
Correct |
83 ms |
36704 KB |
Output is correct |
58 |
Correct |
48 ms |
29168 KB |
Output is correct |
59 |
Correct |
81 ms |
54396 KB |
Output is correct |
60 |
Correct |
324 ms |
93428 KB |
Output is correct |
61 |
Correct |
239 ms |
56068 KB |
Output is correct |
62 |
Correct |
172 ms |
52652 KB |
Output is correct |
63 |
Correct |
285 ms |
52752 KB |
Output is correct |
64 |
Correct |
474 ms |
107472 KB |
Output is correct |
65 |
Correct |
459 ms |
105620 KB |
Output is correct |
66 |
Correct |
298 ms |
88896 KB |
Output is correct |
67 |
Correct |
228 ms |
45508 KB |
Output is correct |
68 |
Correct |
345 ms |
69056 KB |
Output is correct |
69 |
Correct |
372 ms |
73876 KB |
Output is correct |
70 |
Correct |
349 ms |
66664 KB |
Output is correct |