#include<bits/stdc++.h>
using namespace std;
int n,m,c,head[100100],dis[100100],mid,rem;
bool vis[100100];
struct Edge{
int to,nxt,w;
}e[100100];
void read()
{
int m1,m2,m3;
cin>>n>>m;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&m1,&m2,&m3);
e[++c].to=m2;
e[c].nxt=head[m1];
head[m1]=c;
e[c].w=m3;
e[++c].to=m1;
e[c].nxt=head[m2];
head[m2]=c;
e[c].w=m3;
}
}
void dfs(int k,int fa)
{
if(rem<=0)return;
for(int i=head[k];i;i=e[i].nxt)
{
if(e[i].to==fa)continue;
dfs(e[i].to,k);
if(e[i].w+dis[e[i].to]>=mid)rem--,vis[i]=1;
}
//cout<<"GO1"<<endl;
multiset<int>mtst;
for(int i=head[k];i;i=e[i].nxt)
{
if(!vis[i] && e[i].to!=fa)mtst.insert(e[i].w+dis[e[i].to]);
}
//cout<<"GO2"<<endl;
multiset<int>::iterator iit;
for(multiset<int>::reverse_iterator it=mtst.rbegin();it!=mtst.rend();it++)
{
iit=mtst.lower_bound(mid-*it);
if(iit==mtst.end() || *iit>=*it)
{
dis[k]=*it;
break;
}
rem--;
mtst.erase(iit);
}
//cout<<"GO3"<<endl;
}
bool check()
{
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
rem=m;
dfs(1,1);
return rem<=0;
}
int main()
{
read();
int l=1,r=0x7fffffff-1,ans;
while(l<=r)
{
//printf("#%d,%d\n",l,r);
mid=(l+r)>>1;
if(check())ans=mid,l=mid+1;
else r=mid-1;
}
cout<<ans;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 386.03 us | 532 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 397.73 us | 532 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 403.05 us | 532 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 1.509 ms | 572 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 177.298 ms | 2 MB + 716 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 99.49 ms | 2 MB + 272 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 185.101 ms | 2 MB + 716 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 338.893 ms | 4 MB + 144 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #9 | 1.41 ms | 684 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 45.464 ms | 4 MB + 1008 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 80.768 ms | 7 MB + 976 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 429.9 us | 532 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 428.77 us | 532 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 581.95 us | 536 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 581.46 us | 544 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 1.59 ms | 584 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 1.881 ms | 572 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 63.853 ms | 1 MB + 392 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 98.377 ms | 2 MB + 260 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #20 | 165.967 ms | 3 MB + 304 KB | Wrong Answer | Score: 0 | 显示更多 |