#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 30;
int n;
vector<pair<int, int>> g[N];
ll dep[N];
int up[N][18];
ll sum[N][18];
void dfs0(int v, int p){
up[v][0] = p;
sum[v][0] = dep[v] - dep[p];
for (int i = 1; i < 18; i++){
int pp = up[v][i - 1];
up[v][i] = up[pp][i - 1];
sum[v][i] = sum[v][i - 1] + sum[pp][i - 1];
}
for (auto [u, w] : g[v]){
if (u != p){
dep[u] = dep[v] + w;
dfs0(u, v);
}
}
}
ll was[N];
bool deleted[N];
void dfs(int v, int p, ll dist){
if (was[v] >= dist) return;
was[v] = dist;
deleted[v] = true;
for (auto [u, w] : g[v]){
if (u == p) continue;
if (w <= dist){
dfs(u, v, dist - w);
} else {
break;
}
}
}
int parks[N];
vector<int> order;
int parks_cnt(ll d){
for (int i = 1; i <= n; i++){
was[i] = -1;
deleted[i] = false;
}
int sz = 0;
for (int i = 0; i < n; i++){
int v = order[i];
if (deleted[v]) continue;
ll x = d;
for (int j = 17; j >= 0; j--){
if (sum[v][j] <= x){
x -= sum[v][j];
v = up[v][j];
}
}
parks[sz++] = v;
dfs(v, -1, d);
}
return sz;
}
int ans[N];
void solve(){
int 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);
}
int root = 1;
for (int i = 1; i <= n; i++){
if (g[i].size() > 1) root = i;
sort(all(g[i]), [&](auto x, auto y){
return x.second < y.second;
});
}
dfs0(root, root);
order.resize(n);
iota(all(order), 1);
sort(all(order), [&](int i, int j){
return dep[i] > dep[j];
});
int sz = 0;
ll l = 0, r = 2e14;
while (l <= r){
ll m = (l + r) >> 1;
int res = parks_cnt(m);
if (res <= k){
sz = res;
for (int i = 0; i < res; i++) ans[i] = parks[i];
r = m - 1;
} else {
l = m + 1;
}
}
sort(ans, ans + sz);
cout << ++r << endl;
for (int i = 0; i < sz; i++) cout << ans[i] << ' ';
int x = k - sz;
for (int i = 1; i <= n; i++){
if (x == 0) break;
int j = lower_bound(ans, ans + sz, i) - ans;
if (j == sz || ans[j] != i){
cout << i << ' ';
x--;
}
}
cout << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:162:13: warning: unused variable 'startTime' [-Wunused-variable]
162 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
69088 KB |
Output is correct |
2 |
Correct |
249 ms |
70824 KB |
Output is correct |
3 |
Correct |
478 ms |
59080 KB |
Output is correct |
4 |
Correct |
687 ms |
55512 KB |
Output is correct |
5 |
Correct |
629 ms |
53824 KB |
Output is correct |
6 |
Correct |
620 ms |
53860 KB |
Output is correct |
7 |
Correct |
600 ms |
54316 KB |
Output is correct |
8 |
Correct |
641 ms |
56916 KB |
Output is correct |
9 |
Correct |
675 ms |
55536 KB |
Output is correct |
10 |
Correct |
716 ms |
56760 KB |
Output is correct |
11 |
Correct |
474 ms |
58028 KB |
Output is correct |
12 |
Correct |
467 ms |
56168 KB |
Output is correct |
13 |
Correct |
581 ms |
60716 KB |
Output is correct |
14 |
Correct |
430 ms |
55604 KB |
Output is correct |
15 |
Correct |
441 ms |
55780 KB |
Output is correct |
16 |
Correct |
473 ms |
56696 KB |
Output is correct |
17 |
Correct |
476 ms |
55792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
71980 KB |
Output is correct |
2 |
Correct |
222 ms |
70048 KB |
Output is correct |
3 |
Correct |
227 ms |
66868 KB |
Output is correct |
4 |
Correct |
230 ms |
66680 KB |
Output is correct |
5 |
Correct |
296 ms |
71640 KB |
Output is correct |
6 |
Correct |
401 ms |
71088 KB |
Output is correct |
7 |
Correct |
397 ms |
73312 KB |
Output is correct |
8 |
Correct |
228 ms |
72408 KB |
Output is correct |
9 |
Correct |
222 ms |
71740 KB |
Output is correct |
10 |
Correct |
229 ms |
70476 KB |
Output is correct |
11 |
Correct |
246 ms |
68212 KB |
Output is correct |
12 |
Correct |
387 ms |
73936 KB |
Output is correct |
13 |
Correct |
401 ms |
74372 KB |
Output is correct |
14 |
Correct |
405 ms |
72688 KB |
Output is correct |
15 |
Correct |
229 ms |
70368 KB |
Output is correct |
16 |
Correct |
221 ms |
67756 KB |
Output is correct |
17 |
Correct |
230 ms |
67404 KB |
Output is correct |
18 |
Correct |
253 ms |
70944 KB |
Output is correct |
19 |
Correct |
272 ms |
69568 KB |
Output is correct |
20 |
Correct |
287 ms |
71428 KB |
Output is correct |
21 |
Correct |
276 ms |
70036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
246 ms |
69088 KB |
Output is correct |
16 |
Correct |
249 ms |
70824 KB |
Output is correct |
17 |
Correct |
478 ms |
59080 KB |
Output is correct |
18 |
Correct |
687 ms |
55512 KB |
Output is correct |
19 |
Correct |
629 ms |
53824 KB |
Output is correct |
20 |
Correct |
620 ms |
53860 KB |
Output is correct |
21 |
Correct |
600 ms |
54316 KB |
Output is correct |
22 |
Correct |
641 ms |
56916 KB |
Output is correct |
23 |
Correct |
675 ms |
55536 KB |
Output is correct |
24 |
Correct |
716 ms |
56760 KB |
Output is correct |
25 |
Correct |
474 ms |
58028 KB |
Output is correct |
26 |
Correct |
467 ms |
56168 KB |
Output is correct |
27 |
Correct |
581 ms |
60716 KB |
Output is correct |
28 |
Correct |
430 ms |
55604 KB |
Output is correct |
29 |
Correct |
441 ms |
55780 KB |
Output is correct |
30 |
Correct |
473 ms |
56696 KB |
Output is correct |
31 |
Correct |
476 ms |
55792 KB |
Output is correct |
32 |
Correct |
228 ms |
71980 KB |
Output is correct |
33 |
Correct |
222 ms |
70048 KB |
Output is correct |
34 |
Correct |
227 ms |
66868 KB |
Output is correct |
35 |
Correct |
230 ms |
66680 KB |
Output is correct |
36 |
Correct |
296 ms |
71640 KB |
Output is correct |
37 |
Correct |
401 ms |
71088 KB |
Output is correct |
38 |
Correct |
397 ms |
73312 KB |
Output is correct |
39 |
Correct |
228 ms |
72408 KB |
Output is correct |
40 |
Correct |
222 ms |
71740 KB |
Output is correct |
41 |
Correct |
229 ms |
70476 KB |
Output is correct |
42 |
Correct |
246 ms |
68212 KB |
Output is correct |
43 |
Correct |
387 ms |
73936 KB |
Output is correct |
44 |
Correct |
401 ms |
74372 KB |
Output is correct |
45 |
Correct |
405 ms |
72688 KB |
Output is correct |
46 |
Correct |
229 ms |
70368 KB |
Output is correct |
47 |
Correct |
221 ms |
67756 KB |
Output is correct |
48 |
Correct |
230 ms |
67404 KB |
Output is correct |
49 |
Correct |
253 ms |
70944 KB |
Output is correct |
50 |
Correct |
272 ms |
69568 KB |
Output is correct |
51 |
Correct |
287 ms |
71428 KB |
Output is correct |
52 |
Correct |
276 ms |
70036 KB |
Output is correct |
53 |
Correct |
2421 ms |
55020 KB |
Output is correct |
54 |
Correct |
2021 ms |
57416 KB |
Output is correct |
55 |
Correct |
2707 ms |
59652 KB |
Output is correct |
56 |
Correct |
1346 ms |
58340 KB |
Output is correct |
57 |
Correct |
1608 ms |
59216 KB |
Output is correct |
58 |
Correct |
841 ms |
56372 KB |
Output is correct |
59 |
Correct |
1499 ms |
61020 KB |
Output is correct |
60 |
Correct |
970 ms |
59468 KB |
Output is correct |
61 |
Correct |
785 ms |
54600 KB |
Output is correct |
62 |
Correct |
770 ms |
55684 KB |
Output is correct |
63 |
Correct |
657 ms |
59596 KB |
Output is correct |
64 |
Correct |
805 ms |
58848 KB |
Output is correct |
65 |
Correct |
2791 ms |
58068 KB |
Output is correct |
66 |
Correct |
1928 ms |
57440 KB |
Output is correct |
67 |
Correct |
872 ms |
55120 KB |
Output is correct |
68 |
Correct |
1899 ms |
61704 KB |
Output is correct |
69 |
Correct |
233 ms |
71652 KB |
Output is correct |
70 |
Correct |
224 ms |
68500 KB |
Output is correct |
71 |
Correct |
440 ms |
74812 KB |
Output is correct |
72 |
Correct |
466 ms |
56928 KB |
Output is correct |
73 |
Correct |
608 ms |
59328 KB |
Output is correct |
74 |
Correct |
629 ms |
60872 KB |
Output is correct |
75 |
Correct |
511 ms |
58228 KB |
Output is correct |
76 |
Correct |
657 ms |
61540 KB |
Output is correct |
77 |
Correct |
528 ms |
58572 KB |
Output is correct |
78 |
Correct |
641 ms |
59156 KB |
Output is correct |