#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 != 1) 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(1,0)>0 || flag[1]==0) marked[1]=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;
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(1,0)>0 || flag[1]==0) marked[1]=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 << ' ';
}
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:95:28: 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]
95 | foru(i,1,n) if (res.size()<k && marked[i]==0) res.pb(i);
| ~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8284 KB |
Output is correct |
8 |
Correct |
5 ms |
8024 KB |
Output is correct |
9 |
Correct |
6 ms |
8028 KB |
Output is correct |
10 |
Correct |
5 ms |
8284 KB |
Output is correct |
11 |
Correct |
5 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8284 KB |
Output is correct |
13 |
Correct |
5 ms |
8028 KB |
Output is correct |
14 |
Correct |
5 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
31576 KB |
Output is correct |
2 |
Correct |
328 ms |
32336 KB |
Output is correct |
3 |
Correct |
131 ms |
17588 KB |
Output is correct |
4 |
Correct |
645 ms |
17868 KB |
Output is correct |
5 |
Correct |
524 ms |
17564 KB |
Output is correct |
6 |
Correct |
533 ms |
17748 KB |
Output is correct |
7 |
Correct |
539 ms |
17748 KB |
Output is correct |
8 |
Correct |
565 ms |
18016 KB |
Output is correct |
9 |
Correct |
573 ms |
17740 KB |
Output is correct |
10 |
Correct |
608 ms |
18088 KB |
Output is correct |
11 |
Correct |
514 ms |
19028 KB |
Output is correct |
12 |
Correct |
515 ms |
19288 KB |
Output is correct |
13 |
Correct |
603 ms |
20320 KB |
Output is correct |
14 |
Correct |
485 ms |
19148 KB |
Output is correct |
15 |
Correct |
508 ms |
18756 KB |
Output is correct |
16 |
Correct |
524 ms |
19380 KB |
Output is correct |
17 |
Correct |
513 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
32596 KB |
Output is correct |
2 |
Correct |
299 ms |
32120 KB |
Output is correct |
3 |
Correct |
278 ms |
30796 KB |
Output is correct |
4 |
Correct |
297 ms |
30668 KB |
Output is correct |
5 |
Correct |
334 ms |
34500 KB |
Output is correct |
6 |
Correct |
329 ms |
33512 KB |
Output is correct |
7 |
Correct |
362 ms |
34376 KB |
Output is correct |
8 |
Correct |
295 ms |
32852 KB |
Output is correct |
9 |
Correct |
322 ms |
32792 KB |
Output is correct |
10 |
Correct |
301 ms |
32252 KB |
Output is correct |
11 |
Correct |
285 ms |
31324 KB |
Output is correct |
12 |
Correct |
354 ms |
35276 KB |
Output is correct |
13 |
Correct |
372 ms |
35280 KB |
Output is correct |
14 |
Correct |
388 ms |
34000 KB |
Output is correct |
15 |
Correct |
309 ms |
32240 KB |
Output is correct |
16 |
Correct |
344 ms |
31316 KB |
Output is correct |
17 |
Correct |
279 ms |
31060 KB |
Output is correct |
18 |
Correct |
364 ms |
32356 KB |
Output is correct |
19 |
Correct |
360 ms |
33688 KB |
Output is correct |
20 |
Correct |
365 ms |
33992 KB |
Output is correct |
21 |
Correct |
313 ms |
33204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8284 KB |
Output is correct |
5 |
Correct |
5 ms |
8028 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8284 KB |
Output is correct |
8 |
Correct |
5 ms |
8024 KB |
Output is correct |
9 |
Correct |
6 ms |
8028 KB |
Output is correct |
10 |
Correct |
5 ms |
8284 KB |
Output is correct |
11 |
Correct |
5 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8284 KB |
Output is correct |
13 |
Correct |
5 ms |
8028 KB |
Output is correct |
14 |
Correct |
5 ms |
8284 KB |
Output is correct |
15 |
Correct |
324 ms |
31576 KB |
Output is correct |
16 |
Correct |
328 ms |
32336 KB |
Output is correct |
17 |
Correct |
131 ms |
17588 KB |
Output is correct |
18 |
Correct |
645 ms |
17868 KB |
Output is correct |
19 |
Correct |
524 ms |
17564 KB |
Output is correct |
20 |
Correct |
533 ms |
17748 KB |
Output is correct |
21 |
Correct |
539 ms |
17748 KB |
Output is correct |
22 |
Correct |
565 ms |
18016 KB |
Output is correct |
23 |
Correct |
573 ms |
17740 KB |
Output is correct |
24 |
Correct |
608 ms |
18088 KB |
Output is correct |
25 |
Correct |
514 ms |
19028 KB |
Output is correct |
26 |
Correct |
515 ms |
19288 KB |
Output is correct |
27 |
Correct |
603 ms |
20320 KB |
Output is correct |
28 |
Correct |
485 ms |
19148 KB |
Output is correct |
29 |
Correct |
508 ms |
18756 KB |
Output is correct |
30 |
Correct |
524 ms |
19380 KB |
Output is correct |
31 |
Correct |
513 ms |
18772 KB |
Output is correct |
32 |
Correct |
332 ms |
32596 KB |
Output is correct |
33 |
Correct |
299 ms |
32120 KB |
Output is correct |
34 |
Correct |
278 ms |
30796 KB |
Output is correct |
35 |
Correct |
297 ms |
30668 KB |
Output is correct |
36 |
Correct |
334 ms |
34500 KB |
Output is correct |
37 |
Correct |
329 ms |
33512 KB |
Output is correct |
38 |
Correct |
362 ms |
34376 KB |
Output is correct |
39 |
Correct |
295 ms |
32852 KB |
Output is correct |
40 |
Correct |
322 ms |
32792 KB |
Output is correct |
41 |
Correct |
301 ms |
32252 KB |
Output is correct |
42 |
Correct |
285 ms |
31324 KB |
Output is correct |
43 |
Correct |
354 ms |
35276 KB |
Output is correct |
44 |
Correct |
372 ms |
35280 KB |
Output is correct |
45 |
Correct |
388 ms |
34000 KB |
Output is correct |
46 |
Correct |
309 ms |
32240 KB |
Output is correct |
47 |
Correct |
344 ms |
31316 KB |
Output is correct |
48 |
Correct |
279 ms |
31060 KB |
Output is correct |
49 |
Correct |
364 ms |
32356 KB |
Output is correct |
50 |
Correct |
360 ms |
33688 KB |
Output is correct |
51 |
Correct |
365 ms |
33992 KB |
Output is correct |
52 |
Correct |
313 ms |
33204 KB |
Output is correct |
53 |
Correct |
607 ms |
17612 KB |
Output is correct |
54 |
Correct |
633 ms |
18000 KB |
Output is correct |
55 |
Correct |
662 ms |
18492 KB |
Output is correct |
56 |
Correct |
711 ms |
18484 KB |
Output is correct |
57 |
Correct |
744 ms |
19396 KB |
Output is correct |
58 |
Correct |
618 ms |
17744 KB |
Output is correct |
59 |
Correct |
689 ms |
21184 KB |
Output is correct |
60 |
Correct |
646 ms |
18512 KB |
Output is correct |
61 |
Correct |
505 ms |
17492 KB |
Output is correct |
62 |
Correct |
512 ms |
18892 KB |
Output is correct |
63 |
Correct |
632 ms |
18516 KB |
Output is correct |
64 |
Correct |
564 ms |
20672 KB |
Output is correct |
65 |
Correct |
603 ms |
18260 KB |
Output is correct |
66 |
Correct |
631 ms |
19396 KB |
Output is correct |
67 |
Correct |
651 ms |
17632 KB |
Output is correct |
68 |
Correct |
764 ms |
21244 KB |
Output is correct |
69 |
Correct |
321 ms |
32320 KB |
Output is correct |
70 |
Correct |
318 ms |
31060 KB |
Output is correct |
71 |
Correct |
360 ms |
34448 KB |
Output is correct |
72 |
Correct |
121 ms |
17772 KB |
Output is correct |
73 |
Correct |
154 ms |
17484 KB |
Output is correct |
74 |
Correct |
143 ms |
20124 KB |
Output is correct |
75 |
Correct |
537 ms |
19592 KB |
Output is correct |
76 |
Correct |
627 ms |
20100 KB |
Output is correct |
77 |
Correct |
540 ms |
19508 KB |
Output is correct |
78 |
Correct |
598 ms |
20204 KB |
Output is correct |