#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
const int MAXN = 200'005;
const ll inf = 1e16;
vector<pair<int, ll>> adjlist[MAXN];
vector<int> ans;
pair<bool, ll> dfs(int x, int par, ll dist, ll maxdist) {
ll over = -inf, under = 0;
for (auto nxt : adjlist[x]) {
int nn = nxt.first; ll nd = nxt.second;
if (nn == par) continue;
auto res = dfs(nn, x, nd, maxdist);
if (res.first) over = max(over, res.second);
else under = max(under, res.second);
}
if (over >= under) return { 1, over - dist };
else if (under + dist <= maxdist) return { 0, under + dist };
ans.push_back(x);
return { 1, maxdist - dist };
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nums, parks; cin >> nums >> parks;
for (int i = 0; i < nums - 1; i++) {
int a, b; ll w; cin >> a >> b >> w;
adjlist[a].push_back({ b, w });
adjlist[b].push_back({ a, w });
}
ll l = 0, r = inf, opt;
while (l <= r) {
ll m = (l + r) >> 1;
if (m == 8) {
cout << "";
}
dfs(1, -1, inf, m);
if (ans.size() <= parks) {
opt = m;
r = m - 1;
}
else l = m + 1;
ans.clear();
}
cout << opt << '\n';
dfs(1, -1, inf, opt);
set<int> s;
for (auto x : ans) s.insert(x);
for (int i = 1; ans.size() < parks; i++)
if (!s.count(i))
ans.push_back(i);
for (auto x : ans) cout << x << ' ';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:41:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
41 | if (ans.size() <= parks) {
| ~~~~~~~~~~~^~~~~~~~
Main.cpp:52:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | for (int i = 1; ans.size() < parks; i++)
| ~~~~~~~~~~~^~~~~~~
Main.cpp:49:21: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
49 | dfs(1, -1, inf, opt);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 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 |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
4940 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 |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
28444 KB |
Output is correct |
2 |
Correct |
322 ms |
29688 KB |
Output is correct |
3 |
Correct |
240 ms |
15604 KB |
Output is correct |
4 |
Correct |
1151 ms |
15116 KB |
Output is correct |
5 |
Correct |
891 ms |
14808 KB |
Output is correct |
6 |
Correct |
1039 ms |
14800 KB |
Output is correct |
7 |
Correct |
881 ms |
14892 KB |
Output is correct |
8 |
Correct |
1055 ms |
15388 KB |
Output is correct |
9 |
Correct |
951 ms |
14628 KB |
Output is correct |
10 |
Correct |
1046 ms |
15368 KB |
Output is correct |
11 |
Correct |
668 ms |
16320 KB |
Output is correct |
12 |
Correct |
754 ms |
16608 KB |
Output is correct |
13 |
Correct |
733 ms |
17564 KB |
Output is correct |
14 |
Correct |
653 ms |
16400 KB |
Output is correct |
15 |
Correct |
659 ms |
16076 KB |
Output is correct |
16 |
Correct |
691 ms |
16524 KB |
Output is correct |
17 |
Correct |
607 ms |
15976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
29540 KB |
Output is correct |
2 |
Correct |
309 ms |
28984 KB |
Output is correct |
3 |
Correct |
284 ms |
27768 KB |
Output is correct |
4 |
Correct |
262 ms |
27588 KB |
Output is correct |
5 |
Correct |
420 ms |
43000 KB |
Output is correct |
6 |
Correct |
337 ms |
38024 KB |
Output is correct |
7 |
Correct |
386 ms |
39960 KB |
Output is correct |
8 |
Correct |
303 ms |
33160 KB |
Output is correct |
9 |
Correct |
333 ms |
32928 KB |
Output is correct |
10 |
Correct |
290 ms |
30312 KB |
Output is correct |
11 |
Correct |
275 ms |
28104 KB |
Output is correct |
12 |
Correct |
406 ms |
40252 KB |
Output is correct |
13 |
Correct |
370 ms |
37652 KB |
Output is correct |
14 |
Correct |
350 ms |
34332 KB |
Output is correct |
15 |
Correct |
267 ms |
28980 KB |
Output is correct |
16 |
Correct |
293 ms |
27984 KB |
Output is correct |
17 |
Correct |
269 ms |
27892 KB |
Output is correct |
18 |
Correct |
272 ms |
29036 KB |
Output is correct |
19 |
Correct |
349 ms |
37728 KB |
Output is correct |
20 |
Correct |
307 ms |
36372 KB |
Output is correct |
21 |
Correct |
302 ms |
34064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
2 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 |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
4940 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 |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
15 |
Correct |
264 ms |
28444 KB |
Output is correct |
16 |
Correct |
322 ms |
29688 KB |
Output is correct |
17 |
Correct |
240 ms |
15604 KB |
Output is correct |
18 |
Correct |
1151 ms |
15116 KB |
Output is correct |
19 |
Correct |
891 ms |
14808 KB |
Output is correct |
20 |
Correct |
1039 ms |
14800 KB |
Output is correct |
21 |
Correct |
881 ms |
14892 KB |
Output is correct |
22 |
Correct |
1055 ms |
15388 KB |
Output is correct |
23 |
Correct |
951 ms |
14628 KB |
Output is correct |
24 |
Correct |
1046 ms |
15368 KB |
Output is correct |
25 |
Correct |
668 ms |
16320 KB |
Output is correct |
26 |
Correct |
754 ms |
16608 KB |
Output is correct |
27 |
Correct |
733 ms |
17564 KB |
Output is correct |
28 |
Correct |
653 ms |
16400 KB |
Output is correct |
29 |
Correct |
659 ms |
16076 KB |
Output is correct |
30 |
Correct |
691 ms |
16524 KB |
Output is correct |
31 |
Correct |
607 ms |
15976 KB |
Output is correct |
32 |
Correct |
353 ms |
29540 KB |
Output is correct |
33 |
Correct |
309 ms |
28984 KB |
Output is correct |
34 |
Correct |
284 ms |
27768 KB |
Output is correct |
35 |
Correct |
262 ms |
27588 KB |
Output is correct |
36 |
Correct |
420 ms |
43000 KB |
Output is correct |
37 |
Correct |
337 ms |
38024 KB |
Output is correct |
38 |
Correct |
386 ms |
39960 KB |
Output is correct |
39 |
Correct |
303 ms |
33160 KB |
Output is correct |
40 |
Correct |
333 ms |
32928 KB |
Output is correct |
41 |
Correct |
290 ms |
30312 KB |
Output is correct |
42 |
Correct |
275 ms |
28104 KB |
Output is correct |
43 |
Correct |
406 ms |
40252 KB |
Output is correct |
44 |
Correct |
370 ms |
37652 KB |
Output is correct |
45 |
Correct |
350 ms |
34332 KB |
Output is correct |
46 |
Correct |
267 ms |
28980 KB |
Output is correct |
47 |
Correct |
293 ms |
27984 KB |
Output is correct |
48 |
Correct |
269 ms |
27892 KB |
Output is correct |
49 |
Correct |
272 ms |
29036 KB |
Output is correct |
50 |
Correct |
349 ms |
37728 KB |
Output is correct |
51 |
Correct |
307 ms |
36372 KB |
Output is correct |
52 |
Correct |
302 ms |
34064 KB |
Output is correct |
53 |
Correct |
990 ms |
14524 KB |
Output is correct |
54 |
Correct |
1073 ms |
14812 KB |
Output is correct |
55 |
Correct |
1143 ms |
15216 KB |
Output is correct |
56 |
Correct |
1071 ms |
15580 KB |
Output is correct |
57 |
Correct |
1098 ms |
19252 KB |
Output is correct |
58 |
Correct |
964 ms |
14612 KB |
Output is correct |
59 |
Correct |
1085 ms |
26128 KB |
Output is correct |
60 |
Correct |
1030 ms |
15208 KB |
Output is correct |
61 |
Correct |
1024 ms |
14292 KB |
Output is correct |
62 |
Correct |
884 ms |
19752 KB |
Output is correct |
63 |
Correct |
1058 ms |
15244 KB |
Output is correct |
64 |
Correct |
1081 ms |
24820 KB |
Output is correct |
65 |
Correct |
1101 ms |
15060 KB |
Output is correct |
66 |
Correct |
945 ms |
19772 KB |
Output is correct |
67 |
Correct |
906 ms |
14360 KB |
Output is correct |
68 |
Correct |
1184 ms |
26128 KB |
Output is correct |
69 |
Correct |
344 ms |
29052 KB |
Output is correct |
70 |
Correct |
267 ms |
27812 KB |
Output is correct |
71 |
Correct |
354 ms |
34868 KB |
Output is correct |
72 |
Correct |
297 ms |
14568 KB |
Output is correct |
73 |
Correct |
241 ms |
14584 KB |
Output is correct |
74 |
Correct |
292 ms |
19908 KB |
Output is correct |
75 |
Correct |
678 ms |
16336 KB |
Output is correct |
76 |
Correct |
703 ms |
16796 KB |
Output is correct |
77 |
Correct |
634 ms |
16240 KB |
Output is correct |
78 |
Correct |
626 ms |
18336 KB |
Output is correct |