#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 |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4988 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
28528 KB |
Output is correct |
2 |
Correct |
386 ms |
29164 KB |
Output is correct |
3 |
Correct |
229 ms |
13168 KB |
Output is correct |
4 |
Correct |
997 ms |
12048 KB |
Output is correct |
5 |
Correct |
968 ms |
11832 KB |
Output is correct |
6 |
Correct |
898 ms |
11840 KB |
Output is correct |
7 |
Correct |
859 ms |
11900 KB |
Output is correct |
8 |
Correct |
961 ms |
12264 KB |
Output is correct |
9 |
Correct |
904 ms |
12072 KB |
Output is correct |
10 |
Correct |
951 ms |
12240 KB |
Output is correct |
11 |
Correct |
679 ms |
13456 KB |
Output is correct |
12 |
Correct |
591 ms |
13796 KB |
Output is correct |
13 |
Correct |
720 ms |
14596 KB |
Output is correct |
14 |
Correct |
574 ms |
13644 KB |
Output is correct |
15 |
Correct |
622 ms |
13144 KB |
Output is correct |
16 |
Correct |
608 ms |
13804 KB |
Output is correct |
17 |
Correct |
622 ms |
13132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
29600 KB |
Output is correct |
2 |
Correct |
353 ms |
28912 KB |
Output is correct |
3 |
Correct |
393 ms |
27696 KB |
Output is correct |
4 |
Correct |
389 ms |
27700 KB |
Output is correct |
5 |
Correct |
366 ms |
30340 KB |
Output is correct |
6 |
Correct |
426 ms |
29656 KB |
Output is correct |
7 |
Correct |
427 ms |
30720 KB |
Output is correct |
8 |
Correct |
377 ms |
29748 KB |
Output is correct |
9 |
Correct |
355 ms |
29484 KB |
Output is correct |
10 |
Correct |
418 ms |
29136 KB |
Output is correct |
11 |
Correct |
357 ms |
28284 KB |
Output is correct |
12 |
Correct |
402 ms |
31288 KB |
Output is correct |
13 |
Correct |
434 ms |
31360 KB |
Output is correct |
14 |
Correct |
431 ms |
30200 KB |
Output is correct |
15 |
Correct |
338 ms |
29028 KB |
Output is correct |
16 |
Correct |
388 ms |
28076 KB |
Output is correct |
17 |
Correct |
370 ms |
27940 KB |
Output is correct |
18 |
Correct |
409 ms |
29212 KB |
Output is correct |
19 |
Correct |
356 ms |
29644 KB |
Output is correct |
20 |
Correct |
466 ms |
30096 KB |
Output is correct |
21 |
Correct |
372 ms |
29388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4988 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
370 ms |
28528 KB |
Output is correct |
16 |
Correct |
386 ms |
29164 KB |
Output is correct |
17 |
Correct |
229 ms |
13168 KB |
Output is correct |
18 |
Correct |
997 ms |
12048 KB |
Output is correct |
19 |
Correct |
968 ms |
11832 KB |
Output is correct |
20 |
Correct |
898 ms |
11840 KB |
Output is correct |
21 |
Correct |
859 ms |
11900 KB |
Output is correct |
22 |
Correct |
961 ms |
12264 KB |
Output is correct |
23 |
Correct |
904 ms |
12072 KB |
Output is correct |
24 |
Correct |
951 ms |
12240 KB |
Output is correct |
25 |
Correct |
679 ms |
13456 KB |
Output is correct |
26 |
Correct |
591 ms |
13796 KB |
Output is correct |
27 |
Correct |
720 ms |
14596 KB |
Output is correct |
28 |
Correct |
574 ms |
13644 KB |
Output is correct |
29 |
Correct |
622 ms |
13144 KB |
Output is correct |
30 |
Correct |
608 ms |
13804 KB |
Output is correct |
31 |
Correct |
622 ms |
13132 KB |
Output is correct |
32 |
Correct |
363 ms |
29600 KB |
Output is correct |
33 |
Correct |
353 ms |
28912 KB |
Output is correct |
34 |
Correct |
393 ms |
27696 KB |
Output is correct |
35 |
Correct |
389 ms |
27700 KB |
Output is correct |
36 |
Correct |
366 ms |
30340 KB |
Output is correct |
37 |
Correct |
426 ms |
29656 KB |
Output is correct |
38 |
Correct |
427 ms |
30720 KB |
Output is correct |
39 |
Correct |
377 ms |
29748 KB |
Output is correct |
40 |
Correct |
355 ms |
29484 KB |
Output is correct |
41 |
Correct |
418 ms |
29136 KB |
Output is correct |
42 |
Correct |
357 ms |
28284 KB |
Output is correct |
43 |
Correct |
402 ms |
31288 KB |
Output is correct |
44 |
Correct |
434 ms |
31360 KB |
Output is correct |
45 |
Correct |
431 ms |
30200 KB |
Output is correct |
46 |
Correct |
338 ms |
29028 KB |
Output is correct |
47 |
Correct |
388 ms |
28076 KB |
Output is correct |
48 |
Correct |
370 ms |
27940 KB |
Output is correct |
49 |
Correct |
409 ms |
29212 KB |
Output is correct |
50 |
Correct |
356 ms |
29644 KB |
Output is correct |
51 |
Correct |
466 ms |
30096 KB |
Output is correct |
52 |
Correct |
372 ms |
29388 KB |
Output is correct |
53 |
Correct |
975 ms |
11992 KB |
Output is correct |
54 |
Correct |
1135 ms |
12208 KB |
Output is correct |
55 |
Correct |
1076 ms |
12508 KB |
Output is correct |
56 |
Correct |
1024 ms |
12396 KB |
Output is correct |
57 |
Correct |
1022 ms |
13052 KB |
Output is correct |
58 |
Correct |
1032 ms |
12052 KB |
Output is correct |
59 |
Correct |
1143 ms |
14296 KB |
Output is correct |
60 |
Correct |
1016 ms |
12548 KB |
Output is correct |
61 |
Correct |
866 ms |
11824 KB |
Output is correct |
62 |
Correct |
932 ms |
12744 KB |
Output is correct |
63 |
Correct |
990 ms |
12576 KB |
Output is correct |
64 |
Correct |
942 ms |
13996 KB |
Output is correct |
65 |
Correct |
959 ms |
12288 KB |
Output is correct |
66 |
Correct |
980 ms |
12944 KB |
Output is correct |
67 |
Correct |
906 ms |
11928 KB |
Output is correct |
68 |
Correct |
1101 ms |
14292 KB |
Output is correct |
69 |
Correct |
444 ms |
29108 KB |
Output is correct |
70 |
Correct |
331 ms |
27852 KB |
Output is correct |
71 |
Correct |
482 ms |
30836 KB |
Output is correct |
72 |
Correct |
162 ms |
12600 KB |
Output is correct |
73 |
Correct |
183 ms |
12844 KB |
Output is correct |
74 |
Correct |
213 ms |
13884 KB |
Output is correct |
75 |
Correct |
624 ms |
14056 KB |
Output is correct |
76 |
Correct |
745 ms |
14288 KB |
Output is correct |
77 |
Correct |
693 ms |
13892 KB |
Output is correct |
78 |
Correct |
683 ms |
14320 KB |
Output is correct |