This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//TrungNotChung
#include <bits/stdc++.h>
#define pii pair<int , int>
#define fi first
#define se second
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define CNT(x) __builtin_popcountll(x)
#define task "tnc"
using namespace std;
const int N = (int)2e5+228;
const long long INF = (long long)1e15;
int n, k, dp[N], mark[N], res[N];
vector<vector<pii> > adj;
long long dist[N], f[N];
void dfs(int u, int par, long long mid)
{
mark[u] = 0, dp[u] = 0, f[u] = -INF, dist[u] = 0;
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi;
if(v == par) continue;
dfs(v, u, mid);
int w = adj[u][i].se;
f[u] = max(f[u], f[v] - w);
dp[u] += dp[v];
if(dist[v] == 0 && f[v] >= 0)
{
mark[v] = 2;
continue;
}
if(dist[v] + w > mid)
{
mark[v] = 1;
f[u] = max(f[u], mid - w);
dp[u]++;
}
}
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi, w = adj[u][i].se;
if(mark[v] || v == par) continue;
if(f[u] - w >= dist[v])
mark[v] = 2;
}
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi, w = adj[u][i].se;
if(mark[v] || v == par) continue;
dist[u] = max(dist[u], dist[v] + w);
}
}
bool check(long long mid)
{
dfs(1, -1, mid);
return (dp[1] <= k);
}
void solve()
{
cin >> n >> k;
adj.assign(n+7, vector<pii>());
for(int i=1; i<=n; ++i)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
// check(3);
// for(int i=1; i<=n; ++i)
// {
// cout << dist[i] << " " << f[i] << " " << dp[i] << '\n';
// }
// return;
long long l = 0, r = INF, best;
while(l <= r)
{
long long mid = (l+r)/2;
if(check(mid))
{
best = mid;
for(int i=1; i<=n; ++i)
if(mark[i] == 1) res[i] = 1;
else res[i] = 0;
r = mid - 1;
}
else l = mid + 1;
}
int cnt = 0;
for(int i=1; i<=n; ++i)
cnt += res[i];
cnt = k - cnt;
for(int i=1; i<=n; ++i)
if(res[i] == 0 && cnt > 0)
res[i] = 1, --cnt;
cout << best << '\n';
for(int i=1; i<=n; ++i)
if(res[i]) cout << i << " ";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
//cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# | 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... |