#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define pil pair<int,long long>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=2e5+9;
const long long inf=1e15;
vector<pil>a[maxn];
int n,k;
struct ver{
long long maxdis;
int num;
long long mindis;
ver (){
maxdis=0;
num=0;
mindis=inf;
}
}b[maxn];
vector<int>take;
void dfs(int u,int par,long long mid){
//cerr<<u<<'\n';
if (sz(a[u])==1&&a[u][0].fi==par){
//cerr<<u<<'\n';
for (auto v:a[u]){
if (v.se>mid){
b[u].mindis=0;
b[u].maxdis=-1;
take.pb(u);
b[u].num=1;
}
else {
b[u].maxdis=v.se;
}
}
return;
}
for (auto v:a[u]){
if (v.fi==par)continue;
dfs(v.fi,u,mid);
b[u].maxdis=max(b[u].maxdis,b[v.fi].maxdis);
b[u].num+=b[v.fi].num;
b[u].mindis=min(b[u].mindis,b[v.fi].mindis+v.se);
}
if (b[u].mindis+b[u].maxdis<=mid){
b[u].maxdis=-1;
}
//if (u==8)cout<<b[u].mindis<<" "<<b[u].maxdis<<'\n';
for (auto v:a[u]){
if (v.fi==par){
if (b[u].maxdis==-1){
continue;
}
if (b[u].maxdis+v.se>mid){
b[u].num++;
take.pb(u);
//cerr<<u<<'\n';
b[u].mindis=0;
b[u].maxdis=-1;
}
else {
b[u].maxdis+=v.se;
}
}
}
if (u==1){
if (b[u].maxdis!=-1){
b[u].num++;
take.pb(u);
//cerr<<u<<'\n';
}
}
}
bool check(long long mid){
take.clear();
for1(i,1,n){
b[i]=ver();
}
//cerr<<b[8].num<<'\n';
dfs(1,0,mid);
if (b[1].num>k)return false;
return true;
}
bool vis[maxn];
void push(){
for (auto v:take )vis[v]=1;
for1(i,1,n){
if (vis[i])continue;
if (sz(take)==k)return;
take.pb(i);
vis[i]=1;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("KTREE.INP","r",stdin);
//freopen("KTREE.OUT","w",stdout);
cin>>n>>k;
for1(i,1,n-1){
int u,v;
long long w;
cin>>u>>v>>w;
a[u].pb({v,w});
a[v].pb({u,w});
}
long long l=0,r=2e14,ans=2e14;
while (l<=r){
long long mid=(l+r)/2;
if (check(mid)){
r=mid-1;
ans=mid;
}
else l=mid+1;
}
cout<<ans<<'\n';
check(ans);
push();
for (auto v:take)cout<<v<<" ";
//cout<<check(7);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9704 KB |
Output is correct |
7 |
Correct |
4 ms |
9680 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9660 KB |
Output is correct |
13 |
Correct |
5 ms |
9704 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
410 ms |
33184 KB |
Output is correct |
2 |
Correct |
376 ms |
33816 KB |
Output is correct |
3 |
Correct |
341 ms |
19476 KB |
Output is correct |
4 |
Correct |
1370 ms |
19316 KB |
Output is correct |
5 |
Correct |
1173 ms |
18964 KB |
Output is correct |
6 |
Correct |
1033 ms |
19016 KB |
Output is correct |
7 |
Correct |
1065 ms |
19208 KB |
Output is correct |
8 |
Correct |
1186 ms |
19604 KB |
Output is correct |
9 |
Correct |
1091 ms |
19412 KB |
Output is correct |
10 |
Correct |
1091 ms |
19560 KB |
Output is correct |
11 |
Correct |
828 ms |
20620 KB |
Output is correct |
12 |
Correct |
751 ms |
20736 KB |
Output is correct |
13 |
Correct |
822 ms |
21784 KB |
Output is correct |
14 |
Correct |
798 ms |
20636 KB |
Output is correct |
15 |
Correct |
701 ms |
20176 KB |
Output is correct |
16 |
Correct |
760 ms |
20884 KB |
Output is correct |
17 |
Correct |
784 ms |
20180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
395 ms |
34304 KB |
Output is correct |
2 |
Correct |
340 ms |
33612 KB |
Output is correct |
3 |
Correct |
304 ms |
32344 KB |
Output is correct |
4 |
Correct |
317 ms |
32192 KB |
Output is correct |
5 |
Correct |
383 ms |
35292 KB |
Output is correct |
6 |
Correct |
408 ms |
34764 KB |
Output is correct |
7 |
Correct |
423 ms |
35652 KB |
Output is correct |
8 |
Correct |
350 ms |
34476 KB |
Output is correct |
9 |
Correct |
372 ms |
34196 KB |
Output is correct |
10 |
Correct |
350 ms |
33700 KB |
Output is correct |
11 |
Correct |
336 ms |
32800 KB |
Output is correct |
12 |
Correct |
387 ms |
36280 KB |
Output is correct |
13 |
Correct |
393 ms |
36332 KB |
Output is correct |
14 |
Correct |
386 ms |
35424 KB |
Output is correct |
15 |
Correct |
349 ms |
33856 KB |
Output is correct |
16 |
Correct |
346 ms |
32808 KB |
Output is correct |
17 |
Correct |
327 ms |
32720 KB |
Output is correct |
18 |
Correct |
365 ms |
33988 KB |
Output is correct |
19 |
Correct |
341 ms |
34744 KB |
Output is correct |
20 |
Correct |
391 ms |
35232 KB |
Output is correct |
21 |
Correct |
401 ms |
34632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9704 KB |
Output is correct |
7 |
Correct |
4 ms |
9680 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9660 KB |
Output is correct |
13 |
Correct |
5 ms |
9704 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
410 ms |
33184 KB |
Output is correct |
16 |
Correct |
376 ms |
33816 KB |
Output is correct |
17 |
Correct |
341 ms |
19476 KB |
Output is correct |
18 |
Correct |
1370 ms |
19316 KB |
Output is correct |
19 |
Correct |
1173 ms |
18964 KB |
Output is correct |
20 |
Correct |
1033 ms |
19016 KB |
Output is correct |
21 |
Correct |
1065 ms |
19208 KB |
Output is correct |
22 |
Correct |
1186 ms |
19604 KB |
Output is correct |
23 |
Correct |
1091 ms |
19412 KB |
Output is correct |
24 |
Correct |
1091 ms |
19560 KB |
Output is correct |
25 |
Correct |
828 ms |
20620 KB |
Output is correct |
26 |
Correct |
751 ms |
20736 KB |
Output is correct |
27 |
Correct |
822 ms |
21784 KB |
Output is correct |
28 |
Correct |
798 ms |
20636 KB |
Output is correct |
29 |
Correct |
701 ms |
20176 KB |
Output is correct |
30 |
Correct |
760 ms |
20884 KB |
Output is correct |
31 |
Correct |
784 ms |
20180 KB |
Output is correct |
32 |
Correct |
395 ms |
34304 KB |
Output is correct |
33 |
Correct |
340 ms |
33612 KB |
Output is correct |
34 |
Correct |
304 ms |
32344 KB |
Output is correct |
35 |
Correct |
317 ms |
32192 KB |
Output is correct |
36 |
Correct |
383 ms |
35292 KB |
Output is correct |
37 |
Correct |
408 ms |
34764 KB |
Output is correct |
38 |
Correct |
423 ms |
35652 KB |
Output is correct |
39 |
Correct |
350 ms |
34476 KB |
Output is correct |
40 |
Correct |
372 ms |
34196 KB |
Output is correct |
41 |
Correct |
350 ms |
33700 KB |
Output is correct |
42 |
Correct |
336 ms |
32800 KB |
Output is correct |
43 |
Correct |
387 ms |
36280 KB |
Output is correct |
44 |
Correct |
393 ms |
36332 KB |
Output is correct |
45 |
Correct |
386 ms |
35424 KB |
Output is correct |
46 |
Correct |
349 ms |
33856 KB |
Output is correct |
47 |
Correct |
346 ms |
32808 KB |
Output is correct |
48 |
Correct |
327 ms |
32720 KB |
Output is correct |
49 |
Correct |
365 ms |
33988 KB |
Output is correct |
50 |
Correct |
341 ms |
34744 KB |
Output is correct |
51 |
Correct |
391 ms |
35232 KB |
Output is correct |
52 |
Correct |
401 ms |
34632 KB |
Output is correct |
53 |
Correct |
1425 ms |
19368 KB |
Output is correct |
54 |
Correct |
1371 ms |
19660 KB |
Output is correct |
55 |
Correct |
1816 ms |
20068 KB |
Output is correct |
56 |
Correct |
1985 ms |
20424 KB |
Output is correct |
57 |
Correct |
1380 ms |
21140 KB |
Output is correct |
58 |
Correct |
1132 ms |
19572 KB |
Output is correct |
59 |
Correct |
1371 ms |
22256 KB |
Output is correct |
60 |
Correct |
1314 ms |
20168 KB |
Output is correct |
61 |
Correct |
1069 ms |
19252 KB |
Output is correct |
62 |
Correct |
1049 ms |
20488 KB |
Output is correct |
63 |
Correct |
1360 ms |
20212 KB |
Output is correct |
64 |
Correct |
1130 ms |
21724 KB |
Output is correct |
65 |
Correct |
1302 ms |
19896 KB |
Output is correct |
66 |
Correct |
1599 ms |
20736 KB |
Output is correct |
67 |
Correct |
1486 ms |
19328 KB |
Output is correct |
68 |
Correct |
1822 ms |
22288 KB |
Output is correct |
69 |
Correct |
386 ms |
33996 KB |
Output is correct |
70 |
Correct |
396 ms |
32776 KB |
Output is correct |
71 |
Correct |
435 ms |
36000 KB |
Output is correct |
72 |
Correct |
408 ms |
19244 KB |
Output is correct |
73 |
Correct |
352 ms |
20052 KB |
Output is correct |
74 |
Correct |
349 ms |
21684 KB |
Output is correct |
75 |
Correct |
778 ms |
21296 KB |
Output is correct |
76 |
Correct |
854 ms |
21748 KB |
Output is correct |
77 |
Correct |
769 ms |
21324 KB |
Output is correct |
78 |
Correct |
805 ms |
21972 KB |
Output is correct |