// I stand with PALESTINE
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
vector<vector<pair<int, int>>> g(N);
vector<ll> height(N);
vector<int> par(N);
void dfs(int u) {
for (auto [v, w]: g[u]) {
if (par[u] == v) continue;
par[v] = u;
height[v] = height[u] + w;
dfs(v);
}
}
void solve() {
int n, k;
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs(1);
vector<bool> marked, copy;
vector<pair<ll, int>> vec(n);
for (int i = 1; i <= n; i++) vec[i - 1] = make_pair(height[i], i);
sort(vec.rbegin(), vec.rend());
// for (int i = 1; i <= n; i++) cout << height[i] << ' ';
// cout << endl;
// for (int i = 0; i < n; i++) cout << vec[i].second << ' ';
// cout << endl;
auto check = [&](ll m) -> bool {
vector<bool> vis(n + 1);
marked.assign(n + 1, false);
int need = 0;
for (auto [d, u]: vec) {
if (vis[u]) continue;
need++;
int root = u;
while (root != 1 and height[u] - height[par[root]] <= m) root = par[root];
queue<tuple<ll, int, int>> q; q.emplace(0, root, 0);
vis[root] = marked[root] = true;
while (!q.empty()) {
auto [d, v, p] = q.front(); q.pop();
for (auto [vv, w]: g[v]) {
if (vv != p and d + w <= m) {
vis[vv] = true;
q.emplace(d + w, vv, v);
}
}
}
}
// cout << m << ' ' << need << ' ' << count(marked.begin(), marked.end(), true) << endl;
return need <= k;
};
ll lx = 0, rx = 1e18, best = -1;
while (lx <= rx) {
ll mid = (lx + rx) >> 1;
if (check(mid)) {
best = mid;
copy = marked;
rx = mid - 1;
} else {
lx = mid + 1;
}
}
cout << best << '\n';
vector<int> ans;
for (int i = 1; i <= n; i++) if (copy[i]) ans.emplace_back(i);
for (int i = 1; i <= n; i++) {
if (!copy[i] and ans.size() < k) ans.emplace_back(i);
}
for (int x: ans) cout << x << ' ';
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:84:37: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | if (!copy[i] and ans.size() < k) ans.emplace_back(i);
| ~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7500 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7256 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7260 KB |
Output is correct |
14 |
Correct |
4 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
31060 KB |
Output is correct |
2 |
Correct |
174 ms |
32852 KB |
Output is correct |
3 |
Correct |
316 ms |
26264 KB |
Output is correct |
4 |
Correct |
1112 ms |
22156 KB |
Output is correct |
5 |
Correct |
1147 ms |
21616 KB |
Output is correct |
6 |
Correct |
452 ms |
21584 KB |
Output is correct |
7 |
Correct |
554 ms |
20552 KB |
Output is correct |
8 |
Correct |
671 ms |
21028 KB |
Output is correct |
9 |
Correct |
653 ms |
21432 KB |
Output is correct |
10 |
Correct |
714 ms |
21800 KB |
Output is correct |
11 |
Correct |
475 ms |
22732 KB |
Output is correct |
12 |
Correct |
438 ms |
22608 KB |
Output is correct |
13 |
Correct |
476 ms |
23832 KB |
Output is correct |
14 |
Correct |
383 ms |
20960 KB |
Output is correct |
15 |
Correct |
380 ms |
20684 KB |
Output is correct |
16 |
Correct |
426 ms |
21840 KB |
Output is correct |
17 |
Correct |
436 ms |
21232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
170 ms |
33360 KB |
Output is correct |
2 |
Correct |
165 ms |
32596 KB |
Output is correct |
3 |
Correct |
161 ms |
31220 KB |
Output is correct |
4 |
Correct |
171 ms |
31312 KB |
Output is correct |
5 |
Correct |
306 ms |
34076 KB |
Output is correct |
6 |
Correct |
285 ms |
33696 KB |
Output is correct |
7 |
Correct |
296 ms |
34508 KB |
Output is correct |
8 |
Correct |
167 ms |
32848 KB |
Output is correct |
9 |
Correct |
166 ms |
32592 KB |
Output is correct |
10 |
Correct |
170 ms |
32252 KB |
Output is correct |
11 |
Correct |
164 ms |
31060 KB |
Output is correct |
12 |
Correct |
298 ms |
34376 KB |
Output is correct |
13 |
Correct |
282 ms |
34336 KB |
Output is correct |
14 |
Correct |
272 ms |
33456 KB |
Output is correct |
15 |
Correct |
173 ms |
31428 KB |
Output is correct |
16 |
Correct |
165 ms |
30288 KB |
Output is correct |
17 |
Correct |
172 ms |
30224 KB |
Output is correct |
18 |
Correct |
192 ms |
31624 KB |
Output is correct |
19 |
Correct |
203 ms |
32124 KB |
Output is correct |
20 |
Correct |
212 ms |
32492 KB |
Output is correct |
21 |
Correct |
214 ms |
31696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
3 ms |
7260 KB |
Output is correct |
4 |
Correct |
3 ms |
7500 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7256 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7260 KB |
Output is correct |
14 |
Correct |
4 ms |
7516 KB |
Output is correct |
15 |
Correct |
185 ms |
31060 KB |
Output is correct |
16 |
Correct |
174 ms |
32852 KB |
Output is correct |
17 |
Correct |
316 ms |
26264 KB |
Output is correct |
18 |
Correct |
1112 ms |
22156 KB |
Output is correct |
19 |
Correct |
1147 ms |
21616 KB |
Output is correct |
20 |
Correct |
452 ms |
21584 KB |
Output is correct |
21 |
Correct |
554 ms |
20552 KB |
Output is correct |
22 |
Correct |
671 ms |
21028 KB |
Output is correct |
23 |
Correct |
653 ms |
21432 KB |
Output is correct |
24 |
Correct |
714 ms |
21800 KB |
Output is correct |
25 |
Correct |
475 ms |
22732 KB |
Output is correct |
26 |
Correct |
438 ms |
22608 KB |
Output is correct |
27 |
Correct |
476 ms |
23832 KB |
Output is correct |
28 |
Correct |
383 ms |
20960 KB |
Output is correct |
29 |
Correct |
380 ms |
20684 KB |
Output is correct |
30 |
Correct |
426 ms |
21840 KB |
Output is correct |
31 |
Correct |
436 ms |
21232 KB |
Output is correct |
32 |
Correct |
170 ms |
33360 KB |
Output is correct |
33 |
Correct |
165 ms |
32596 KB |
Output is correct |
34 |
Correct |
161 ms |
31220 KB |
Output is correct |
35 |
Correct |
171 ms |
31312 KB |
Output is correct |
36 |
Correct |
306 ms |
34076 KB |
Output is correct |
37 |
Correct |
285 ms |
33696 KB |
Output is correct |
38 |
Correct |
296 ms |
34508 KB |
Output is correct |
39 |
Correct |
167 ms |
32848 KB |
Output is correct |
40 |
Correct |
166 ms |
32592 KB |
Output is correct |
41 |
Correct |
170 ms |
32252 KB |
Output is correct |
42 |
Correct |
164 ms |
31060 KB |
Output is correct |
43 |
Correct |
298 ms |
34376 KB |
Output is correct |
44 |
Correct |
282 ms |
34336 KB |
Output is correct |
45 |
Correct |
272 ms |
33456 KB |
Output is correct |
46 |
Correct |
173 ms |
31428 KB |
Output is correct |
47 |
Correct |
165 ms |
30288 KB |
Output is correct |
48 |
Correct |
172 ms |
30224 KB |
Output is correct |
49 |
Correct |
192 ms |
31624 KB |
Output is correct |
50 |
Correct |
203 ms |
32124 KB |
Output is correct |
51 |
Correct |
212 ms |
32492 KB |
Output is correct |
52 |
Correct |
214 ms |
31696 KB |
Output is correct |
53 |
Execution timed out |
3054 ms |
22072 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |