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>
constexpr int maxn = 1000010;
#define en cout << "\n";
#define int int64_t
constexpr int inf = 1e18;
using namespace std;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
template <typename T1, typename T2>
void minimize(T1 &a, T2 b) {
if (a > b) {
a = b;
}
}
template <typename T1, typename T2>
void maximize(T1 &a, T2 b) {
if (a < b) {
a = b;
}
}
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n, k;
vii ke[maxn];
bool covered[maxn], used[maxn];
int cnt, mid;
int dfs(int u, int par)
{
if (ke[u].size() == 1 and u > 1)
return 0;
int dist = inf, reach = -inf;
for (auto [v, w] : ke[u])
if (v != par)
{
int a = dfs(v, u);
if (a == 0 and covered[v])
{
maximize(reach, 0LL);
minimize(dist, 0LL);
continue;
}
if (a < 0 and a + w <= 0)
covered[u] = 1;
if (a < 0 and a + w > 0)
a = 0;
else
a += w;
if (a > mid)
{
a = min(int(-mid + w), int(0LL));
if (-mid + w <= 0)
covered[u] = 1;
covered[v] = 1;
used[v] = 1;
++cnt;
}
minimize(dist, a);
maximize(reach, a);
}
if (-dist >= reach)
return dist;
return reach;
}
int32_t main() {
#define name "TASK"
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
int LIM = 0;
for(int i = 1; i < n; i ++)
{
int u, v, w;
cin >> u >> v >> w;
ke[u].emplace_back(v, w);
ke[v].emplace_back(u, w);
LIM += w;
}
int l = 0, r = LIM, ans = LIM;
while (l <= r)
{
for(int i = 1; i <= n; i ++) covered[i] = used[i] = 0;
cnt = 0;
mid = (l + r) >> 1;
if (dfs(1, 1) > 0)
used[1] = 1, ++cnt;
else if (!covered[1])
used[1] = 1, ++cnt;
if (cnt <= k)
r = mid - 1, ans = mid;
else
l = mid + 1;
}
cout << ans;
en;
mid = ans;
for(int i = 1; i <= n; i ++) covered[i] = used[i] = 0;
cnt = 0;
if (dfs(1, 1) > 0)
used[1] = 1, ++cnt;
else if (!covered[1])
used[1] = 1, ++cnt;
vi res;
for(int i = 1; i <= n; i++) if (used[i]) res.emplace_back(i);
for(int i = 1; i <= n; i++) if ((int)res.size() < k and !used[i]) res.emplace_back(i);
for (int it : res)
cout << it << '\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... |