#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k;
vector <pair <int, int>> adj[25];
int dist[20];
void dfs (int pos, int par = -1, int dep = 0) {
dist[pos] = min(dist[pos], dep);
for (auto j : adj[pos]) {
if (j.first != par) {
dfs(j.first, pos, dep + j.second);
}
}
}
signed main () {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b, w;
cin >> a >> b >> w; a--; b--;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
int mn = 1e16;
vector <int> ans;
for (int i = 0; i < (1 << n); i++) {
if (__builtin_popcount(i) == k) {
for (int j = 0; j < n; j++) {
dist[j] = 1e16;
}
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
dfs(j);
}
}
int mx = 0;
for (int j = 0; j < n; j++) mx = max(mx, dist[j]);
if (mx < mn) {
vector <int> p;
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
p.push_back(j + 1);
}
}
mn = mx; ans = p;
}
}
}
cout << mn << '\n';
for (auto i : ans) cout << i << " ";
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
436 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
48 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
22 ms |
436 KB |
Output is correct |
12 |
Correct |
47 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
448 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
436 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
48 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
22 ms |
436 KB |
Output is correct |
12 |
Correct |
47 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
23 ms |
348 KB |
Output is correct |
15 |
Runtime error |
1 ms |
448 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |