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>
//#include<ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC target("avx2")
//#pragma comment(linker, "/STACK:268435456")
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define F first
#define S second
#define pb push_back
#define sz(x) int(x.size())
#define el '\n'
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef short int si;
typedef unsigned long long ull;
typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 2e5 + 500;
vector <int> ans;
ll dp_down[N], dp_up[N], D;
int n, k;
int u, v, w;
vector <pair <int, int> > g[N];
void dfs(int v, int pr, ll d) {
dp_down[v] = 1e18;
dp_up[v] = 0;
for (auto [u, w] : g[v]) {
if (u == pr) {
continue;
}
dfs(u, v, w);
dp_down[v] = min(dp_down[v], dp_down[u]);
dp_up[v] = max(dp_up[v], dp_up[u]);
}
if (dp_down[v] + dp_up[v] <= D) {
dp_up[v] = -1e18;
}
if (dp_up[v] != -1e18 && (pr == -1 || d + dp_up[v] > D)) {
ans.pb(v);
dp_down[v] = 0;
dp_up[v] = -1e18;
}
dp_down[v] += d; dp_up[v] += d;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("anti.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v >> w;
g[v].pb({u, w});
g[u].pb({v, w});
}
ll l = 0;
ll r = (ll)1e18;
while (l < r) {
ll md = (l + r) >> 1;
ans.clear(); D = md;
dfs(1, -1, 0);
if (sz(ans) > k) {
l = md + 1;
} else {
r = md;
}
}
D = l;
ans.clear();
dfs(1, -1, 0);
map <int, int> used;
for (auto x : ans) {
used[x] = 1;
}
for (int i = 1; i <= n; i++) {
if (!used[i] && sz(ans) < k) {
ans.pb(i);
}
}
cout << l << el;
for (auto x : ans) {
cout << x << " ";
}
}
# | 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... |