#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 18;
const ll MAXN = 2e5 + 5;
ll n, k, q;
vector<vector<pair<int,ll>>> graph;
vector<int> parks, ans_parks;
ll nearest[MAXN], longest[MAXN];
void dfs(int node, int parent) {
for (auto u : graph[node]) {
if (u.f == parent)
continue;
dfs(u.f, node);
nearest[node] = min(nearest[node], nearest[u.f] + u.s);
if (longest[u.f] + u.s <= q) // bir üste aktarabilme durumu
longest[node] = max(longest[node], longest[u.f] + u.s);
else {
parks.push_back(u.f);
nearest[node] = min(nearest[node], u.s);
}
}
if (nearest[node] + longest[node] <= q)
longest[node] = -1e15;
}
bool check(ll x) {
for (int i = 0; i < MAXN; i++)
nearest[i] = 1e15, longest[i] = 0;
parks.clear();
q = x;
dfs(1, 1);
if (longest[1] + nearest[1] > x)
parks.push_back(1);
return (parks.size() <= k);
}
int main() {
fast
cin >> n >> k;
graph = vector<vector<pair<int,ll>>>(n+1, vector<pair<int,ll>>());
for (int i = 1; i < n; i++) {
ll a, b, w;
cin >> a >> b >> w;
graph[a].push_back({b, w});
graph[b].push_back({a, w});
}
ll L = 0;
ll R = 1e15;
ll ans = -1;
while (R >= L) {
ll mid = L + (R-L)/2;
if (check(mid)) {
R = mid - 1;
ans = mid;
ans_parks = parks;
} else
L = mid + 1;
}
cout << ans << "\n";
set<int> st;
for (auto u : ans_parks)
st.insert(u);
int rem = k - st.size();
for (int i = 1; i <= n && rem; i++) {
if (st.count(i) == 0) {
st.insert(i);
rem--;
}
}
for (auto u : st)
cout << u << " ";
cout << "\n";
}
Compilation message
Main.cpp: In function 'bool check(ll)':
Main.cpp:46:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
46 | return (parks.size() <= k);
| ~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3416 KB |
Output is correct |
2 |
Correct |
9 ms |
3936 KB |
Output is correct |
3 |
Correct |
5 ms |
3416 KB |
Output is correct |
4 |
Correct |
5 ms |
3420 KB |
Output is correct |
5 |
Correct |
6 ms |
3420 KB |
Output is correct |
6 |
Correct |
5 ms |
3420 KB |
Output is correct |
7 |
Correct |
6 ms |
3420 KB |
Output is correct |
8 |
Correct |
6 ms |
3420 KB |
Output is correct |
9 |
Correct |
5 ms |
3544 KB |
Output is correct |
10 |
Correct |
5 ms |
3672 KB |
Output is correct |
11 |
Correct |
5 ms |
3540 KB |
Output is correct |
12 |
Correct |
5 ms |
3540 KB |
Output is correct |
13 |
Correct |
6 ms |
3420 KB |
Output is correct |
14 |
Correct |
6 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
37208 KB |
Output is correct |
2 |
Correct |
358 ms |
42336 KB |
Output is correct |
3 |
Correct |
301 ms |
21948 KB |
Output is correct |
4 |
Correct |
859 ms |
21660 KB |
Output is correct |
5 |
Correct |
654 ms |
21076 KB |
Output is correct |
6 |
Correct |
833 ms |
20980 KB |
Output is correct |
7 |
Correct |
769 ms |
19804 KB |
Output is correct |
8 |
Correct |
817 ms |
20904 KB |
Output is correct |
9 |
Correct |
831 ms |
21076 KB |
Output is correct |
10 |
Correct |
941 ms |
21332 KB |
Output is correct |
11 |
Correct |
668 ms |
23668 KB |
Output is correct |
12 |
Correct |
604 ms |
23632 KB |
Output is correct |
13 |
Correct |
774 ms |
25332 KB |
Output is correct |
14 |
Correct |
595 ms |
22316 KB |
Output is correct |
15 |
Correct |
682 ms |
21332 KB |
Output is correct |
16 |
Correct |
646 ms |
23008 KB |
Output is correct |
17 |
Correct |
661 ms |
22212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
406 ms |
38872 KB |
Output is correct |
2 |
Correct |
346 ms |
42000 KB |
Output is correct |
3 |
Correct |
328 ms |
40020 KB |
Output is correct |
4 |
Correct |
341 ms |
40272 KB |
Output is correct |
5 |
Correct |
440 ms |
52808 KB |
Output is correct |
6 |
Correct |
431 ms |
47468 KB |
Output is correct |
7 |
Correct |
470 ms |
50016 KB |
Output is correct |
8 |
Correct |
426 ms |
42568 KB |
Output is correct |
9 |
Correct |
358 ms |
42324 KB |
Output is correct |
10 |
Correct |
406 ms |
41620 KB |
Output is correct |
11 |
Correct |
380 ms |
40020 KB |
Output is correct |
12 |
Correct |
453 ms |
53792 KB |
Output is correct |
13 |
Correct |
427 ms |
51352 KB |
Output is correct |
14 |
Correct |
411 ms |
47444 KB |
Output is correct |
15 |
Correct |
335 ms |
40788 KB |
Output is correct |
16 |
Correct |
375 ms |
39248 KB |
Output is correct |
17 |
Correct |
334 ms |
38996 KB |
Output is correct |
18 |
Correct |
413 ms |
41524 KB |
Output is correct |
19 |
Correct |
406 ms |
50040 KB |
Output is correct |
20 |
Correct |
379 ms |
48724 KB |
Output is correct |
21 |
Correct |
368 ms |
46296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3416 KB |
Output is correct |
2 |
Correct |
9 ms |
3936 KB |
Output is correct |
3 |
Correct |
5 ms |
3416 KB |
Output is correct |
4 |
Correct |
5 ms |
3420 KB |
Output is correct |
5 |
Correct |
6 ms |
3420 KB |
Output is correct |
6 |
Correct |
5 ms |
3420 KB |
Output is correct |
7 |
Correct |
6 ms |
3420 KB |
Output is correct |
8 |
Correct |
6 ms |
3420 KB |
Output is correct |
9 |
Correct |
5 ms |
3544 KB |
Output is correct |
10 |
Correct |
5 ms |
3672 KB |
Output is correct |
11 |
Correct |
5 ms |
3540 KB |
Output is correct |
12 |
Correct |
5 ms |
3540 KB |
Output is correct |
13 |
Correct |
6 ms |
3420 KB |
Output is correct |
14 |
Correct |
6 ms |
3420 KB |
Output is correct |
15 |
Correct |
343 ms |
37208 KB |
Output is correct |
16 |
Correct |
358 ms |
42336 KB |
Output is correct |
17 |
Correct |
301 ms |
21948 KB |
Output is correct |
18 |
Correct |
859 ms |
21660 KB |
Output is correct |
19 |
Correct |
654 ms |
21076 KB |
Output is correct |
20 |
Correct |
833 ms |
20980 KB |
Output is correct |
21 |
Correct |
769 ms |
19804 KB |
Output is correct |
22 |
Correct |
817 ms |
20904 KB |
Output is correct |
23 |
Correct |
831 ms |
21076 KB |
Output is correct |
24 |
Correct |
941 ms |
21332 KB |
Output is correct |
25 |
Correct |
668 ms |
23668 KB |
Output is correct |
26 |
Correct |
604 ms |
23632 KB |
Output is correct |
27 |
Correct |
774 ms |
25332 KB |
Output is correct |
28 |
Correct |
595 ms |
22316 KB |
Output is correct |
29 |
Correct |
682 ms |
21332 KB |
Output is correct |
30 |
Correct |
646 ms |
23008 KB |
Output is correct |
31 |
Correct |
661 ms |
22212 KB |
Output is correct |
32 |
Correct |
406 ms |
38872 KB |
Output is correct |
33 |
Correct |
346 ms |
42000 KB |
Output is correct |
34 |
Correct |
328 ms |
40020 KB |
Output is correct |
35 |
Correct |
341 ms |
40272 KB |
Output is correct |
36 |
Correct |
440 ms |
52808 KB |
Output is correct |
37 |
Correct |
431 ms |
47468 KB |
Output is correct |
38 |
Correct |
470 ms |
50016 KB |
Output is correct |
39 |
Correct |
426 ms |
42568 KB |
Output is correct |
40 |
Correct |
358 ms |
42324 KB |
Output is correct |
41 |
Correct |
406 ms |
41620 KB |
Output is correct |
42 |
Correct |
380 ms |
40020 KB |
Output is correct |
43 |
Correct |
453 ms |
53792 KB |
Output is correct |
44 |
Correct |
427 ms |
51352 KB |
Output is correct |
45 |
Correct |
411 ms |
47444 KB |
Output is correct |
46 |
Correct |
335 ms |
40788 KB |
Output is correct |
47 |
Correct |
375 ms |
39248 KB |
Output is correct |
48 |
Correct |
334 ms |
38996 KB |
Output is correct |
49 |
Correct |
413 ms |
41524 KB |
Output is correct |
50 |
Correct |
406 ms |
50040 KB |
Output is correct |
51 |
Correct |
379 ms |
48724 KB |
Output is correct |
52 |
Correct |
368 ms |
46296 KB |
Output is correct |
53 |
Correct |
789 ms |
21356 KB |
Output is correct |
54 |
Correct |
755 ms |
22356 KB |
Output is correct |
55 |
Correct |
818 ms |
22944 KB |
Output is correct |
56 |
Correct |
717 ms |
23120 KB |
Output is correct |
57 |
Correct |
931 ms |
27032 KB |
Output is correct |
58 |
Correct |
704 ms |
21764 KB |
Output is correct |
59 |
Correct |
865 ms |
34196 KB |
Output is correct |
60 |
Correct |
804 ms |
21456 KB |
Output is correct |
61 |
Correct |
644 ms |
19796 KB |
Output is correct |
62 |
Correct |
653 ms |
25524 KB |
Output is correct |
63 |
Correct |
872 ms |
21372 KB |
Output is correct |
64 |
Correct |
801 ms |
31660 KB |
Output is correct |
65 |
Correct |
737 ms |
21720 KB |
Output is correct |
66 |
Correct |
767 ms |
26580 KB |
Output is correct |
67 |
Correct |
597 ms |
20560 KB |
Output is correct |
68 |
Correct |
997 ms |
33784 KB |
Output is correct |
69 |
Correct |
356 ms |
42580 KB |
Output is correct |
70 |
Correct |
330 ms |
39760 KB |
Output is correct |
71 |
Correct |
410 ms |
48636 KB |
Output is correct |
72 |
Correct |
198 ms |
21564 KB |
Output is correct |
73 |
Correct |
250 ms |
21188 KB |
Output is correct |
74 |
Correct |
264 ms |
27748 KB |
Output is correct |
75 |
Correct |
591 ms |
24148 KB |
Output is correct |
76 |
Correct |
682 ms |
23636 KB |
Output is correct |
77 |
Correct |
572 ms |
23376 KB |
Output is correct |
78 |
Correct |
639 ms |
24988 KB |
Output is correct |