This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int n, k;
vector<bool> vis;
vector<vector<pair<int, int>>> g;
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 (n <= 20) {
ll 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';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |