今天晚上思维比较乱,以后再写写吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+5,INF=1e9+5;
double eps=1e-4;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int n,L,U,a,b;
double w,maxVal,ans;
struct edge{
int v,ne;
double w;
}e[N<<1];
int h[N],cnt;
inline void ins(int u,int v,double w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int f[N],size[N],all,vis[N],root;
void dfsRt(int u,int fa){
size[u]=1;f[u]=0;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]||v==fa) continue;
dfsRt(v,u);
size[u]+=size[v];
f[u]=max(f[u],size[v]);
}
f[u]=max(f[u],all-size[u]);
if(f[u]<f[root]) root=u;
}
int q[N],head,tail,deep[N];
double val[N],mx[N];
bool visit[N];
void bfs(double g){
while(head<=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]) continue;
if(!visit[v]){
visit[v]=1;
q[++tail]=v;
deep[v]=deep[u]+1;
val[v]=val[u]+e[i].w-g;
}
}
}
}
double c[N];
int m;
bool dp(int n){//printf("dp %d %d
",n,n+m);
if(m+n<L) return false;
head=1;tail=0;
int l=max(L-n,1),r=U-n;
for(int i=l;i<=r&&i<=m;i++){//printf("ins %d
",i);
while(head<=tail&&c[q[tail]]<c[i]) tail--;
q[++tail]=i;
}
for(int i=n;i>=1;i--){//printf("i %d %lf
",i,mx[i]);
int l=L-i,r=U-i;//printf("lala %d %d
",l,r);
while(head<=tail&&q[head]<l) head++;
while(head<=tail&&c[r]>c[q[tail]]) tail--;
q[++tail]=r;
//printf("qqq %d %d %lf
",head,tail,c[q[head]]);
if(head<=tail&&c[q[head]]+mx[i]>=0) return true;
}
return false;
}
bool check(int u,double g){//printf("
check %d %lf
",u,g);
for(int i=1;i<=m;i++) c[i]=-INF;m=0;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(vis[v]) continue;
//printf("
v %d
",v);
head=1;tail=0;
q[++tail]=v;
deep[v]=1;val[v]=e[i].w-g;
bfs(g);
//for(int i=1;i<tail;i++) //printf("%lf ",val[i]);puts("");
for(int i=1;i<=deep[q[tail]];i++) mx[i]=-INF;
int id;
for(int i=1;i<=tail;i++)
id=q[i],mx[deep[id]]=max(mx[deep[id]],val[id]),visit[id]=0;
//for(int i=1;i<=maxDeep;i++) printf("mx %d %lf
",i,mx[i]);
if(dp(deep[q[tail]])) return true;
m=max(m,deep[q[tail]]);
for(int i=1;i<=deep[q[tail]];i++) c[i]=max(c[i],mx[i]);
}
return false;
}
void dfsSol(int u){//printf("dfsSol %d
",u);if(u!=1) return;
vis[u]=1;
double l=ans,r=maxVal;//printf("lr %lf %lf
",l,r);
while(r-l>eps){
double mid=(l+r)/2;
if(check(u,mid)) l=mid;
else r=mid;
}
ans=max(ans,l);
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v]){
root=0;all=size[e[i].v];
if(size[e[i].v]<L) continue;
dfsRt(e[i].v,0);
dfsSol(root);
}
}
int main(){
freopen("in","r",stdin);
//freopen("out","w",stdout);
n=read();L=read();U=read();
for(int i=1;i<n;i++)
a=read(),b=read(),w=read(),ins(a,b,w),maxVal=max(maxVal,w);
root=0;f[0]=INF;all=n;
dfsRt(1,0);
dfsSol(root);
printf("%.3lf",ans);
}