#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,0,n-1) if (marked[i]==1) res.pb(i+1);
foru(i,0,n-1) if (res.size()<k && marked[i]==0) res.pb(i+1);
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:96:30: 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]
96 | foru(i,0,n-1) if (res.size()<k && marked[i]==0) res.pb(i+1);
| ~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8280 KB |
Output is correct |
2 |
Correct |
6 ms |
8028 KB |
Output is correct |
3 |
Correct |
6 ms |
8028 KB |
Output is correct |
4 |
Correct |
5 ms |
8272 KB |
Output is correct |
5 |
Correct |
6 ms |
8024 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8280 KB |
Output is correct |
8 |
Correct |
5 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8276 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 |
8028 KB |
Output is correct |
13 |
Correct |
5 ms |
8280 KB |
Output is correct |
14 |
Correct |
5 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
31720 KB |
Output is correct |
2 |
Correct |
400 ms |
32372 KB |
Output is correct |
3 |
Correct |
139 ms |
17628 KB |
Output is correct |
4 |
Correct |
787 ms |
17784 KB |
Output is correct |
5 |
Correct |
756 ms |
17488 KB |
Output is correct |
6 |
Correct |
693 ms |
17576 KB |
Output is correct |
7 |
Correct |
690 ms |
17644 KB |
Output is correct |
8 |
Correct |
793 ms |
17940 KB |
Output is correct |
9 |
Correct |
838 ms |
17888 KB |
Output is correct |
10 |
Correct |
791 ms |
18256 KB |
Output is correct |
11 |
Correct |
631 ms |
19172 KB |
Output is correct |
12 |
Correct |
600 ms |
19288 KB |
Output is correct |
13 |
Correct |
779 ms |
20316 KB |
Output is correct |
14 |
Correct |
655 ms |
19152 KB |
Output is correct |
15 |
Correct |
584 ms |
18772 KB |
Output is correct |
16 |
Correct |
630 ms |
19268 KB |
Output is correct |
17 |
Correct |
513 ms |
18736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
380 ms |
32664 KB |
Output is correct |
2 |
Correct |
354 ms |
32340 KB |
Output is correct |
3 |
Correct |
341 ms |
30908 KB |
Output is correct |
4 |
Correct |
319 ms |
30852 KB |
Output is correct |
5 |
Correct |
412 ms |
34356 KB |
Output is correct |
6 |
Correct |
350 ms |
33232 KB |
Output is correct |
7 |
Correct |
366 ms |
34384 KB |
Output is correct |
8 |
Correct |
343 ms |
32864 KB |
Output is correct |
9 |
Correct |
375 ms |
32848 KB |
Output is correct |
10 |
Correct |
323 ms |
32248 KB |
Output is correct |
11 |
Correct |
322 ms |
31312 KB |
Output is correct |
12 |
Correct |
386 ms |
35364 KB |
Output is correct |
13 |
Correct |
398 ms |
35124 KB |
Output is correct |
14 |
Correct |
342 ms |
33856 KB |
Output is correct |
15 |
Correct |
318 ms |
32340 KB |
Output is correct |
16 |
Correct |
337 ms |
31236 KB |
Output is correct |
17 |
Correct |
311 ms |
31148 KB |
Output is correct |
18 |
Correct |
339 ms |
32424 KB |
Output is correct |
19 |
Correct |
304 ms |
33576 KB |
Output is correct |
20 |
Correct |
345 ms |
34064 KB |
Output is correct |
21 |
Correct |
370 ms |
32936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8280 KB |
Output is correct |
2 |
Correct |
6 ms |
8028 KB |
Output is correct |
3 |
Correct |
6 ms |
8028 KB |
Output is correct |
4 |
Correct |
5 ms |
8272 KB |
Output is correct |
5 |
Correct |
6 ms |
8024 KB |
Output is correct |
6 |
Correct |
5 ms |
8284 KB |
Output is correct |
7 |
Correct |
5 ms |
8280 KB |
Output is correct |
8 |
Correct |
5 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8276 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 |
8028 KB |
Output is correct |
13 |
Correct |
5 ms |
8280 KB |
Output is correct |
14 |
Correct |
5 ms |
8280 KB |
Output is correct |
15 |
Correct |
356 ms |
31720 KB |
Output is correct |
16 |
Correct |
400 ms |
32372 KB |
Output is correct |
17 |
Correct |
139 ms |
17628 KB |
Output is correct |
18 |
Correct |
787 ms |
17784 KB |
Output is correct |
19 |
Correct |
756 ms |
17488 KB |
Output is correct |
20 |
Correct |
693 ms |
17576 KB |
Output is correct |
21 |
Correct |
690 ms |
17644 KB |
Output is correct |
22 |
Correct |
793 ms |
17940 KB |
Output is correct |
23 |
Correct |
838 ms |
17888 KB |
Output is correct |
24 |
Correct |
791 ms |
18256 KB |
Output is correct |
25 |
Correct |
631 ms |
19172 KB |
Output is correct |
26 |
Correct |
600 ms |
19288 KB |
Output is correct |
27 |
Correct |
779 ms |
20316 KB |
Output is correct |
28 |
Correct |
655 ms |
19152 KB |
Output is correct |
29 |
Correct |
584 ms |
18772 KB |
Output is correct |
30 |
Correct |
630 ms |
19268 KB |
Output is correct |
31 |
Correct |
513 ms |
18736 KB |
Output is correct |
32 |
Correct |
380 ms |
32664 KB |
Output is correct |
33 |
Correct |
354 ms |
32340 KB |
Output is correct |
34 |
Correct |
341 ms |
30908 KB |
Output is correct |
35 |
Correct |
319 ms |
30852 KB |
Output is correct |
36 |
Correct |
412 ms |
34356 KB |
Output is correct |
37 |
Correct |
350 ms |
33232 KB |
Output is correct |
38 |
Correct |
366 ms |
34384 KB |
Output is correct |
39 |
Correct |
343 ms |
32864 KB |
Output is correct |
40 |
Correct |
375 ms |
32848 KB |
Output is correct |
41 |
Correct |
323 ms |
32248 KB |
Output is correct |
42 |
Correct |
322 ms |
31312 KB |
Output is correct |
43 |
Correct |
386 ms |
35364 KB |
Output is correct |
44 |
Correct |
398 ms |
35124 KB |
Output is correct |
45 |
Correct |
342 ms |
33856 KB |
Output is correct |
46 |
Correct |
318 ms |
32340 KB |
Output is correct |
47 |
Correct |
337 ms |
31236 KB |
Output is correct |
48 |
Correct |
311 ms |
31148 KB |
Output is correct |
49 |
Correct |
339 ms |
32424 KB |
Output is correct |
50 |
Correct |
304 ms |
33576 KB |
Output is correct |
51 |
Correct |
345 ms |
34064 KB |
Output is correct |
52 |
Correct |
370 ms |
32936 KB |
Output is correct |
53 |
Correct |
901 ms |
17792 KB |
Output is correct |
54 |
Correct |
832 ms |
18080 KB |
Output is correct |
55 |
Correct |
909 ms |
18492 KB |
Output is correct |
56 |
Correct |
960 ms |
18516 KB |
Output is correct |
57 |
Correct |
835 ms |
19320 KB |
Output is correct |
58 |
Correct |
769 ms |
17880 KB |
Output is correct |
59 |
Correct |
841 ms |
21172 KB |
Output is correct |
60 |
Correct |
889 ms |
18476 KB |
Output is correct |
61 |
Correct |
737 ms |
17488 KB |
Output is correct |
62 |
Correct |
771 ms |
18892 KB |
Output is correct |
63 |
Correct |
766 ms |
18512 KB |
Output is correct |
64 |
Correct |
734 ms |
20672 KB |
Output is correct |
65 |
Correct |
792 ms |
18004 KB |
Output is correct |
66 |
Correct |
661 ms |
19144 KB |
Output is correct |
67 |
Correct |
598 ms |
17856 KB |
Output is correct |
68 |
Correct |
883 ms |
21248 KB |
Output is correct |
69 |
Correct |
328 ms |
32084 KB |
Output is correct |
70 |
Correct |
356 ms |
31316 KB |
Output is correct |
71 |
Correct |
372 ms |
34252 KB |
Output is correct |
72 |
Correct |
122 ms |
16876 KB |
Output is correct |
73 |
Correct |
131 ms |
17464 KB |
Output is correct |
74 |
Correct |
153 ms |
20804 KB |
Output is correct |
75 |
Correct |
525 ms |
19536 KB |
Output is correct |
76 |
Correct |
693 ms |
19976 KB |
Output is correct |
77 |
Correct |
587 ms |
19284 KB |
Output is correct |
78 |
Correct |
633 ms |
20356 KB |
Output is correct |