// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 2e5 + 20;
const long long inf = 1e18;
vector<pair<int, int>> adj[ms];
bool f[ms];
long long mx[ms];
long long mi[ms];
void dfs(int u, int par, long long w, long long lim) {
mx[u] = -inf;
mi[u] = inf;
for (auto x: adj[u]) {
if (x.first != par) {
dfs(x.first, u, x.second, lim);
}
}
if (u != 1) {
if (max(mx[u], 0LL) + min(mi[u], w) <= lim) {
f[u] = false;
if (max(mx[u], 0LL) + mi[u] > lim) {
mx[par] = max(mx[par], max(mx[u], 0LL) + w);
}
mi[par] = min(mi[par], mi[u] + w);
}
else {
f[u] = true;
mi[par] = min(mi[par], w);
}
}
else {
if (max(mx[u], 0LL) + mi[u] <= lim) {
f[1] = false;
}
else {
f[1] = true;
}
}
}
void just_do_it() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
long long lt = 0;
long long rt = 2e14;
long long res = -1;
vector<int> best;
while (lt <= rt) {
long long mt = (lt + rt) / 2;
dfs(1, -1, -1, mt);
vector<int> cur;
for (int i = 1; i <= n; i++) {
if (f[i]) {
cur.push_back(i);
}
}
if ((int)cur.size() <= k) {
best = cur;
res = mt;
rt = mt - 1;
}
else {
lt = mt + 1;
}
}
for (int i = 1; i <= n; i++) {
f[i] = false;
}
for (auto x: best) {
f[x] = true;
}
for (int i = 1; i <= n; i++) {
if (!f[i] && (int)best.size() < k) {
best.push_back(i);
}
}
cout << res << '\n';
for (auto x: best) {
cout << x << " ";
}
}
// END MAIN CODE
Compilation message
Main.cpp: In function 'void file_io(std::string, std::string)':
Main.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 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 |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
31528 KB |
Output is correct |
2 |
Correct |
402 ms |
33600 KB |
Output is correct |
3 |
Correct |
343 ms |
20948 KB |
Output is correct |
4 |
Correct |
1205 ms |
19280 KB |
Output is correct |
5 |
Correct |
1157 ms |
18764 KB |
Output is correct |
6 |
Correct |
1167 ms |
18836 KB |
Output is correct |
7 |
Correct |
1150 ms |
17648 KB |
Output is correct |
8 |
Correct |
1180 ms |
18324 KB |
Output is correct |
9 |
Correct |
1277 ms |
18672 KB |
Output is correct |
10 |
Correct |
1239 ms |
19008 KB |
Output is correct |
11 |
Correct |
845 ms |
20512 KB |
Output is correct |
12 |
Correct |
791 ms |
20428 KB |
Output is correct |
13 |
Correct |
926 ms |
21868 KB |
Output is correct |
14 |
Correct |
808 ms |
19020 KB |
Output is correct |
15 |
Correct |
789 ms |
18636 KB |
Output is correct |
16 |
Correct |
809 ms |
19948 KB |
Output is correct |
17 |
Correct |
800 ms |
19252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
390 ms |
34108 KB |
Output is correct |
2 |
Correct |
352 ms |
33208 KB |
Output is correct |
3 |
Correct |
326 ms |
31828 KB |
Output is correct |
4 |
Correct |
328 ms |
31820 KB |
Output is correct |
5 |
Correct |
356 ms |
37376 KB |
Output is correct |
6 |
Correct |
386 ms |
34964 KB |
Output is correct |
7 |
Correct |
407 ms |
36248 KB |
Output is correct |
8 |
Correct |
364 ms |
33588 KB |
Output is correct |
9 |
Correct |
396 ms |
33356 KB |
Output is correct |
10 |
Correct |
346 ms |
32924 KB |
Output is correct |
11 |
Correct |
319 ms |
31692 KB |
Output is correct |
12 |
Correct |
414 ms |
36596 KB |
Output is correct |
13 |
Correct |
434 ms |
36144 KB |
Output is correct |
14 |
Correct |
409 ms |
35024 KB |
Output is correct |
15 |
Correct |
358 ms |
31952 KB |
Output is correct |
16 |
Correct |
335 ms |
31048 KB |
Output is correct |
17 |
Correct |
317 ms |
30708 KB |
Output is correct |
18 |
Correct |
350 ms |
32268 KB |
Output is correct |
19 |
Correct |
335 ms |
33176 KB |
Output is correct |
20 |
Correct |
364 ms |
34096 KB |
Output is correct |
21 |
Correct |
357 ms |
33428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
5032 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 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 |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
361 ms |
31528 KB |
Output is correct |
16 |
Correct |
402 ms |
33600 KB |
Output is correct |
17 |
Correct |
343 ms |
20948 KB |
Output is correct |
18 |
Correct |
1205 ms |
19280 KB |
Output is correct |
19 |
Correct |
1157 ms |
18764 KB |
Output is correct |
20 |
Correct |
1167 ms |
18836 KB |
Output is correct |
21 |
Correct |
1150 ms |
17648 KB |
Output is correct |
22 |
Correct |
1180 ms |
18324 KB |
Output is correct |
23 |
Correct |
1277 ms |
18672 KB |
Output is correct |
24 |
Correct |
1239 ms |
19008 KB |
Output is correct |
25 |
Correct |
845 ms |
20512 KB |
Output is correct |
26 |
Correct |
791 ms |
20428 KB |
Output is correct |
27 |
Correct |
926 ms |
21868 KB |
Output is correct |
28 |
Correct |
808 ms |
19020 KB |
Output is correct |
29 |
Correct |
789 ms |
18636 KB |
Output is correct |
30 |
Correct |
809 ms |
19948 KB |
Output is correct |
31 |
Correct |
800 ms |
19252 KB |
Output is correct |
32 |
Correct |
390 ms |
34108 KB |
Output is correct |
33 |
Correct |
352 ms |
33208 KB |
Output is correct |
34 |
Correct |
326 ms |
31828 KB |
Output is correct |
35 |
Correct |
328 ms |
31820 KB |
Output is correct |
36 |
Correct |
356 ms |
37376 KB |
Output is correct |
37 |
Correct |
386 ms |
34964 KB |
Output is correct |
38 |
Correct |
407 ms |
36248 KB |
Output is correct |
39 |
Correct |
364 ms |
33588 KB |
Output is correct |
40 |
Correct |
396 ms |
33356 KB |
Output is correct |
41 |
Correct |
346 ms |
32924 KB |
Output is correct |
42 |
Correct |
319 ms |
31692 KB |
Output is correct |
43 |
Correct |
414 ms |
36596 KB |
Output is correct |
44 |
Correct |
434 ms |
36144 KB |
Output is correct |
45 |
Correct |
409 ms |
35024 KB |
Output is correct |
46 |
Correct |
358 ms |
31952 KB |
Output is correct |
47 |
Correct |
335 ms |
31048 KB |
Output is correct |
48 |
Correct |
317 ms |
30708 KB |
Output is correct |
49 |
Correct |
350 ms |
32268 KB |
Output is correct |
50 |
Correct |
335 ms |
33176 KB |
Output is correct |
51 |
Correct |
364 ms |
34096 KB |
Output is correct |
52 |
Correct |
357 ms |
33428 KB |
Output is correct |
53 |
Correct |
1183 ms |
19176 KB |
Output is correct |
54 |
Correct |
1242 ms |
19656 KB |
Output is correct |
55 |
Correct |
1366 ms |
20220 KB |
Output is correct |
56 |
Correct |
1319 ms |
20024 KB |
Output is correct |
57 |
Correct |
1283 ms |
21296 KB |
Output is correct |
58 |
Correct |
1256 ms |
19224 KB |
Output is correct |
59 |
Correct |
1372 ms |
23812 KB |
Output is correct |
60 |
Correct |
1477 ms |
18752 KB |
Output is correct |
61 |
Correct |
1411 ms |
17512 KB |
Output is correct |
62 |
Correct |
1165 ms |
19020 KB |
Output is correct |
63 |
Correct |
1418 ms |
18812 KB |
Output is correct |
64 |
Correct |
1450 ms |
20696 KB |
Output is correct |
65 |
Correct |
1407 ms |
19124 KB |
Output is correct |
66 |
Correct |
1260 ms |
20772 KB |
Output is correct |
67 |
Correct |
1268 ms |
18304 KB |
Output is correct |
68 |
Correct |
1350 ms |
22792 KB |
Output is correct |
69 |
Correct |
376 ms |
33532 KB |
Output is correct |
70 |
Correct |
348 ms |
31340 KB |
Output is correct |
71 |
Correct |
452 ms |
35980 KB |
Output is correct |
72 |
Correct |
312 ms |
20080 KB |
Output is correct |
73 |
Correct |
384 ms |
19880 KB |
Output is correct |
74 |
Correct |
373 ms |
20992 KB |
Output is correct |
75 |
Correct |
803 ms |
20768 KB |
Output is correct |
76 |
Correct |
905 ms |
20188 KB |
Output is correct |
77 |
Correct |
815 ms |
20144 KB |
Output is correct |
78 |
Correct |
867 ms |
20220 KB |
Output is correct |