#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#define int long long
const int mod=1e9+7,mxn=2e5+5,lg=30,inf=1e18,minf=-1e18,Mxn=100000,root=700;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int n,k,mid;
vector<pii>adj[mxn+10];
int cnt=0;
int dist[mxn+10],last[mxn+10],seen[mxn+10];
vector<int>path;
void putmx(pii &a,int k){
if(dist[a.f]<dist[k])swap(a.f,a.s),a.f=k;
else if(dist[a.s]<dist[k])a.s=k;
}
void check(int cur,int p,int up){
for(auto i:adj[cur])if(i.f!=p)check(i.f,cur,i.s);
seen[cur]=last[cur]=-1;
int c=0;
for(auto i:adj[cur])if(i.f!=p){
c++;
if(seen[i.f]!=-1&&(seen[cur]==-1||dist[seen[cur]]<dist[seen[i.f]]))seen[cur]=seen[i.f];
if(last[i.f]!=-1&&(last[cur]==-1||dist[last[cur]]>dist[last[i.f]]))last[cur]=last[i.f];
}
if(seen[cur]!=-1&&last[cur]!=-1&&dist[seen[cur]]+dist[last[cur]]-2*dist[cur]<=mid)seen[cur]=-1;
if(c==0||(dist[last[cur]]-dist[cur]>mid&&seen[cur]==-1))seen[cur]=cur;
if(seen[cur]!=-1&&dist[seen[cur]]-dist[cur]+up>mid)last[cur]=cur,cnt++,path.pb(cur),seen[cur]=-1;
if(seen[cur]!=-1&&cur==1)last[cur]=cur,cnt++,path.pb(cur),seen[cur]=-1;
}
void getdist(int cur,int p){for(auto i:adj[cur])if(i.f!=p)dist[i.f]=dist[cur]+i.s,getdist(i.f,cur);}
bool on[mxn+10];
int32_t main(){
//setIO("mooriokart");
fastio
cin>>n>>k;
int l=0,r=0;
for(int i=0;i<n-1;i++){
int a,b,c;cin>>a>>b>>c;
r+=c;
adj[a].pb({b,c});
adj[b].pb({a,c});
}
getdist(1,-1);
pair<int,vector<int>>ans={inf,{0}};
while(l<=r){
mid=l+(r-l)/2;
path.clear();
cnt=0;
check(1,-1,0);
if(cnt<=k)r=mid-1,ans=min(ans,{mid,path});
else l=mid+1;
}
for(auto i:ans.s)on[i]=1;
cnt=k-ans.s.size();
for(int i=1;i<=n;i++)if(!on[i]&&cnt)ans.s.pb(i),cnt--;
cout<<ans.f<<'\n';
for(auto i:ans.s)cout<<i<<" ";
}
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:36:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
3 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
3 ms |
7768 KB |
Output is correct |
5 |
Correct |
2 ms |
7596 KB |
Output is correct |
6 |
Correct |
2 ms |
7772 KB |
Output is correct |
7 |
Correct |
2 ms |
7772 KB |
Output is correct |
8 |
Correct |
2 ms |
7772 KB |
Output is correct |
9 |
Correct |
2 ms |
7772 KB |
Output is correct |
10 |
Correct |
2 ms |
7768 KB |
Output is correct |
11 |
Correct |
2 ms |
7820 KB |
Output is correct |
12 |
Correct |
2 ms |
7772 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
2 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
32920 KB |
Output is correct |
2 |
Correct |
283 ms |
35196 KB |
Output is correct |
3 |
Correct |
349 ms |
24744 KB |
Output is correct |
4 |
Correct |
1174 ms |
23532 KB |
Output is correct |
5 |
Correct |
923 ms |
23124 KB |
Output is correct |
6 |
Correct |
1154 ms |
23064 KB |
Output is correct |
7 |
Correct |
509 ms |
22100 KB |
Output is correct |
8 |
Correct |
593 ms |
22832 KB |
Output is correct |
9 |
Correct |
987 ms |
22864 KB |
Output is correct |
10 |
Correct |
1165 ms |
23588 KB |
Output is correct |
11 |
Correct |
806 ms |
24856 KB |
Output is correct |
12 |
Correct |
657 ms |
24656 KB |
Output is correct |
13 |
Correct |
834 ms |
25928 KB |
Output is correct |
14 |
Correct |
293 ms |
22864 KB |
Output is correct |
15 |
Correct |
357 ms |
22612 KB |
Output is correct |
16 |
Correct |
670 ms |
24044 KB |
Output is correct |
17 |
Correct |
684 ms |
23376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
319 ms |
35764 KB |
Output is correct |
2 |
Correct |
253 ms |
34976 KB |
Output is correct |
3 |
Correct |
263 ms |
33536 KB |
Output is correct |
4 |
Correct |
253 ms |
33364 KB |
Output is correct |
5 |
Correct |
300 ms |
43048 KB |
Output is correct |
6 |
Correct |
301 ms |
39100 KB |
Output is correct |
7 |
Correct |
323 ms |
39956 KB |
Output is correct |
8 |
Correct |
287 ms |
35156 KB |
Output is correct |
9 |
Correct |
260 ms |
34944 KB |
Output is correct |
10 |
Correct |
262 ms |
34400 KB |
Output is correct |
11 |
Correct |
220 ms |
33172 KB |
Output is correct |
12 |
Correct |
328 ms |
40828 KB |
Output is correct |
13 |
Correct |
381 ms |
39812 KB |
Output is correct |
14 |
Correct |
324 ms |
37960 KB |
Output is correct |
15 |
Correct |
137 ms |
33616 KB |
Output is correct |
16 |
Correct |
135 ms |
32600 KB |
Output is correct |
17 |
Correct |
131 ms |
32336 KB |
Output is correct |
18 |
Correct |
146 ms |
33616 KB |
Output is correct |
19 |
Correct |
167 ms |
37704 KB |
Output is correct |
20 |
Correct |
180 ms |
37000 KB |
Output is correct |
21 |
Correct |
179 ms |
37760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7768 KB |
Output is correct |
2 |
Correct |
3 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
3 ms |
7768 KB |
Output is correct |
5 |
Correct |
2 ms |
7596 KB |
Output is correct |
6 |
Correct |
2 ms |
7772 KB |
Output is correct |
7 |
Correct |
2 ms |
7772 KB |
Output is correct |
8 |
Correct |
2 ms |
7772 KB |
Output is correct |
9 |
Correct |
2 ms |
7772 KB |
Output is correct |
10 |
Correct |
2 ms |
7768 KB |
Output is correct |
11 |
Correct |
2 ms |
7820 KB |
Output is correct |
12 |
Correct |
2 ms |
7772 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
2 ms |
7772 KB |
Output is correct |
15 |
Correct |
135 ms |
32920 KB |
Output is correct |
16 |
Correct |
283 ms |
35196 KB |
Output is correct |
17 |
Correct |
349 ms |
24744 KB |
Output is correct |
18 |
Correct |
1174 ms |
23532 KB |
Output is correct |
19 |
Correct |
923 ms |
23124 KB |
Output is correct |
20 |
Correct |
1154 ms |
23064 KB |
Output is correct |
21 |
Correct |
509 ms |
22100 KB |
Output is correct |
22 |
Correct |
593 ms |
22832 KB |
Output is correct |
23 |
Correct |
987 ms |
22864 KB |
Output is correct |
24 |
Correct |
1165 ms |
23588 KB |
Output is correct |
25 |
Correct |
806 ms |
24856 KB |
Output is correct |
26 |
Correct |
657 ms |
24656 KB |
Output is correct |
27 |
Correct |
834 ms |
25928 KB |
Output is correct |
28 |
Correct |
293 ms |
22864 KB |
Output is correct |
29 |
Correct |
357 ms |
22612 KB |
Output is correct |
30 |
Correct |
670 ms |
24044 KB |
Output is correct |
31 |
Correct |
684 ms |
23376 KB |
Output is correct |
32 |
Correct |
319 ms |
35764 KB |
Output is correct |
33 |
Correct |
253 ms |
34976 KB |
Output is correct |
34 |
Correct |
263 ms |
33536 KB |
Output is correct |
35 |
Correct |
253 ms |
33364 KB |
Output is correct |
36 |
Correct |
300 ms |
43048 KB |
Output is correct |
37 |
Correct |
301 ms |
39100 KB |
Output is correct |
38 |
Correct |
323 ms |
39956 KB |
Output is correct |
39 |
Correct |
287 ms |
35156 KB |
Output is correct |
40 |
Correct |
260 ms |
34944 KB |
Output is correct |
41 |
Correct |
262 ms |
34400 KB |
Output is correct |
42 |
Correct |
220 ms |
33172 KB |
Output is correct |
43 |
Correct |
328 ms |
40828 KB |
Output is correct |
44 |
Correct |
381 ms |
39812 KB |
Output is correct |
45 |
Correct |
324 ms |
37960 KB |
Output is correct |
46 |
Correct |
137 ms |
33616 KB |
Output is correct |
47 |
Correct |
135 ms |
32600 KB |
Output is correct |
48 |
Correct |
131 ms |
32336 KB |
Output is correct |
49 |
Correct |
146 ms |
33616 KB |
Output is correct |
50 |
Correct |
167 ms |
37704 KB |
Output is correct |
51 |
Correct |
180 ms |
37000 KB |
Output is correct |
52 |
Correct |
179 ms |
37760 KB |
Output is correct |
53 |
Correct |
1131 ms |
23892 KB |
Output is correct |
54 |
Correct |
1006 ms |
23892 KB |
Output is correct |
55 |
Correct |
1096 ms |
24548 KB |
Output is correct |
56 |
Correct |
1262 ms |
24964 KB |
Output is correct |
57 |
Correct |
1252 ms |
27140 KB |
Output is correct |
58 |
Correct |
1024 ms |
23596 KB |
Output is correct |
59 |
Correct |
1267 ms |
31748 KB |
Output is correct |
60 |
Correct |
542 ms |
22868 KB |
Output is correct |
61 |
Correct |
502 ms |
21728 KB |
Output is correct |
62 |
Correct |
475 ms |
25148 KB |
Output is correct |
63 |
Correct |
573 ms |
23144 KB |
Output is correct |
64 |
Correct |
465 ms |
27868 KB |
Output is correct |
65 |
Correct |
1094 ms |
23576 KB |
Output is correct |
66 |
Correct |
982 ms |
26208 KB |
Output is correct |
67 |
Correct |
998 ms |
22516 KB |
Output is correct |
68 |
Correct |
1342 ms |
29680 KB |
Output is correct |
69 |
Correct |
313 ms |
34964 KB |
Output is correct |
70 |
Correct |
242 ms |
33104 KB |
Output is correct |
71 |
Correct |
329 ms |
39024 KB |
Output is correct |
72 |
Correct |
406 ms |
24032 KB |
Output is correct |
73 |
Correct |
233 ms |
23224 KB |
Output is correct |
74 |
Correct |
186 ms |
27320 KB |
Output is correct |
75 |
Correct |
794 ms |
24928 KB |
Output is correct |
76 |
Correct |
362 ms |
24332 KB |
Output is correct |
77 |
Correct |
641 ms |
24244 KB |
Output is correct |
78 |
Correct |
383 ms |
25644 KB |
Output is correct |