// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
int n,k,ans=0,num=0,mid;
vector<pii> edge[maxn];
vector<int> cur,res;
bool used[maxn];
pii dfs(int u,int par,int len){
int park=inf,Max=0;
for(pii v:edge[u]){
if(v.first==par) continue;
pii x=dfs(v.first,u,v.second);
park=min(park,x.first+v.second);
Max=max(Max,x.second+v.second);
}
if(Max>0 && par==0){
num++;cur.push_back(u);
return{};
}
if(Max+park<=mid) return {park,0};
else if(Max+len<=mid) return {park,Max};
else{num++;cur.push_back(u);return {0,-inf};}
}
bool check(){
cur.clear();
num=0;dfs(1,0,0);
return (num<=k);
}
void solve(){
cin >> n >> k;
for(int i=1;i<n;i++){
int u,v,w;cin >> u >> v >> w;
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
int l=1,r=1e16,ans=r;
while(r>=l){
mid=(r+l)>>1;
if(check()){ans=mid;res=cur;r=mid-1;}
else l=mid+1;
}
cout << ans << '\n';
for(int v:res) used[v]=true;
for(int i=1;i<=n;i++){
if((int)res.size()==k || used[i]) continue;
res.push_back(i);
}
for(int v:res) cout << v << ' ';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
324 ms |
28524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
312 ms |
29556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |