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>
#define Y8o "KTREE"
#define maxn (int) 2e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
const ll INF = 1e18;
int n, k, place;
vector<pii> o[maxn];
ll dis[maxn], reach[maxn], lim;
bool choose[maxn];
void dfs(int u = 1, int p = 0) {
for(auto [v, w] : o[u]) if(v != p) {
dfs(v, u);
if(reach[v] + min(1ll * w, dis[v]) <= lim) {
if(reach[v] + dis[v] > lim) reach[u] = max(reach[u], reach[v] + w);
dis[u] = min(dis[u], dis[v] + w);
}
else {
place ++, choose[v] = 1;
dis[u] = min(dis[u], 1ll * w);
}
}
}
bool check(ll mid) {
lim = mid, place = 0;
fi(i, 1, n) dis[i] = INF, reach[i] = 0, choose[i] = 0;
dfs();
if(reach[1] + dis[1] > lim) place ++, choose[1] = 1;
return place <= k;
}
void solve() {
cin >> n >> k ;
fi(i, 1, n - 1) {
int u, v, w; cin >> u >> v >> w;
o[u].push_back({ v, w }), o[v].push_back({ u, w });
}
ll l = 1, r = 2e14;
while(l <= r) {
ll mid = (l + r) >> 1;
if(check(mid)) r = mid - 1;
else l = mid + 1;
}
cout << l << '\n';
check(l);
if(place < k) {
fi(i ,1, n) {
if(!choose[i]) choose[i] = 1, place ++;
if(place == k) break;
}
}
fi(i, 1, n) if(choose[i]) cout << i << ' ';
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void iof()':
Main.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |