#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int n, k;
vector<bool> vis;
vector<vector<pair<int, int>>> g;
vector<ll> dp;
ll ans;
int best_pos;
void sol2_dfs1(int u, int p) {
for (auto [v, w]: g[u]) {
if (v == p) {
continue;
}
sol2_dfs1(v, u);
dp[u] = max(dp[u], dp[v] + w);
}
}
void sol2_dfs2(int u, int p, ll val) {
if (ans > max(dp[u], val)) {
ans = max(dp[u], val);
best_pos = u;
}
ll mx1 = val, mx2 = 0;
for (auto [v, w]: g[u]) {
if (v == p) {
continue;
}
if (mx1 < dp[v] + w) {
mx2 = mx1;
mx1 = dp[v] + w;
} else {
mx2 = max(mx2, dp[v] + w);
}
}
for (auto [v, w]: g[u]) {
if (v == p) {
continue;
}
if (dp[v] + w == mx1) {
sol2_dfs2(v, u, mx2 + w);
} else {
sol2_dfs2(v, u, mx1 + w);
}
}
}
void solve() {
cin >> n >> k;
g.assign(n + 1, {});
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);
}
if (k == 1) {
dp.assign(n + 1, 0);
ans = 1e18;
sol2_dfs1(1, 0);
sol2_dfs2(1, 0, 0);
cout << ans << '\n' << best_pos;
return;
}
if (n <= 20) {
ans = 1e18;
vector<int> built;
for (int i = 0; i < (1 << n); i++) {
if (__builtin_popcount(i) != k) {
continue;
}
priority_queue<pair<ll, int>> pq;
vector<int> now;
for (int j = 0; j < n; j++) {
if ((1 << j) & i) {
pq.emplace(0, j + 1);
now.emplace_back(j + 1);
}
}
vis.assign(n + 1, false);
ll close = 0;
while (!pq.empty()) {
auto [d1, u] = pq.top();
pq.pop();
if (vis[u]) {
continue;
}
vis[u] = true;
d1 = -d1;
close = max(close, d1);
for (auto [v, d2]: g[u]) {
if (!vis[v]) {
pq.emplace(-d1 - d2, v);
}
}
}
if (ans > close) {
ans = close;
built = now;
}
}
cout << ans << '\n';
for (int x: built) {
cout << x << ' ';
}
return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
solve();
// cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
320 KB |
Output is correct |
3 |
Correct |
4 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
312 KB |
Output is correct |
6 |
Correct |
17 ms |
212 KB |
Output is correct |
7 |
Correct |
18 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
212 KB |
Output is correct |
9 |
Correct |
77 ms |
212 KB |
Output is correct |
10 |
Correct |
8 ms |
212 KB |
Output is correct |
11 |
Correct |
52 ms |
312 KB |
Output is correct |
12 |
Correct |
87 ms |
296 KB |
Output is correct |
13 |
Correct |
4 ms |
320 KB |
Output is correct |
14 |
Correct |
38 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
26636 KB |
Output is correct |
2 |
Correct |
78 ms |
31904 KB |
Output is correct |
3 |
Correct |
65 ms |
18748 KB |
Output is correct |
4 |
Correct |
113 ms |
17344 KB |
Output is correct |
5 |
Correct |
87 ms |
16844 KB |
Output is correct |
6 |
Correct |
73 ms |
16716 KB |
Output is correct |
7 |
Correct |
67 ms |
15528 KB |
Output is correct |
8 |
Correct |
84 ms |
16504 KB |
Output is correct |
9 |
Correct |
79 ms |
16740 KB |
Output is correct |
10 |
Correct |
74 ms |
17036 KB |
Output is correct |
11 |
Correct |
77 ms |
18564 KB |
Output is correct |
12 |
Correct |
70 ms |
18312 KB |
Output is correct |
13 |
Correct |
81 ms |
20020 KB |
Output is correct |
14 |
Correct |
69 ms |
16988 KB |
Output is correct |
15 |
Correct |
60 ms |
16460 KB |
Output is correct |
16 |
Correct |
72 ms |
17928 KB |
Output is correct |
17 |
Correct |
66 ms |
17132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
52 ms |
10968 KB |
Unexpected end of file - int64 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
320 KB |
Output is correct |
3 |
Correct |
4 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
312 KB |
Output is correct |
6 |
Correct |
17 ms |
212 KB |
Output is correct |
7 |
Correct |
18 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
212 KB |
Output is correct |
9 |
Correct |
77 ms |
212 KB |
Output is correct |
10 |
Correct |
8 ms |
212 KB |
Output is correct |
11 |
Correct |
52 ms |
312 KB |
Output is correct |
12 |
Correct |
87 ms |
296 KB |
Output is correct |
13 |
Correct |
4 ms |
320 KB |
Output is correct |
14 |
Correct |
38 ms |
296 KB |
Output is correct |
15 |
Correct |
55 ms |
26636 KB |
Output is correct |
16 |
Correct |
78 ms |
31904 KB |
Output is correct |
17 |
Correct |
65 ms |
18748 KB |
Output is correct |
18 |
Correct |
113 ms |
17344 KB |
Output is correct |
19 |
Correct |
87 ms |
16844 KB |
Output is correct |
20 |
Correct |
73 ms |
16716 KB |
Output is correct |
21 |
Correct |
67 ms |
15528 KB |
Output is correct |
22 |
Correct |
84 ms |
16504 KB |
Output is correct |
23 |
Correct |
79 ms |
16740 KB |
Output is correct |
24 |
Correct |
74 ms |
17036 KB |
Output is correct |
25 |
Correct |
77 ms |
18564 KB |
Output is correct |
26 |
Correct |
70 ms |
18312 KB |
Output is correct |
27 |
Correct |
81 ms |
20020 KB |
Output is correct |
28 |
Correct |
69 ms |
16988 KB |
Output is correct |
29 |
Correct |
60 ms |
16460 KB |
Output is correct |
30 |
Correct |
72 ms |
17928 KB |
Output is correct |
31 |
Correct |
66 ms |
17132 KB |
Output is correct |
32 |
Incorrect |
52 ms |
10968 KB |
Unexpected end of file - int64 expected |
33 |
Halted |
0 ms |
0 KB |
- |