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;
#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';
}
# | 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... |