#include <bits/stdc++.h>
using namespace std;
long long N,K, dp1[200005], dp2[200005], val, inf = 1e18;
vector< pair<long long,long long> > lst[200005];
bitset<200005> ans;
void dfs (long long x, long long p) {
dp1[x] = inf, dp2[x] = 0;
for (auto v: lst[x]) if (v.first != p) {
dfs(v.first,x);
dp1[x] = min(dp1[x],dp1[v.first] + v.second);
if (dp2[v.first] != -1) {
if (dp2[v.first] + v.second > val) ans[v.first] = 1, dp1[x] = min(dp1[x],v.second);
else dp2[x] = max(dp2[x],dp2[v.first] + v.second);
}
}
if (dp1[x]+dp2[x] <= val) dp2[x] = -1;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> K;
for (long long i = 0; i < N-1; ++i) {
long long u,v,w; cin >> u >> v >> w; u--, v--;
lst[u].emplace_back(v,w), lst[v].emplace_back(u,w);
}
long long low = -1, high = 2e14;
while (high - low > 1) {
val = (low+high)/2;
ans.reset();
dfs(0,-1);
if (dp2[0] != -1) ans[0] = 1;
if (ans.count() > K) low = val;
else high = val;
}
cout << high << '\n';
val = high;
ans.reset();
dfs(0,-1);
if (dp2[0] != -1) ans[0] = 1;
for (long long i = 0, num = ans.count(); i < N && num < K; ++i) if (!ans[i]) ans[i] = 1, num++;
for (long long i = 0; i < N; ++i) if (ans[i]) cout << i+1 << ' ';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:34:25: warning: comparison of integer expressions of different signedness: 'std::size_t' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
34 | if (ans.count() > K) low = val;
| ~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
31564 KB |
Output is correct |
2 |
Correct |
309 ms |
32284 KB |
Output is correct |
3 |
Correct |
317 ms |
17480 KB |
Output is correct |
4 |
Correct |
1305 ms |
17776 KB |
Output is correct |
5 |
Correct |
1226 ms |
20840 KB |
Output is correct |
6 |
Correct |
1219 ms |
20932 KB |
Output is correct |
7 |
Correct |
1304 ms |
19860 KB |
Output is correct |
8 |
Correct |
1310 ms |
19228 KB |
Output is correct |
9 |
Correct |
1234 ms |
17588 KB |
Output is correct |
10 |
Correct |
1302 ms |
21244 KB |
Output is correct |
11 |
Correct |
882 ms |
22300 KB |
Output is correct |
12 |
Correct |
778 ms |
22452 KB |
Output is correct |
13 |
Correct |
922 ms |
24132 KB |
Output is correct |
14 |
Correct |
863 ms |
21276 KB |
Output is correct |
15 |
Correct |
851 ms |
20804 KB |
Output is correct |
16 |
Correct |
849 ms |
22264 KB |
Output is correct |
17 |
Correct |
834 ms |
21520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
32840 KB |
Output is correct |
2 |
Correct |
314 ms |
31996 KB |
Output is correct |
3 |
Correct |
297 ms |
30788 KB |
Output is correct |
4 |
Correct |
302 ms |
30564 KB |
Output is correct |
5 |
Correct |
376 ms |
32720 KB |
Output is correct |
6 |
Correct |
447 ms |
32336 KB |
Output is correct |
7 |
Correct |
434 ms |
33432 KB |
Output is correct |
8 |
Correct |
376 ms |
33324 KB |
Output is correct |
9 |
Correct |
310 ms |
33208 KB |
Output is correct |
10 |
Correct |
336 ms |
32576 KB |
Output is correct |
11 |
Correct |
352 ms |
31000 KB |
Output is correct |
12 |
Correct |
401 ms |
33576 KB |
Output is correct |
13 |
Correct |
432 ms |
33708 KB |
Output is correct |
14 |
Correct |
396 ms |
32968 KB |
Output is correct |
15 |
Correct |
319 ms |
31900 KB |
Output is correct |
16 |
Correct |
320 ms |
30884 KB |
Output is correct |
17 |
Correct |
314 ms |
30800 KB |
Output is correct |
18 |
Correct |
337 ms |
32196 KB |
Output is correct |
19 |
Correct |
330 ms |
31792 KB |
Output is correct |
20 |
Correct |
353 ms |
32516 KB |
Output is correct |
21 |
Correct |
304 ms |
31832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5028 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
303 ms |
31564 KB |
Output is correct |
16 |
Correct |
309 ms |
32284 KB |
Output is correct |
17 |
Correct |
317 ms |
17480 KB |
Output is correct |
18 |
Correct |
1305 ms |
17776 KB |
Output is correct |
19 |
Correct |
1226 ms |
20840 KB |
Output is correct |
20 |
Correct |
1219 ms |
20932 KB |
Output is correct |
21 |
Correct |
1304 ms |
19860 KB |
Output is correct |
22 |
Correct |
1310 ms |
19228 KB |
Output is correct |
23 |
Correct |
1234 ms |
17588 KB |
Output is correct |
24 |
Correct |
1302 ms |
21244 KB |
Output is correct |
25 |
Correct |
882 ms |
22300 KB |
Output is correct |
26 |
Correct |
778 ms |
22452 KB |
Output is correct |
27 |
Correct |
922 ms |
24132 KB |
Output is correct |
28 |
Correct |
863 ms |
21276 KB |
Output is correct |
29 |
Correct |
851 ms |
20804 KB |
Output is correct |
30 |
Correct |
849 ms |
22264 KB |
Output is correct |
31 |
Correct |
834 ms |
21520 KB |
Output is correct |
32 |
Correct |
343 ms |
32840 KB |
Output is correct |
33 |
Correct |
314 ms |
31996 KB |
Output is correct |
34 |
Correct |
297 ms |
30788 KB |
Output is correct |
35 |
Correct |
302 ms |
30564 KB |
Output is correct |
36 |
Correct |
376 ms |
32720 KB |
Output is correct |
37 |
Correct |
447 ms |
32336 KB |
Output is correct |
38 |
Correct |
434 ms |
33432 KB |
Output is correct |
39 |
Correct |
376 ms |
33324 KB |
Output is correct |
40 |
Correct |
310 ms |
33208 KB |
Output is correct |
41 |
Correct |
336 ms |
32576 KB |
Output is correct |
42 |
Correct |
352 ms |
31000 KB |
Output is correct |
43 |
Correct |
401 ms |
33576 KB |
Output is correct |
44 |
Correct |
432 ms |
33708 KB |
Output is correct |
45 |
Correct |
396 ms |
32968 KB |
Output is correct |
46 |
Correct |
319 ms |
31900 KB |
Output is correct |
47 |
Correct |
320 ms |
30884 KB |
Output is correct |
48 |
Correct |
314 ms |
30800 KB |
Output is correct |
49 |
Correct |
337 ms |
32196 KB |
Output is correct |
50 |
Correct |
330 ms |
31792 KB |
Output is correct |
51 |
Correct |
353 ms |
32516 KB |
Output is correct |
52 |
Correct |
304 ms |
31832 KB |
Output is correct |
53 |
Correct |
1406 ms |
17456 KB |
Output is correct |
54 |
Correct |
1409 ms |
17832 KB |
Output is correct |
55 |
Correct |
1490 ms |
18372 KB |
Output is correct |
56 |
Correct |
1367 ms |
18020 KB |
Output is correct |
57 |
Correct |
1387 ms |
18324 KB |
Output is correct |
58 |
Correct |
1371 ms |
17564 KB |
Output is correct |
59 |
Correct |
1480 ms |
19196 KB |
Output is correct |
60 |
Correct |
1436 ms |
18340 KB |
Output is correct |
61 |
Correct |
1216 ms |
17144 KB |
Output is correct |
62 |
Correct |
1265 ms |
17600 KB |
Output is correct |
63 |
Correct |
1338 ms |
18420 KB |
Output is correct |
64 |
Correct |
1235 ms |
18700 KB |
Output is correct |
65 |
Correct |
1316 ms |
18076 KB |
Output is correct |
66 |
Correct |
1262 ms |
18016 KB |
Output is correct |
67 |
Correct |
1211 ms |
17244 KB |
Output is correct |
68 |
Correct |
1418 ms |
19292 KB |
Output is correct |
69 |
Correct |
307 ms |
32124 KB |
Output is correct |
70 |
Correct |
327 ms |
30580 KB |
Output is correct |
71 |
Correct |
359 ms |
33596 KB |
Output is correct |
72 |
Correct |
281 ms |
16616 KB |
Output is correct |
73 |
Correct |
290 ms |
17192 KB |
Output is correct |
74 |
Correct |
300 ms |
17864 KB |
Output is correct |
75 |
Correct |
826 ms |
19260 KB |
Output is correct |
76 |
Correct |
895 ms |
20032 KB |
Output is correct |
77 |
Correct |
833 ms |
19268 KB |
Output is correct |
78 |
Correct |
837 ms |
19520 KB |
Output is correct |