#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 25;
vector <pair <int, int>> adj[MAXN];
int mid;
int n, k;
int cnt = 0;
vector <int> cur, anss;
void dfs (int pos, int par, int dep = 0) {
if (dep > mid) {
cnt++; dep = 0; cur.push_back(pos);
}
for (auto j : adj[pos])
if (j.first != par)
dfs(j.first, pos, dep + j.second);
}
bool check () {
for (int i = 1; i <= n; i++) {
cnt = 1;
cur.clear(); cur.push_back(i);
dfs(i, -1);
if (cnt <= k) {
anss = cur;
return 1;
}
}
return 0;
}
signed main () {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int l = 1, r = 1e16; int ans = 1;
while (l <= r) {
mid = (l + r) >> 1;
if (check()) {
r = mid - 1; ans = mid;
} else {
l = mid + 1;
}
}
cout << ans << '\n';
set <int> u; for (int i = 1; i <= n; i++) u.insert(i);
for (auto i : anss) {
u.erase(i); cout << i << " "; k--;
}
while (k--) {
cout << *(--u.end()) << " ";
u.erase(--u.end());
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3046 ms |
28356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3030 ms |
30936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |