#include<bits/stdc++.h>
using namespace std;
int getint(){
int ans=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
ans=ans*10+c-'0';
c=getchar();
}
return ans*f;
}
const int N=100010,M=N<<1;
struct bian{
int l,e,n;
};
bian b[M];
int s[N],tot=0;
void add(int x,int y,int z){
tot++;
b[tot].l=z;
b[tot].e=y;
b[tot].n=s[x];
s[x]=tot;
}
long long maxson[N];
int cnt=0,mid=0;
multiset<long long> sons;
void ss(int x,int fa){
//cout<<">>>"<<x<<endl;
for(int i=s[x];i;i=b[i].n){
if(b[i].e==fa)continue;
ss(b[i].e,x);
}
sons.clear();
for(int i=s[x];i;i=b[i].n){
if(b[i].e==fa)continue;
sons.insert(maxson[b[i].e]+b[i].l);
}
multiset<long long>::reverse_iterator it=sons.rbegin();
while(sons.size()){
if(*it>=mid){
cnt++;
sons.erase(sons.find(*it));
}else break;
}
while(sons.size()){
int t=*sons.begin();
sons.erase(sons.begin());
multiset<long long>::iterator itt=sons.lower_bound(mid-t);
if(itt==sons.end())maxson[x]=t;
else cnt++,sons.erase(itt);
}
//cout<<">> "<<x<<" "<<maxson[x]<<endl;
}
int main(){
int n=getint(),m=getint();
long long sum=0;
for(int i=0;i<n-1;i++){
int x=getint(),y=getint(),z=getint();
add(x,y,z);
add(y,x,z);
sum+=z;
}
long long l=0,r=sum/m+1;
while(l<r){
mid=(l+r+1)>>1;
cnt=0;
memset(maxson,0,sizeof(maxson));
ss(2,0);
if(cnt>=m)l=mid;
else r=mid-1;
//cout<<"---"<<mid<<"---"<<endl;
}
cout<<l;
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 330.72 us | 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 306.66 us | 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 313.9 us | 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 1.373 ms | 860 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 173.087 ms | 2 MB + 1004 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 98.752 ms | 2 MB + 456 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 92.249 ms | 2 MB + 1004 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 166.183 ms | 4 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 919.03 us | 928 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 22.916 ms | 3 MB + 912 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 40.359 ms | 5 MB + 976 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 336.87 us | 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 338.98 us | 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 447.35 us | 828 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 426.67 us | 832 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 965.09 us | 860 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 1.153 ms | 864 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 33.527 ms | 1 MB + 672 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 47.213 ms | 2 MB + 412 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 84.906 ms | 3 MB + 336 KB | Accepted | Score: 5 | 显示更多 |