This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"
typedef long long ll;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,k;
int res[maxn];
vector<pair<int,int>>G[maxn];
int sum=0;
int f[maxn];
int dist[maxn];
int mark[maxn];
int dp[maxn];
void dfs(int u,int pa,int mid){
for(auto v:G[u]){
if(v.fi==pa)continue;
int w=v.se;
dfs(v.fi,u,mid);
f[u]=max(f[v.fi]-w,f[u]);
dp[u]+=dp[v.fi];
if(dist[v.fi]==0 && f[v.fi]>=0){
mark[v.fi]=2;
continue;
}
if(dist[v.fi]+w>mid){
mark[v.fi]=1;
f[u]=max(mid-w,f[u]);
dp[u]++;
}
}
for(auto v:G[u]){
if(v.fi==pa || mark[v.fi])continue;
if(f[u]-v.se>=dist[v.fi]){
mark[v.fi]=2;
}
}
for(auto v:G[u]){
if(v.fi==pa || mark[v.fi])continue;
dist[u]=max(dist[u],dist[v.fi]+v.se);
}
}
bool check(int mid){
sum=0;
for(int i=1;i<=n;i++){
f[i]=-INF;
dist[i]=0;
mark[i]=0;
dp[i]=0;
res[i]=0;
}
dp[1]++;
dfs(1,-1,mid);
if(dist[1]==0 && f[1]>=0){
dp[1]--;
}
else{
mark[1]=1;
}
if(dp[1]<=k){
return true;
}
else{
return false;
}
}
signed main()
{
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin>>n>>k;
int l,r;
l=1;
for(int i=1;i<n;i++){
int x,y,w;
cin>>x>>y>>w;
G[x].pb({y,w});
G[y].pb({x,w});
}
r=1e15;
int ans;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){
ans=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++){
if(res[i]==1){
cnt++;
}
}
cnt=k-cnt;
for(int i=1;i<=n;i++){
if(res[i]==0 && cnt>0){
res[i]=1;
cnt--;
}
}
cout<<ans<<"\n";
for(int i=1;i<=n;i++){
if(res[i]==1){
cout<<i<<" ";
}
}
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... |