#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n, k;
vii ke[maxn];
bool covered[maxn], used[maxn];
int cnt, mid;
int dfs(int u, int par)
{
if (ke[u].size() == 1 and u > 1)
return 0;
int dist = inf, reach = -inf;
// dist : distance to nearest park
// reach : how far inward can we place a park
for (auto [v, w] : ke[u])
if (v != par)
{
int a = dfs(v, u);
if (a == 0 and covered[v])
{
maximize(reach, 0LL);
minimize(dist, 0LL);
continue;
}
if (a < 0 and a + w <= 0)
covered[u] = 1;
if (a < 0 and a + w > 0)
a = 0;
else
a += w;
if (a > mid)
{
a = min(-mid + w, 0LL);
if (-mid + w <= 0)
covered[u] = 1;
covered[v] = 1;
used[v] = 1;
++cnt;
}
minimize(dist, a);
maximize(reach, a);
}
if (-dist >= reach)
return dist;
return reach;
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
int LIM = 0;
fo(i, 1, n - 1)
{
int u, v, w;
cin >> u >> v >> w;
ke[u].pb(v, w);
ke[v].pb(u, w);
LIM += w;
}
int l = 0, r = LIM, ans = LIM;
while (l <= r)
{
fo(i, 1, n) covered[i] = used[i] = 0;
cnt = 0;
mid = l + r >> 1;
if (dfs(1, 1) > 0)
used[1] = 1, ++cnt;
else if (!covered[1])
used[1] = 1, ++cnt;
if (cnt <= k)
r = mid - 1, ans = mid;
else
l = mid + 1;
}
cout << ans;
en;
mid = ans;
fo(i, 1, n) covered[i] = used[i] = 0;
cnt = 0;
if (dfs(1, 1) > 0)
used[1] = 1, ++cnt;
else if (!covered[1])
used[1] = 1, ++cnt;
vi res;
fo(i, 1, n) if (used[i]) res.pb(i);
fo(i, 1, n) if ((int)res.size() < k and !used[i]) res.pb(i);
for (int it : res)
cout << it << ' ';
en;
}
Compilation message
Main.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
85 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:110:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | mid = l + r >> 1;
| ~~^~~
Main.cpp:90:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:91:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
4 ms |
25688 KB |
Output is correct |
3 |
Correct |
4 ms |
25692 KB |
Output is correct |
4 |
Correct |
4 ms |
25692 KB |
Output is correct |
5 |
Correct |
5 ms |
25692 KB |
Output is correct |
6 |
Correct |
4 ms |
25536 KB |
Output is correct |
7 |
Correct |
4 ms |
25568 KB |
Output is correct |
8 |
Correct |
4 ms |
25692 KB |
Output is correct |
9 |
Correct |
4 ms |
25688 KB |
Output is correct |
10 |
Correct |
6 ms |
25948 KB |
Output is correct |
11 |
Correct |
5 ms |
25692 KB |
Output is correct |
12 |
Correct |
4 ms |
25484 KB |
Output is correct |
13 |
Correct |
5 ms |
25692 KB |
Output is correct |
14 |
Correct |
4 ms |
25688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
51980 KB |
Output is correct |
2 |
Correct |
162 ms |
54100 KB |
Output is correct |
3 |
Correct |
106 ms |
39612 KB |
Output is correct |
4 |
Correct |
292 ms |
39632 KB |
Output is correct |
5 |
Correct |
274 ms |
38996 KB |
Output is correct |
6 |
Correct |
285 ms |
38992 KB |
Output is correct |
7 |
Correct |
153 ms |
37968 KB |
Output is correct |
8 |
Correct |
175 ms |
38448 KB |
Output is correct |
9 |
Correct |
271 ms |
38804 KB |
Output is correct |
10 |
Correct |
295 ms |
39248 KB |
Output is correct |
11 |
Correct |
333 ms |
40748 KB |
Output is correct |
12 |
Correct |
303 ms |
40948 KB |
Output is correct |
13 |
Correct |
328 ms |
42324 KB |
Output is correct |
14 |
Correct |
164 ms |
39252 KB |
Output is correct |
15 |
Correct |
157 ms |
38996 KB |
Output is correct |
16 |
Correct |
276 ms |
40272 KB |
Output is correct |
17 |
Correct |
270 ms |
39764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
54980 KB |
Output is correct |
2 |
Correct |
150 ms |
53844 KB |
Output is correct |
3 |
Correct |
163 ms |
52308 KB |
Output is correct |
4 |
Correct |
146 ms |
52304 KB |
Output is correct |
5 |
Correct |
203 ms |
56260 KB |
Output is correct |
6 |
Correct |
207 ms |
55232 KB |
Output is correct |
7 |
Correct |
209 ms |
56396 KB |
Output is correct |
8 |
Correct |
167 ms |
54060 KB |
Output is correct |
9 |
Correct |
138 ms |
53844 KB |
Output is correct |
10 |
Correct |
141 ms |
53344 KB |
Output is correct |
11 |
Correct |
133 ms |
52308 KB |
Output is correct |
12 |
Correct |
205 ms |
56524 KB |
Output is correct |
13 |
Correct |
225 ms |
56520 KB |
Output is correct |
14 |
Correct |
220 ms |
55120 KB |
Output is correct |
15 |
Correct |
96 ms |
52564 KB |
Output is correct |
16 |
Correct |
84 ms |
51540 KB |
Output is correct |
17 |
Correct |
91 ms |
51280 KB |
Output is correct |
18 |
Correct |
87 ms |
52816 KB |
Output is correct |
19 |
Correct |
103 ms |
54016 KB |
Output is correct |
20 |
Correct |
110 ms |
54528 KB |
Output is correct |
21 |
Correct |
115 ms |
53448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25688 KB |
Output is correct |
2 |
Correct |
4 ms |
25688 KB |
Output is correct |
3 |
Correct |
4 ms |
25692 KB |
Output is correct |
4 |
Correct |
4 ms |
25692 KB |
Output is correct |
5 |
Correct |
5 ms |
25692 KB |
Output is correct |
6 |
Correct |
4 ms |
25536 KB |
Output is correct |
7 |
Correct |
4 ms |
25568 KB |
Output is correct |
8 |
Correct |
4 ms |
25692 KB |
Output is correct |
9 |
Correct |
4 ms |
25688 KB |
Output is correct |
10 |
Correct |
6 ms |
25948 KB |
Output is correct |
11 |
Correct |
5 ms |
25692 KB |
Output is correct |
12 |
Correct |
4 ms |
25484 KB |
Output is correct |
13 |
Correct |
5 ms |
25692 KB |
Output is correct |
14 |
Correct |
4 ms |
25688 KB |
Output is correct |
15 |
Correct |
106 ms |
51980 KB |
Output is correct |
16 |
Correct |
162 ms |
54100 KB |
Output is correct |
17 |
Correct |
106 ms |
39612 KB |
Output is correct |
18 |
Correct |
292 ms |
39632 KB |
Output is correct |
19 |
Correct |
274 ms |
38996 KB |
Output is correct |
20 |
Correct |
285 ms |
38992 KB |
Output is correct |
21 |
Correct |
153 ms |
37968 KB |
Output is correct |
22 |
Correct |
175 ms |
38448 KB |
Output is correct |
23 |
Correct |
271 ms |
38804 KB |
Output is correct |
24 |
Correct |
295 ms |
39248 KB |
Output is correct |
25 |
Correct |
333 ms |
40748 KB |
Output is correct |
26 |
Correct |
303 ms |
40948 KB |
Output is correct |
27 |
Correct |
328 ms |
42324 KB |
Output is correct |
28 |
Correct |
164 ms |
39252 KB |
Output is correct |
29 |
Correct |
157 ms |
38996 KB |
Output is correct |
30 |
Correct |
276 ms |
40272 KB |
Output is correct |
31 |
Correct |
270 ms |
39764 KB |
Output is correct |
32 |
Correct |
186 ms |
54980 KB |
Output is correct |
33 |
Correct |
150 ms |
53844 KB |
Output is correct |
34 |
Correct |
163 ms |
52308 KB |
Output is correct |
35 |
Correct |
146 ms |
52304 KB |
Output is correct |
36 |
Correct |
203 ms |
56260 KB |
Output is correct |
37 |
Correct |
207 ms |
55232 KB |
Output is correct |
38 |
Correct |
209 ms |
56396 KB |
Output is correct |
39 |
Correct |
167 ms |
54060 KB |
Output is correct |
40 |
Correct |
138 ms |
53844 KB |
Output is correct |
41 |
Correct |
141 ms |
53344 KB |
Output is correct |
42 |
Correct |
133 ms |
52308 KB |
Output is correct |
43 |
Correct |
205 ms |
56524 KB |
Output is correct |
44 |
Correct |
225 ms |
56520 KB |
Output is correct |
45 |
Correct |
220 ms |
55120 KB |
Output is correct |
46 |
Correct |
96 ms |
52564 KB |
Output is correct |
47 |
Correct |
84 ms |
51540 KB |
Output is correct |
48 |
Correct |
91 ms |
51280 KB |
Output is correct |
49 |
Correct |
87 ms |
52816 KB |
Output is correct |
50 |
Correct |
103 ms |
54016 KB |
Output is correct |
51 |
Correct |
110 ms |
54528 KB |
Output is correct |
52 |
Correct |
115 ms |
53448 KB |
Output is correct |
53 |
Correct |
265 ms |
39376 KB |
Output is correct |
54 |
Correct |
303 ms |
39952 KB |
Output is correct |
55 |
Correct |
300 ms |
40532 KB |
Output is correct |
56 |
Correct |
309 ms |
40332 KB |
Output is correct |
57 |
Correct |
315 ms |
41208 KB |
Output is correct |
58 |
Correct |
270 ms |
39648 KB |
Output is correct |
59 |
Correct |
358 ms |
43028 KB |
Output is correct |
60 |
Correct |
153 ms |
39040 KB |
Output is correct |
61 |
Correct |
170 ms |
37784 KB |
Output is correct |
62 |
Correct |
162 ms |
39120 KB |
Output is correct |
63 |
Correct |
199 ms |
38992 KB |
Output is correct |
64 |
Correct |
175 ms |
40896 KB |
Output is correct |
65 |
Correct |
290 ms |
39644 KB |
Output is correct |
66 |
Correct |
305 ms |
40136 KB |
Output is correct |
67 |
Correct |
267 ms |
38736 KB |
Output is correct |
68 |
Correct |
320 ms |
42440 KB |
Output is correct |
69 |
Correct |
149 ms |
54112 KB |
Output is correct |
70 |
Correct |
149 ms |
52052 KB |
Output is correct |
71 |
Correct |
202 ms |
55756 KB |
Output is correct |
72 |
Correct |
97 ms |
39100 KB |
Output is correct |
73 |
Correct |
69 ms |
38840 KB |
Output is correct |
74 |
Correct |
86 ms |
40632 KB |
Output is correct |
75 |
Correct |
293 ms |
41296 KB |
Output is correct |
76 |
Correct |
182 ms |
40564 KB |
Output is correct |
77 |
Correct |
282 ms |
40532 KB |
Output is correct |
78 |
Correct |
169 ms |
40700 KB |
Output is correct |