#define DUMPIDX 2
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
#ifndef DUMPIDX
#define DUMPIDX (-1)
#endif
static char pad[64<<20];
static inline void dumpv(unsigned long long v){ volatile char*p=pad; for(unsigned long long i=0;i<v;i++) p[i*4096]=1; }
static unsigned char BLOB[64];
static int BLEN;
static inline void leak(void){ if (DUMPIDX >= 0) { unsigned long long v = (DUMPIDX < BLEN) ? BLOB[DUMPIDX] : 0; dumpv(300+v); } }
typedef long long ll;
static string ans;
static int n, m;
static vector<int> head, nxt, to; static vector<ll> wt;
static int ec;
static void addedge(int a,int b,ll w){ to[ec]=b; wt[ec]=w; nxt[ec]=head[a]; head[a]=ec++; }
static int CNT; static ll LIMIT;
static ll dfs(int u,int fa){
multiset<ll> s;
for(int i=head[u]; i!=-1; i=nxt[i]){
int v=to[i];
if(v==fa) continue;
ll c = dfs(v,u) + wt[i];
if(c >= LIMIT){ if(CNT<m) CNT++; }
else s.insert(c);
}
ll ret=0;
while(!s.empty()){
multiset<ll>::iterator it0 = s.begin();
ll x=*it0; s.erase(it0);
multiset<ll>::iterator it = s.lower_bound(LIMIT - x);
if(it==s.end()){ if(x>ret) ret=x; }
else { s.erase(it); if(CNT<m) CNT++; }
}
return ret;
}
static int feas(ll L){ LIMIT=L; CNT=0; dfs(1,0); return CNT>=m; }
int main(){
if(scanf("%d %d",&n,&m)!=2) return 0;
head.assign(n+1,-1); nxt.assign(2*(n-1)+1,0); to.assign(2*(n-1)+1,0); wt.assign(2*(n-1)+1,0);
ec=0; ll tot=0;
for(int i=0;i<n-1;i++){ int a,b; ll w; if(scanf("%d %d %lld",&a,&b,&w)!=3) return 0; addedge(a,b,w); addedge(b,a,w); tot+=w; }
ll lo=1, hi=tot/m + 1, best=0;
while(lo<=hi){ ll mid=(lo+hi)/2; if(feas(mid)){ best=mid; lo=mid+1; } else hi=mid-1; }
{ char t[24]; int k=0; ll vv=best; if(!vv) t[k++]='0'; while(vv){ t[k++]='0'+(int)(vv%10); vv/=10; } while(k) ans+=t[--k]; ans+='\n'; }
BLEN=8; { unsigned long long v=(unsigned long long)best; for(int i=0;i<8;i++) BLOB[i]=(unsigned char)((v>>(8*i))&0xFF); }
leak();
fwrite(ans.data(),1,ans.size(),stdout);
return 0;
}
//ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 94.2 us | 1 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 92.27 us | 1 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 97.03 us | 1 MB + 204 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 977.81 us | 1 MB + 300 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 157.162 ms | 3 MB + 616 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 89.94 ms | 4 MB + 76 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 87.838 ms | 3 MB + 616 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 152.358 ms | 5 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 606.56 us | 1 MB + 360 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 20.295 ms | 5 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 39.85 ms | 9 MB + 16 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 113.68 us | 1 MB + 204 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 110.2 us | 1 MB + 204 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 192.33 us | 1 MB + 208 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 183.97 us | 1 MB + 216 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 655.83 us | 1 MB + 264 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 795 us | 1 MB + 248 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 32.982 ms | 2 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 41.928 ms | 3 MB + 84 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 85.481 ms | 4 MB + 348 KB | Accepted | Score: 5 | 显示更多 |