#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int SIZE = 200000;
int n, k, ans = INT_MAX;
int sub[SIZE], table[1000001];
vector<pii> G[SIZE];
bitset<SIZE> vis;
bitset<1000001> use;
int calc(int V, int P) {
sub[V] = 1;
for(pii X : G[V])
if (!vis[X.first] and X.first != P)
sub[V] += calc(X.first, V);
return sub[V];
}
int centroid(int V, int P, int sz) {
for(pii X : G[V])
if (!vis[X.first] and X.first != P and sub[X.first] > sz / 2)
return centroid(X.first, V, sz);
return V;
}
void calc2(int V, int P, int len, int cost, vector<pii> &vec) {
if (cost > k)
return;
vec.emplace_back(make_pair(cost, len));
for(pii X : G[V])
if (!vis[X.first] and X.first != P)
calc2(X.first, V, len + 1, cost + X.second, vec);
}
void centroidDecomposite(int V) {
calc(V, -1);
int C = centroid(V, -1, sub[V]);
vector<int> update(1, 0);
use[0] = true, table[0] = 0;
for(pii X : G[C]) {
if (vis[X.first]) continue;
vector<pii> tmp;
calc2(X.first, C, 1, X.second, tmp);
for(pii Y : tmp) {
int cost = Y.first, len = Y.second;
if (!use[k - cost]) continue;
ans = min(ans, len + table[k - cost]);
}
for(pii Y : tmp) {
if (!use[Y.first]) {
table[Y.first] = Y.second, use[Y.first] = true;
update.emplace_back(Y.first);
} else {
table[Y.first] = min(table[Y.first], Y.second);
}
}
}
for(int X : update)
use[X] = false;
vis[C] = true;
for(pii X : G[C])
if (!vis[X.first])
centroidDecomposite(X.first);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for(int i = 0; i < N - 1; i++) {
G[H[i][0]].emplace_back(make_pair(H[i][1], L[i]));
G[H[i][1]].emplace_back(make_pair(H[i][0], L[i]));
}
centroidDecomposite(0);
return ans == INT_MAX ? -1 : ans;
}
/*
int A[SIZE - 1][2];
int B[SIZE - 1];
int main() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n - 1; i++)
cin >> A[i][0] >> A[i][1];
for(int i = 0; i < n - 1; i++)
cin >> B[i];
cout << best_path(n, k, A, B) << '\n';
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
7628 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
8012 KB |
Output is correct |
25 |
Correct |
4 ms |
6860 KB |
Output is correct |
26 |
Correct |
4 ms |
6348 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
4 ms |
6476 KB |
Output is correct |
31 |
Correct |
4 ms |
6860 KB |
Output is correct |
32 |
Correct |
4 ms |
6988 KB |
Output is correct |
33 |
Correct |
5 ms |
7840 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8200 KB |
Output is correct |
36 |
Correct |
6 ms |
8396 KB |
Output is correct |
37 |
Correct |
4 ms |
6988 KB |
Output is correct |
38 |
Correct |
4 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
112 ms |
10308 KB |
Output is correct |
20 |
Correct |
110 ms |
10344 KB |
Output is correct |
21 |
Correct |
131 ms |
10568 KB |
Output is correct |
22 |
Correct |
114 ms |
10604 KB |
Output is correct |
23 |
Correct |
73 ms |
10432 KB |
Output is correct |
24 |
Correct |
52 ms |
10316 KB |
Output is correct |
25 |
Correct |
108 ms |
13036 KB |
Output is correct |
26 |
Correct |
91 ms |
16760 KB |
Output is correct |
27 |
Correct |
166 ms |
15708 KB |
Output is correct |
28 |
Correct |
259 ms |
26872 KB |
Output is correct |
29 |
Correct |
247 ms |
25968 KB |
Output is correct |
30 |
Correct |
164 ms |
15600 KB |
Output is correct |
31 |
Correct |
171 ms |
15756 KB |
Output is correct |
32 |
Correct |
200 ms |
15640 KB |
Output is correct |
33 |
Correct |
206 ms |
14532 KB |
Output is correct |
34 |
Correct |
166 ms |
14452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
2 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
5 ms |
7628 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
8012 KB |
Output is correct |
25 |
Correct |
4 ms |
6860 KB |
Output is correct |
26 |
Correct |
4 ms |
6348 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
4 ms |
6476 KB |
Output is correct |
31 |
Correct |
4 ms |
6860 KB |
Output is correct |
32 |
Correct |
4 ms |
6988 KB |
Output is correct |
33 |
Correct |
5 ms |
7840 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8200 KB |
Output is correct |
36 |
Correct |
6 ms |
8396 KB |
Output is correct |
37 |
Correct |
4 ms |
6988 KB |
Output is correct |
38 |
Correct |
4 ms |
6220 KB |
Output is correct |
39 |
Correct |
112 ms |
10308 KB |
Output is correct |
40 |
Correct |
110 ms |
10344 KB |
Output is correct |
41 |
Correct |
131 ms |
10568 KB |
Output is correct |
42 |
Correct |
114 ms |
10604 KB |
Output is correct |
43 |
Correct |
73 ms |
10432 KB |
Output is correct |
44 |
Correct |
52 ms |
10316 KB |
Output is correct |
45 |
Correct |
108 ms |
13036 KB |
Output is correct |
46 |
Correct |
91 ms |
16760 KB |
Output is correct |
47 |
Correct |
166 ms |
15708 KB |
Output is correct |
48 |
Correct |
259 ms |
26872 KB |
Output is correct |
49 |
Correct |
247 ms |
25968 KB |
Output is correct |
50 |
Correct |
164 ms |
15600 KB |
Output is correct |
51 |
Correct |
171 ms |
15756 KB |
Output is correct |
52 |
Correct |
200 ms |
15640 KB |
Output is correct |
53 |
Correct |
206 ms |
14532 KB |
Output is correct |
54 |
Correct |
166 ms |
14452 KB |
Output is correct |
55 |
Correct |
10 ms |
5580 KB |
Output is correct |
56 |
Correct |
10 ms |
5540 KB |
Output is correct |
57 |
Correct |
83 ms |
11132 KB |
Output is correct |
58 |
Correct |
35 ms |
10532 KB |
Output is correct |
59 |
Correct |
109 ms |
19216 KB |
Output is correct |
60 |
Correct |
402 ms |
34988 KB |
Output is correct |
61 |
Correct |
179 ms |
18756 KB |
Output is correct |
62 |
Correct |
188 ms |
18772 KB |
Output is correct |
63 |
Correct |
258 ms |
18852 KB |
Output is correct |
64 |
Correct |
387 ms |
21828 KB |
Output is correct |
65 |
Correct |
215 ms |
19508 KB |
Output is correct |
66 |
Correct |
355 ms |
31340 KB |
Output is correct |
67 |
Correct |
106 ms |
19364 KB |
Output is correct |
68 |
Correct |
238 ms |
23240 KB |
Output is correct |
69 |
Correct |
247 ms |
23348 KB |
Output is correct |
70 |
Correct |
234 ms |
22608 KB |
Output is correct |