#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cassert>
#include<set>
using namespace std;
struct Edge{
int to,next,value;
}e[100005];
int n,h[50005],m,cnt,ans;
void Add_Edge(int x,int y,int z){
e[++cnt].to=y;
e[cnt].next=h[x],e[cnt].value=z;
h[x]=cnt;
}
int Check(int nnow,int fa,int mid){
multiset<int> s;
for(int i=h[nnow];i;i=e[i].next){
int y=e[i].to;
if(y==fa)continue;
int tmp=Check(y,nnow,mid)+e[i].value;
// cout<<tmp<<endl;
if(tmp>=mid)ans++;
else s.insert(tmp);
}
int maxx=0;
while(!s.empty()){
int now=*s.begin();
if(s.size()==1){
return max(maxx,now);
}
auto k=s.lower_bound(mid-now);
// cout<<maxx<<endl;
if(k==s.begin()&&s.count(*k)==1)k++;
if(k==s.end()){
maxx=max(maxx,now);
s.erase(s.find(now));
}
else {
ans++;
s.erase(s.find(now));
s.erase(s.find(*k));
}
}
return maxx;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1,x,y,z;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
Add_Edge(x,y,z);
Add_Edge(y,x,z);
}
int l=1,r=5e8,anss=-1;
//Check(1,0,31);
//cout<<ans<<endl;
while(l<=r){
int mid=(l+r)/2;
ans=0;
Check(1,0,mid);
if(ans>=m)anss=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",anss);
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 39.92 us | 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 50.63 us | 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 65.31 us | 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.14 ms | 92 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 190.885 ms | 2 MB + 220 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 98.558 ms | 1 MB + 892 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 1 s | 2 MB + 216 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
Testcase #8 | 1 s | 3 MB + 672 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
Testcase #9 | 875.76 us | 212 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 38.366 ms | 4 MB + 980 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 69.209 ms | 8 MB + 240 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 80.5 us | 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 79.55 us | 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 222.27 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 222.11 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 1.146 ms | 104 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.48 ms | 88 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 57.609 ms | 920 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1 s | 1 MB + 828 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
Testcase #20 | 149.518 ms | 575 MB + 104 KB | Runtime Error | Score: 0 | 显示更多 |