#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#define mp make_pair
using namespace std;
const int N=80010;
int n,m,tot,Next[N*2],head[N],tree[N*2],val[N*2],top,Stack[N],V[N],P,f[N][2],A[N];
bool vis[N],FLAG;
set<pair<int,int> >S;
void add(int x,int y,int z)
{
tot++;
Next[tot]=head[x];
head[x]=tot;
tree[tot]=y;
val[tot]=z;
}
void dfs(int u,int fa)
{
int lasttop=top;
for (int i=head[u];i;i=Next[i])
{
int v=tree[i];
if (v==fa) continue;
Stack[++top]=v;V[top]=val[i];
dfs(v,u);
if (FLAG) return;
}
int ans=0,cnt=0;
for (int i=lasttop+1;i<=top;i++)
{
if (f[Stack[i]][0]+V[i]>=P) ans++;
else A[++cnt]=f[Stack[i]][0]+V[i];
ans+=f[Stack[i]][1];
}
sort(A+1,A+cnt+1);
for (int i=1;i<=cnt;i++)
{
vis[i]=0;
S.insert(mp(A[i],i));
}
set<pair<int,int> >::iterator it;
while (!S.empty())
{
pair<int,int> x=*S.begin();
it=S.lower_bound(mp(P-x.first,0));
if (it!=S.end())
{
if (it==S.begin()) it++;
if (it!=S.end())
{
vis[x.second]=1;
vis[(*it).second]=1;
ans++;
S.erase(it);
}
}
S.erase(S.begin());
}
f[u][0]=0;
for (int i=cnt;i>=1;i--)
if (!vis[i]) { f[u][0]=A[i];break;}
f[u][1]=ans;
if (f[u][1]>=m) FLAG=1;
top=lasttop;
}
bool check(int x)
{
top=0;
P=x;
FLAG=0;
for (int i=1;i<=n;i++) f[i][0]=f[i][1]=0;
dfs(1,0);
return FLAG|(f[1][1]>=m);
}
void erfen(int l,int r)
{
while (l<r)
{
int mid=(l+r+1)>>1;
if (check(mid)) l=mid;
else r=mid-1;
}
printf("%d\n",l);
}
int main()
{
scanf("%d%d",&n,&m);
int sum=0;
for (int i=1;i<=n-1;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);add(y,x,z);
sum+=z;
}
erfen(0,sum/m+1);
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 17.78 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 19.89 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 22.75 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.13 ms | 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 180.55 ms | 2 MB + 856 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 92.912 ms | 2 MB + 172 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 88.983 ms | 2 MB + 856 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 154.602 ms | 4 MB + 696 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 642.76 us | 212 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 21.817 ms | 4 MB + 532 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 43.674 ms | 7 MB + 508 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 49.24 us | 64 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 44.98 us | 64 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 149.42 us | 68 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 137.91 us | 72 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 693.61 us | 116 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 836.06 us | 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 37.324 ms | 1 MB + 160 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 41.172 ms | 2 MB + 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 82.122 ms | 3 MB + 356 KB | Accepted | Score: 5 | 显示更多 |