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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
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;
// dist : distance to nearest park
// reach : how far inward can we place a park
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(-mid + w, 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;
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
int LIM = 0;
fo(i, 1, n - 1)
{
int u, v, w;
cin >> u >> v >> w;
ke[u].pb(v, w);
ke[v].pb(u, w);
LIM += w;
}
int l = 0, r = LIM, ans = LIM;
while (l <= r)
{
fo(i, 1, n) 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;
fo(i, 1, n) 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;
fo(i, 1, n) if (used[i]) res.pb(i);
fo(i, 1, n) if ((int)res.size() < k and !used[i]) res.pb(i);
for (int it : res)
cout << it << ' ';
en;
}
Compilation message (stderr)
Main.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
85 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:110:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | mid = l + r >> 1;
| ~~^~~
Main.cpp:90:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:91:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |