#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=500010;
const ll INF=2147483647;
struct edge{
ll to,val;
}e[maxn];
ll n,m,head[maxn],nxt[maxn],tot,x,y,z,l,r,ans[maxn],len[maxn],fa[maxn],cnt;
bool vis[maxn];
multiset<ll>s;
multiset<ll>::iterator it;
void add_edge(ll x,ll y,ll z){
nxt[++tot] = head[x];
e[tot].to = y;
e[tot].val = z;
head[x] = tot;
}
void dfs(ll x,ll sum){
vis[x] = 1;
for(int i = head[x]; i;i = nxt[i]){
ll tem = e[i].to;
if(!vis[tem]){
fa[tem] = x;
dfs(tem,sum);
}
}
s.clear();
for(int i = head[x]; i;i = nxt[i]){
ll tem = e[i].to;
if(fa[tem] != x)continue;
if(e[i].val + len[tem] >= sum)ans[x]++;
else s.insert(e[i].val + len[tem]);
}
ll maxx = 0;
while(!s.empty()){
if(s.size() == 1){
maxx = max(maxx,*s.begin());
break;
}
it = s.lower_bound(sum - *s.begin());
if(it == s.begin() && s.count(*it) == 1)it++;
if(it == s.end()){
maxx = max(maxx,*s.begin());
s.erase(s.find(*s.begin()));
}
else{
ans[x]++;
s.erase(s.find(*it));
s.erase(s.find(*s.begin()));
}
}
len[x] = maxx;
cnt += ans[x];
return;
}
bool check(ll x){
cnt = 0;
memset(vis,0,sizeof(vis));
memset(ans,0,sizeof(ans));
dfs(1,x);
return (cnt >= m) ? true : false;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i = 1;i < n;i++){
cin>>x>>y>>z;
add_edge(x,y,z);
add_edge(y,x,z);
}
l = 1;
r = INF;
while(l < r - 1){
ll mid = (l + r)>>1;
if(check(mid)){
l = mid;
}
else r = mid;
}
cout<<l;
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 3.13 ms | 4 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 3.146 ms | 4 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 3.158 ms | 4 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 4.548 ms | 4 MB + 464 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 233.717 ms | 7 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 132.062 ms | 7 MB + 336 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 238.025 ms | 7 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 429.637 ms | 10 MB + 96 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 4.186 ms | 4 MB + 560 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 64.705 ms | 9 MB + 652 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 109.577 ms | 13 MB + 148 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 3.188 ms | 4 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 3.181 ms | 4 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 3.358 ms | 4 MB + 392 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 3.358 ms | 4 MB + 396 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 4.584 ms | 4 MB + 472 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 5.041 ms | 4 MB + 464 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 88.604 ms | 6 MB + 500 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 130.784 ms | 7 MB + 324 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 221.666 ms | 9 MB + 168 KB | Accepted | Score: 5 | 显示更多 |