#include<cstdio>
#include<set>
using namespace std;
const int N=5e4+50;
int fir[N],las[N<<1],to[N<<1],ds[N<<1],cnt;
inline void add_edge(int u,int v,int a){
to[++cnt]=v;las[cnt]=fir[u];fir[u]=cnt;ds[cnt]=a;
to[++cnt]=u;las[cnt]=fir[v];fir[v]=cnt;ds[cnt]=a;
}
inline int min(int u,int v){return u<v?u:v;}
inline int max(int u,int v){return u>v?u:v;}
int n,m,x,y,z,l,r,mid,Ans;
multiset<int>s[N];
typedef multiset<int>::iterator IT;
int dfs(int u,int v){
int qwq=0,qvq=0;
IT it;
for(int i=fir[u];i;i=las[i])
if(to[i]!=v){
qvq=dfs(to[i],u)+ds[i];
if(qvq>=mid)++Ans;
else s[u].insert(qvq);
}
while(s[u].size()){
qvq=*s[u].begin();
s[u].erase(s[u].begin());
it=s[u].lower_bound(mid-qvq);
if(it!=s[u].end()){
Ans++;
s[u].erase(it);
}else qwq=qvq;
}
// if(s[u].size()){
// if(*s[u].begin()>=mid)Ans++;
// else qwq=*s[u].begin();
// s[u].clear();
// }
return qwq;
}
bool ok(){
Ans=0;
dfs(1,0);
return Ans>=m;
}
int main(){
scanf("%d%d",&n,&m);
l=2e9;
for(int i=1;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,z);
l=min(l,z);
r+=z;
}
r/=m;
while(l<r){
mid=l+r+1>>1;
if(ok())l=mid;
else r=mid-1;
}
printf("%d\n",l);
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 404.45 us | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 413.87 us | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 412.48 us | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.189 ms | 2 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 172.7 ms | 4 MB + 520 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 90.824 ms | 4 MB + 28 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 86.805 ms | 4 MB + 520 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 141.195 ms | 5 MB + 964 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 868.5 us | 2 MB + 460 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 18.754 ms | 5 MB + 900 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 38.179 ms | 8 MB + 232 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 432.89 us | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 429.22 us | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 493.54 us | 2 MB + 344 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 490.6 us | 2 MB + 352 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 916.68 us | 2 MB + 388 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.029 ms | 2 MB + 384 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 32.652 ms | 3 MB + 192 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 45.587 ms | 3 MB + 920 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 77.782 ms | 4 MB + 928 KB | Accepted | Score: 5 | 显示更多 |