#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5;
const int oo = 1e18, mod = 1e9 + 7;
int n, m, k, mid, uk, flag[N], marked[N];
vector<ii> adj[N];
vector<int> res;
int dfs(int x, int par) {
if (adj[x].size() == 1 && x != 0) return 0;
int mi = (1ll << 60), ma = -(1ll << 60);
for (auto tr : adj[x]) {
int y = tr.fi;
int d = tr.se;
if (y == par) continue;
int a = dfs(y, x);
if (a == 0 && flag[y] == 1) {
mi = min(mi, 0ll);
ma = max(ma, 0ll);
continue;
}
if (a < 0 && a + d <= 0) flag[x] = 1;
if (a < 0 && a + d > 0) a = 0;
else a += d;
if (a > mid) {
uk++;
marked[y] = 1;
a = min(-mid + d, 0ll);
if (-mid + d <= 0) flag[x] = 1;
flag[y] = 1;
}
ma = max(ma, a);
mi = min(mi, a);
}
if (-mi >= ma) {
return mi;
}
return ma;
}
int binary_search(int lo, int hi) {
while (lo < hi) {
mid = lo + (hi-lo)/2;
uk=0;
memset(flag, 0, sizeof(flag));
memset(marked, 0, sizeof(marked));
if (dfs(0,0)>0 || flag[0]==0) marked[0]=1, uk++;
if (uk<=k)
hi = mid;
else
lo = mid+1;
}
return lo;
}
void process() {
res.clear();
cin >> n >> k;
foru(i,1,n-1) {
int u, v, w; cin >> u >> v >> w;
u--, v--;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
/*mid=binary_search(0, oo);
uk=0;
cout << mid << '\n';
memset(flag, 0, sizeof(flag));
memset(marked, 0, sizeof(marked));
if (dfs(0,0)>0 || flag[0]==0) marked[0]=1, uk++;
foru(i,1,n) if (marked[i]==1) res.pb(i);
foru(i,1,n) if (res.size()<k && marked[i]==0) res.pb(i);
for (auto x: res) cout << x << ' ';*/
int lo = 0, hi = (1LL << 60);
while (lo < hi) {
mid = (lo + hi) / 2;
uk = 0;
memset(flag, 0, sizeof flag);
memset(marked, 0, sizeof marked);
if (dfs(0, 0) > 0) marked[0] = 1, uk++;
else if (flag[0] == 0) marked[0] = 1, uk++;
//cout << endl << uk << endl;
//cout << mid << " " << uk << endl;
if (uk <= k) hi = mid;
else lo = mid + 1;
}
mid = lo;
uk = 0;
memset(flag, 0, sizeof flag);
memset(marked, 0, sizeof marked);
if (dfs(0, 0) > 0) marked[0] = 1, uk++;
else if (flag[0] == 0) marked[0] = 1, uk++;
cout << lo << "\n";
vector <int> out;
foru(i,0,n-1) if (marked[i] == 1) out.push_back(i + 1);
foru(i,0,n-1) if (out.size() < k && marked[i] == 0) out.push_back(i + 1);
for (int x : out) cout << x << " "; cout << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
Compilation message
Main.cpp: In function 'void process()':
Main.cpp:125:34: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
125 | foru(i,0,n-1) if (out.size() < k && marked[i] == 0) out.push_back(i + 1);
| ~~~~~~~~~~~^~~
Main.cpp:126:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
126 | for (int x : out) cout << x << " "; cout << "\n";
| ^~~
Main.cpp:126:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
126 | for (int x : out) cout << x << " "; cout << "\n";
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8280 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8280 KB |
Output is correct |
4 |
Correct |
6 ms |
8284 KB |
Output is correct |
5 |
Correct |
5 ms |
8284 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8044 KB |
Output is correct |
8 |
Correct |
6 ms |
8296 KB |
Output is correct |
9 |
Correct |
5 ms |
8292 KB |
Output is correct |
10 |
Correct |
5 ms |
8292 KB |
Output is correct |
11 |
Correct |
5 ms |
8292 KB |
Output is correct |
12 |
Correct |
5 ms |
8292 KB |
Output is correct |
13 |
Correct |
6 ms |
8284 KB |
Output is correct |
14 |
Correct |
6 ms |
8292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
31732 KB |
Output is correct |
2 |
Correct |
338 ms |
32384 KB |
Output is correct |
3 |
Correct |
139 ms |
17532 KB |
Output is correct |
4 |
Correct |
589 ms |
17756 KB |
Output is correct |
5 |
Correct |
565 ms |
17564 KB |
Output is correct |
6 |
Correct |
661 ms |
17576 KB |
Output is correct |
7 |
Correct |
629 ms |
17488 KB |
Output is correct |
8 |
Correct |
704 ms |
18004 KB |
Output is correct |
9 |
Correct |
661 ms |
17884 KB |
Output is correct |
10 |
Correct |
648 ms |
18004 KB |
Output is correct |
11 |
Correct |
612 ms |
19032 KB |
Output is correct |
12 |
Correct |
599 ms |
19296 KB |
Output is correct |
13 |
Correct |
607 ms |
20324 KB |
Output is correct |
14 |
Correct |
613 ms |
19152 KB |
Output is correct |
15 |
Correct |
566 ms |
18800 KB |
Output is correct |
16 |
Correct |
604 ms |
19380 KB |
Output is correct |
17 |
Correct |
517 ms |
18512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
33104 KB |
Output is correct |
2 |
Correct |
317 ms |
32084 KB |
Output is correct |
3 |
Correct |
373 ms |
30804 KB |
Output is correct |
4 |
Correct |
319 ms |
30848 KB |
Output is correct |
5 |
Correct |
321 ms |
34296 KB |
Output is correct |
6 |
Correct |
443 ms |
37840 KB |
Output is correct |
7 |
Correct |
362 ms |
38856 KB |
Output is correct |
8 |
Correct |
365 ms |
36688 KB |
Output is correct |
9 |
Correct |
320 ms |
36296 KB |
Output is correct |
10 |
Correct |
306 ms |
35664 KB |
Output is correct |
11 |
Correct |
287 ms |
34640 KB |
Output is correct |
12 |
Correct |
361 ms |
39036 KB |
Output is correct |
13 |
Correct |
366 ms |
38856 KB |
Output is correct |
14 |
Correct |
378 ms |
37508 KB |
Output is correct |
15 |
Correct |
292 ms |
34924 KB |
Output is correct |
16 |
Correct |
285 ms |
34004 KB |
Output is correct |
17 |
Correct |
280 ms |
33876 KB |
Output is correct |
18 |
Correct |
320 ms |
35152 KB |
Output is correct |
19 |
Correct |
322 ms |
36432 KB |
Output is correct |
20 |
Correct |
322 ms |
36808 KB |
Output is correct |
21 |
Correct |
400 ms |
35776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8280 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8280 KB |
Output is correct |
4 |
Correct |
6 ms |
8284 KB |
Output is correct |
5 |
Correct |
5 ms |
8284 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8044 KB |
Output is correct |
8 |
Correct |
6 ms |
8296 KB |
Output is correct |
9 |
Correct |
5 ms |
8292 KB |
Output is correct |
10 |
Correct |
5 ms |
8292 KB |
Output is correct |
11 |
Correct |
5 ms |
8292 KB |
Output is correct |
12 |
Correct |
5 ms |
8292 KB |
Output is correct |
13 |
Correct |
6 ms |
8284 KB |
Output is correct |
14 |
Correct |
6 ms |
8292 KB |
Output is correct |
15 |
Correct |
330 ms |
31732 KB |
Output is correct |
16 |
Correct |
338 ms |
32384 KB |
Output is correct |
17 |
Correct |
139 ms |
17532 KB |
Output is correct |
18 |
Correct |
589 ms |
17756 KB |
Output is correct |
19 |
Correct |
565 ms |
17564 KB |
Output is correct |
20 |
Correct |
661 ms |
17576 KB |
Output is correct |
21 |
Correct |
629 ms |
17488 KB |
Output is correct |
22 |
Correct |
704 ms |
18004 KB |
Output is correct |
23 |
Correct |
661 ms |
17884 KB |
Output is correct |
24 |
Correct |
648 ms |
18004 KB |
Output is correct |
25 |
Correct |
612 ms |
19032 KB |
Output is correct |
26 |
Correct |
599 ms |
19296 KB |
Output is correct |
27 |
Correct |
607 ms |
20324 KB |
Output is correct |
28 |
Correct |
613 ms |
19152 KB |
Output is correct |
29 |
Correct |
566 ms |
18800 KB |
Output is correct |
30 |
Correct |
604 ms |
19380 KB |
Output is correct |
31 |
Correct |
517 ms |
18512 KB |
Output is correct |
32 |
Correct |
367 ms |
33104 KB |
Output is correct |
33 |
Correct |
317 ms |
32084 KB |
Output is correct |
34 |
Correct |
373 ms |
30804 KB |
Output is correct |
35 |
Correct |
319 ms |
30848 KB |
Output is correct |
36 |
Correct |
321 ms |
34296 KB |
Output is correct |
37 |
Correct |
443 ms |
37840 KB |
Output is correct |
38 |
Correct |
362 ms |
38856 KB |
Output is correct |
39 |
Correct |
365 ms |
36688 KB |
Output is correct |
40 |
Correct |
320 ms |
36296 KB |
Output is correct |
41 |
Correct |
306 ms |
35664 KB |
Output is correct |
42 |
Correct |
287 ms |
34640 KB |
Output is correct |
43 |
Correct |
361 ms |
39036 KB |
Output is correct |
44 |
Correct |
366 ms |
38856 KB |
Output is correct |
45 |
Correct |
378 ms |
37508 KB |
Output is correct |
46 |
Correct |
292 ms |
34924 KB |
Output is correct |
47 |
Correct |
285 ms |
34004 KB |
Output is correct |
48 |
Correct |
280 ms |
33876 KB |
Output is correct |
49 |
Correct |
320 ms |
35152 KB |
Output is correct |
50 |
Correct |
322 ms |
36432 KB |
Output is correct |
51 |
Correct |
322 ms |
36808 KB |
Output is correct |
52 |
Correct |
400 ms |
35776 KB |
Output is correct |
53 |
Correct |
754 ms |
21908 KB |
Output is correct |
54 |
Correct |
682 ms |
22324 KB |
Output is correct |
55 |
Correct |
755 ms |
22864 KB |
Output is correct |
56 |
Correct |
752 ms |
22796 KB |
Output is correct |
57 |
Correct |
876 ms |
23772 KB |
Output is correct |
58 |
Correct |
688 ms |
22100 KB |
Output is correct |
59 |
Correct |
811 ms |
25384 KB |
Output is correct |
60 |
Correct |
829 ms |
21468 KB |
Output is correct |
61 |
Correct |
613 ms |
20268 KB |
Output is correct |
62 |
Correct |
724 ms |
21620 KB |
Output is correct |
63 |
Correct |
742 ms |
21524 KB |
Output is correct |
64 |
Correct |
904 ms |
23348 KB |
Output is correct |
65 |
Correct |
816 ms |
21836 KB |
Output is correct |
66 |
Correct |
1030 ms |
22728 KB |
Output is correct |
67 |
Correct |
644 ms |
21060 KB |
Output is correct |
68 |
Correct |
903 ms |
24772 KB |
Output is correct |
69 |
Correct |
316 ms |
36688 KB |
Output is correct |
70 |
Correct |
292 ms |
34504 KB |
Output is correct |
71 |
Correct |
368 ms |
38092 KB |
Output is correct |
72 |
Correct |
125 ms |
21248 KB |
Output is correct |
73 |
Correct |
123 ms |
20412 KB |
Output is correct |
74 |
Correct |
138 ms |
22908 KB |
Output is correct |
75 |
Correct |
561 ms |
23764 KB |
Output is correct |
76 |
Correct |
670 ms |
23060 KB |
Output is correct |
77 |
Correct |
697 ms |
23040 KB |
Output is correct |
78 |
Correct |
567 ms |
23032 KB |
Output is correct |