#include <bits/stdc++.h>
constexpr int maxn = 1000010;
#define en cout << "\n";
#define int int64_t
constexpr int inf = 1e18;
using namespace std;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
template <typename T1, typename T2>
void minimize(T1 &a, T2 b) {
if (a > b) {
a = b;
}
}
template <typename T1, typename T2>
void maximize(T1 &a, T2 b) {
if (a < b) {
a = b;
}
}
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;
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(int(-mid + w), int(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;
}
int32_t main() {
#define name "TASK"
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
int LIM = 0;
for(int i = 1; i < n; i ++)
{
int u, v, w;
cin >> u >> v >> w;
ke[u].emplace_back(v, w);
ke[v].emplace_back(u, w);
LIM += w;
}
int l = 0, r = LIM, ans = LIM;
while (l <= r)
{
for(int i = 1; i <= n; i ++) 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;
for(int i = 1; i <= n; i ++) 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;
for(int i = 1; i <= n; i++) if (used[i]) res.emplace_back(i);
for(int i = 1; i <= n; i++) if ((int)res.size() < k and !used[i]) res.emplace_back(i);
for (int it : res)
cout << it << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23896 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23948 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23856 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23900 KB |
Output is correct |
12 |
Correct |
10 ms |
23956 KB |
Output is correct |
13 |
Correct |
12 ms |
23896 KB |
Output is correct |
14 |
Correct |
12 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
50464 KB |
Output is correct |
2 |
Correct |
182 ms |
52632 KB |
Output is correct |
3 |
Correct |
121 ms |
38020 KB |
Output is correct |
4 |
Correct |
391 ms |
38012 KB |
Output is correct |
5 |
Correct |
371 ms |
37616 KB |
Output is correct |
6 |
Correct |
361 ms |
37460 KB |
Output is correct |
7 |
Correct |
221 ms |
36300 KB |
Output is correct |
8 |
Correct |
203 ms |
36944 KB |
Output is correct |
9 |
Correct |
332 ms |
37716 KB |
Output is correct |
10 |
Correct |
359 ms |
37636 KB |
Output is correct |
11 |
Correct |
328 ms |
39388 KB |
Output is correct |
12 |
Correct |
318 ms |
39276 KB |
Output is correct |
13 |
Correct |
342 ms |
40788 KB |
Output is correct |
14 |
Correct |
189 ms |
37672 KB |
Output is correct |
15 |
Correct |
170 ms |
37456 KB |
Output is correct |
16 |
Correct |
306 ms |
38740 KB |
Output is correct |
17 |
Correct |
298 ms |
37940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
53072 KB |
Output is correct |
2 |
Correct |
158 ms |
52340 KB |
Output is correct |
3 |
Correct |
154 ms |
50768 KB |
Output is correct |
4 |
Correct |
149 ms |
50832 KB |
Output is correct |
5 |
Correct |
182 ms |
54464 KB |
Output is correct |
6 |
Correct |
194 ms |
53452 KB |
Output is correct |
7 |
Correct |
212 ms |
54848 KB |
Output is correct |
8 |
Correct |
160 ms |
52628 KB |
Output is correct |
9 |
Correct |
158 ms |
52304 KB |
Output is correct |
10 |
Correct |
166 ms |
51712 KB |
Output is correct |
11 |
Correct |
149 ms |
51028 KB |
Output is correct |
12 |
Correct |
213 ms |
54956 KB |
Output is correct |
13 |
Correct |
207 ms |
54732 KB |
Output is correct |
14 |
Correct |
186 ms |
53708 KB |
Output is correct |
15 |
Correct |
103 ms |
50944 KB |
Output is correct |
16 |
Correct |
102 ms |
50004 KB |
Output is correct |
17 |
Correct |
104 ms |
49932 KB |
Output is correct |
18 |
Correct |
101 ms |
51280 KB |
Output is correct |
19 |
Correct |
119 ms |
52384 KB |
Output is correct |
20 |
Correct |
114 ms |
52936 KB |
Output is correct |
21 |
Correct |
114 ms |
51940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23896 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23948 KB |
Output is correct |
6 |
Correct |
10 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
23856 KB |
Output is correct |
8 |
Correct |
10 ms |
23900 KB |
Output is correct |
9 |
Correct |
10 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23900 KB |
Output is correct |
12 |
Correct |
10 ms |
23956 KB |
Output is correct |
13 |
Correct |
12 ms |
23896 KB |
Output is correct |
14 |
Correct |
12 ms |
23900 KB |
Output is correct |
15 |
Correct |
107 ms |
50464 KB |
Output is correct |
16 |
Correct |
182 ms |
52632 KB |
Output is correct |
17 |
Correct |
121 ms |
38020 KB |
Output is correct |
18 |
Correct |
391 ms |
38012 KB |
Output is correct |
19 |
Correct |
371 ms |
37616 KB |
Output is correct |
20 |
Correct |
361 ms |
37460 KB |
Output is correct |
21 |
Correct |
221 ms |
36300 KB |
Output is correct |
22 |
Correct |
203 ms |
36944 KB |
Output is correct |
23 |
Correct |
332 ms |
37716 KB |
Output is correct |
24 |
Correct |
359 ms |
37636 KB |
Output is correct |
25 |
Correct |
328 ms |
39388 KB |
Output is correct |
26 |
Correct |
318 ms |
39276 KB |
Output is correct |
27 |
Correct |
342 ms |
40788 KB |
Output is correct |
28 |
Correct |
189 ms |
37672 KB |
Output is correct |
29 |
Correct |
170 ms |
37456 KB |
Output is correct |
30 |
Correct |
306 ms |
38740 KB |
Output is correct |
31 |
Correct |
298 ms |
37940 KB |
Output is correct |
32 |
Correct |
170 ms |
53072 KB |
Output is correct |
33 |
Correct |
158 ms |
52340 KB |
Output is correct |
34 |
Correct |
154 ms |
50768 KB |
Output is correct |
35 |
Correct |
149 ms |
50832 KB |
Output is correct |
36 |
Correct |
182 ms |
54464 KB |
Output is correct |
37 |
Correct |
194 ms |
53452 KB |
Output is correct |
38 |
Correct |
212 ms |
54848 KB |
Output is correct |
39 |
Correct |
160 ms |
52628 KB |
Output is correct |
40 |
Correct |
158 ms |
52304 KB |
Output is correct |
41 |
Correct |
166 ms |
51712 KB |
Output is correct |
42 |
Correct |
149 ms |
51028 KB |
Output is correct |
43 |
Correct |
213 ms |
54956 KB |
Output is correct |
44 |
Correct |
207 ms |
54732 KB |
Output is correct |
45 |
Correct |
186 ms |
53708 KB |
Output is correct |
46 |
Correct |
103 ms |
50944 KB |
Output is correct |
47 |
Correct |
102 ms |
50004 KB |
Output is correct |
48 |
Correct |
104 ms |
49932 KB |
Output is correct |
49 |
Correct |
101 ms |
51280 KB |
Output is correct |
50 |
Correct |
119 ms |
52384 KB |
Output is correct |
51 |
Correct |
114 ms |
52936 KB |
Output is correct |
52 |
Correct |
114 ms |
51940 KB |
Output is correct |
53 |
Correct |
332 ms |
37968 KB |
Output is correct |
54 |
Correct |
429 ms |
38484 KB |
Output is correct |
55 |
Correct |
337 ms |
38992 KB |
Output is correct |
56 |
Correct |
347 ms |
38884 KB |
Output is correct |
57 |
Correct |
405 ms |
39580 KB |
Output is correct |
58 |
Correct |
333 ms |
37972 KB |
Output is correct |
59 |
Correct |
393 ms |
41320 KB |
Output is correct |
60 |
Correct |
201 ms |
37456 KB |
Output is correct |
61 |
Correct |
179 ms |
36180 KB |
Output is correct |
62 |
Correct |
191 ms |
37580 KB |
Output is correct |
63 |
Correct |
198 ms |
37348 KB |
Output is correct |
64 |
Correct |
200 ms |
39400 KB |
Output is correct |
65 |
Correct |
342 ms |
37716 KB |
Output is correct |
66 |
Correct |
406 ms |
38568 KB |
Output is correct |
67 |
Correct |
321 ms |
36948 KB |
Output is correct |
68 |
Correct |
389 ms |
40920 KB |
Output is correct |
69 |
Correct |
168 ms |
52628 KB |
Output is correct |
70 |
Correct |
152 ms |
50516 KB |
Output is correct |
71 |
Correct |
203 ms |
54212 KB |
Output is correct |
72 |
Correct |
115 ms |
37320 KB |
Output is correct |
73 |
Correct |
83 ms |
36544 KB |
Output is correct |
74 |
Correct |
101 ms |
38896 KB |
Output is correct |
75 |
Correct |
342 ms |
39764 KB |
Output is correct |
76 |
Correct |
193 ms |
38992 KB |
Output is correct |
77 |
Correct |
311 ms |
38996 KB |
Output is correct |
78 |
Correct |
194 ms |
38868 KB |
Output is correct |