#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define M 50010
using namespace std;
int n,m,num,mid,sum,ans,l,r,maxn,p;
int head[M],vis[M];
struct point{int next,to,dis;}e[M<<1];
struct node{int id,v;};
vector<node>S[M];
bool operator < (node a1,node a2){return a1.v<a2.v;}
void add(int from,int to,int dis)
{
e[++num].next=head[from];
e[num].to=to;
e[num].dis=dis;
head[from]=num;
}
int dfs(int x,int fa)
{
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
{
int l=dfs(e[i].to,x)+e[i].dis,to=e[i].to;
S[x].push_back((node){to,l});
}
int G=S[x].size();
sort(S[x].begin(),S[x].end());
for(int i=G-1;i>=0;i--)
{
if(S[x][i].v>=mid) sum++,vis[S[x][i].id]=true;
else break;
}
for(int i=0;i<G;i++)
{
int u=S[x][i].id,l=S[x][i].v;
if(vis[u]) continue;
for(int j=i+1;j<G;j++)
{
if(vis[S[x][j].id]) continue;
if(l+S[x][j].v<mid) continue;
vis[u]=vis[S[x][j].id]=true;
sum++;
break;
}
}
for(int i=G-1;i>=0;i--)
if(!vis[S[x][i].id])
return S[x][i].v;
return 0;
}
void DFS(int x,int fa,int now)
{
if(now>=maxn) maxn=now,p=x;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
DFS(e[i].to,x,now+e[i].dis);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++)
{
int a,b,c;scanf("%d%d%d",&a,&b,&c);
add(a,b,c),add(b,a,c);
r+=c;
}
r/=m;
while(l<=r)
{
for(int i=1;i<=n;i++)
S[i].clear();
memset(vis,false,sizeof(vis));
mid=(l+r)/2;
sum=0,dfs(1,1);
if(sum<m) r=mid-1;
else ans=mid,l=mid+1;
}
printf("%d\n",ans);
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 266.75 us | 1 MB + 388 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 266.98 us | 1 MB + 388 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 264.2 us | 1 MB + 388 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 1.025 ms | 1 MB + 448 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 1 s | 2 MB + 676 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #6 | 1 s | 2 MB + 976 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #7 | 1 s | 2 MB + 676 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #8 | 1 s | 3 MB + 604 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #9 | 709.82 us | 1 MB + 540 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 19.441 ms | 5 MB + 864 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 35.422 ms | 8 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 286.72 us | 1 MB + 392 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 283.7 us | 1 MB + 392 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 358.37 us | 1 MB + 396 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 352.25 us | 1 MB + 400 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 792.13 us | 1 MB + 452 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 864.77 us | 1 MB + 444 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 30.849 ms | 2 MB + 784 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 332.294 ms | 2 MB + 972 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 1 s | 4 MB + 224 KB | Time Limit Exceeded | Score: 0 | 显示更多 |