#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5;
const int64_t INF = 1e18L;
vector<pair<int, int>> g[1 + kN];
int N, depth[1 + kN], sol[kN];
bitset<1 + kN> mark, covered;
void maxSelf(int64_t &x, int64_t y) {
if (x < y) {
x = y;
}
}
void minSelf(int64_t &x, int64_t y) {
if (y < x) {
x = y;
}
}
int64_t dfs(int u, int par, int64_t radius) {
if ((int)g[u].size() == 1 && par) {
return 0;
}
int64_t mx = -INF, mn = INF;
for (auto it : g[u]) {
int v, w;
tie(v, w) = it;
if (v != par) {
int64_t ret = dfs(v, u, radius);
if (ret == 0 && covered[v]) { // v este acoperit si nimic din subarborele sau nu ma mai poate ajuta / incurca mai mult decat u
maxSelf(mx, 0LL);
minSelf(mn, 0LL);
continue;
}
if (ret < 0 && ret + w <= 0) { // u este acoperit
covered[u] = true;
}
if (ret < 0 && ret + w > 0) { // v sigur e acoperit si incepand de la u nu se mai acopera nimic nou, deci "resetez"
ret = 0;
} else { // creste distanta fata de ultimul nod marcat si mai mult
ret += w;
}
if (ret > radius) { // trebuie sa il marchez pe v acum ca sa acopere alte noduri nemarcate, pentru ca deja u nu poate ajuta
sol[N++] = v;
mark[v] = true;
covered[v] = true;
ret = min(-radius + w, (int64_t)0); // aproape analog cu cazurile anterioare
if (w <= radius) {
covered[u] = true;
}
}
maxSelf(mx, ret);
minSelf(mn, ret);
}
}
if (mx <= -mn) { // nodul de unde trebuie sa incep sa marchez din cauza lui mx(ca s-ar termina) va fi ori acoperit ori voi mai putea amana o eventuala marcare a sa datorita lui mn
return mn;
}
return mx;
}
bool check(int64_t m, int k) {
N = 0;
mark.reset();
covered.reset();
if (dfs(1, 0, m) > 0) {
sol[N++] = 1;
mark[1] = true;
} else if (!covered[1]) {
sol[N++] = 1;
mark[1] = true;
}
return N <= k;
}
void testCase() {
int n, k;
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
int64_t ans = 0, step = (1LL << 50);
while (step) {
if (!check(ans | step, k)) {
ans |= step;
}
step /= 2;
}
ans += 1;
check(ans, k);
for (int v = 1; v <= n && N < k; ++v) {
if (!mark[v]) {
sol[N++] = v;
}
}
cout << ans << '\n';
for (int i = 0; i < k; ++i) {
cout << sol[i] << ' ';
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 1; tc <= tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
28616 KB |
Output is correct |
2 |
Correct |
399 ms |
29172 KB |
Output is correct |
3 |
Correct |
174 ms |
13072 KB |
Output is correct |
4 |
Correct |
857 ms |
12104 KB |
Output is correct |
5 |
Correct |
696 ms |
15860 KB |
Output is correct |
6 |
Correct |
702 ms |
15820 KB |
Output is correct |
7 |
Correct |
756 ms |
14668 KB |
Output is correct |
8 |
Correct |
807 ms |
15180 KB |
Output is correct |
9 |
Correct |
796 ms |
15656 KB |
Output is correct |
10 |
Correct |
763 ms |
15896 KB |
Output is correct |
11 |
Correct |
602 ms |
17664 KB |
Output is correct |
12 |
Correct |
544 ms |
17868 KB |
Output is correct |
13 |
Correct |
625 ms |
19020 KB |
Output is correct |
14 |
Correct |
534 ms |
16460 KB |
Output is correct |
15 |
Correct |
568 ms |
15944 KB |
Output is correct |
16 |
Correct |
562 ms |
17196 KB |
Output is correct |
17 |
Correct |
553 ms |
16588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
29500 KB |
Output is correct |
2 |
Correct |
342 ms |
28796 KB |
Output is correct |
3 |
Correct |
363 ms |
27664 KB |
Output is correct |
4 |
Correct |
355 ms |
27644 KB |
Output is correct |
5 |
Correct |
377 ms |
30328 KB |
Output is correct |
6 |
Correct |
383 ms |
29676 KB |
Output is correct |
7 |
Correct |
406 ms |
30864 KB |
Output is correct |
8 |
Correct |
420 ms |
29756 KB |
Output is correct |
9 |
Correct |
352 ms |
29508 KB |
Output is correct |
10 |
Correct |
356 ms |
29048 KB |
Output is correct |
11 |
Correct |
333 ms |
28156 KB |
Output is correct |
12 |
Correct |
399 ms |
31308 KB |
Output is correct |
13 |
Correct |
417 ms |
31192 KB |
Output is correct |
14 |
Correct |
436 ms |
30236 KB |
Output is correct |
15 |
Correct |
346 ms |
29008 KB |
Output is correct |
16 |
Correct |
323 ms |
28032 KB |
Output is correct |
17 |
Correct |
323 ms |
27848 KB |
Output is correct |
18 |
Correct |
353 ms |
29300 KB |
Output is correct |
19 |
Correct |
345 ms |
29616 KB |
Output is correct |
20 |
Correct |
368 ms |
30188 KB |
Output is correct |
21 |
Correct |
344 ms |
29340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
321 ms |
28616 KB |
Output is correct |
16 |
Correct |
399 ms |
29172 KB |
Output is correct |
17 |
Correct |
174 ms |
13072 KB |
Output is correct |
18 |
Correct |
857 ms |
12104 KB |
Output is correct |
19 |
Correct |
696 ms |
15860 KB |
Output is correct |
20 |
Correct |
702 ms |
15820 KB |
Output is correct |
21 |
Correct |
756 ms |
14668 KB |
Output is correct |
22 |
Correct |
807 ms |
15180 KB |
Output is correct |
23 |
Correct |
796 ms |
15656 KB |
Output is correct |
24 |
Correct |
763 ms |
15896 KB |
Output is correct |
25 |
Correct |
602 ms |
17664 KB |
Output is correct |
26 |
Correct |
544 ms |
17868 KB |
Output is correct |
27 |
Correct |
625 ms |
19020 KB |
Output is correct |
28 |
Correct |
534 ms |
16460 KB |
Output is correct |
29 |
Correct |
568 ms |
15944 KB |
Output is correct |
30 |
Correct |
562 ms |
17196 KB |
Output is correct |
31 |
Correct |
553 ms |
16588 KB |
Output is correct |
32 |
Correct |
378 ms |
29500 KB |
Output is correct |
33 |
Correct |
342 ms |
28796 KB |
Output is correct |
34 |
Correct |
363 ms |
27664 KB |
Output is correct |
35 |
Correct |
355 ms |
27644 KB |
Output is correct |
36 |
Correct |
377 ms |
30328 KB |
Output is correct |
37 |
Correct |
383 ms |
29676 KB |
Output is correct |
38 |
Correct |
406 ms |
30864 KB |
Output is correct |
39 |
Correct |
420 ms |
29756 KB |
Output is correct |
40 |
Correct |
352 ms |
29508 KB |
Output is correct |
41 |
Correct |
356 ms |
29048 KB |
Output is correct |
42 |
Correct |
333 ms |
28156 KB |
Output is correct |
43 |
Correct |
399 ms |
31308 KB |
Output is correct |
44 |
Correct |
417 ms |
31192 KB |
Output is correct |
45 |
Correct |
436 ms |
30236 KB |
Output is correct |
46 |
Correct |
346 ms |
29008 KB |
Output is correct |
47 |
Correct |
323 ms |
28032 KB |
Output is correct |
48 |
Correct |
323 ms |
27848 KB |
Output is correct |
49 |
Correct |
353 ms |
29300 KB |
Output is correct |
50 |
Correct |
345 ms |
29616 KB |
Output is correct |
51 |
Correct |
368 ms |
30188 KB |
Output is correct |
52 |
Correct |
344 ms |
29340 KB |
Output is correct |
53 |
Correct |
738 ms |
16136 KB |
Output is correct |
54 |
Correct |
781 ms |
16532 KB |
Output is correct |
55 |
Correct |
860 ms |
16948 KB |
Output is correct |
56 |
Correct |
823 ms |
16696 KB |
Output is correct |
57 |
Correct |
874 ms |
17356 KB |
Output is correct |
58 |
Correct |
796 ms |
16228 KB |
Output is correct |
59 |
Correct |
892 ms |
18588 KB |
Output is correct |
60 |
Correct |
813 ms |
15496 KB |
Output is correct |
61 |
Correct |
690 ms |
14548 KB |
Output is correct |
62 |
Correct |
733 ms |
15472 KB |
Output is correct |
63 |
Correct |
895 ms |
15552 KB |
Output is correct |
64 |
Correct |
747 ms |
16788 KB |
Output is correct |
65 |
Correct |
793 ms |
15956 KB |
Output is correct |
66 |
Correct |
833 ms |
16432 KB |
Output is correct |
67 |
Correct |
794 ms |
15316 KB |
Output is correct |
68 |
Correct |
947 ms |
18028 KB |
Output is correct |
69 |
Correct |
347 ms |
33384 KB |
Output is correct |
70 |
Correct |
335 ms |
31228 KB |
Output is correct |
71 |
Correct |
405 ms |
34428 KB |
Output is correct |
72 |
Correct |
170 ms |
16956 KB |
Output is correct |
73 |
Correct |
146 ms |
15924 KB |
Output is correct |
74 |
Correct |
171 ms |
16964 KB |
Output is correct |
75 |
Correct |
586 ms |
18252 KB |
Output is correct |
76 |
Correct |
628 ms |
17292 KB |
Output is correct |
77 |
Correct |
611 ms |
17428 KB |
Output is correct |
78 |
Correct |
730 ms |
17160 KB |
Output is correct |