1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include<cstdio> #include<cstring> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std;
const int maxn=5005, maxL=1e5+5; const double eps=1e-8;
int n,L,v[maxn],T;
int tot,go[2*maxn],next[2*maxn],f1[maxn]; void ins(int x,int y) { go[++tot]=y; next[tot]=f1[x]; f1[x]=tot; }
double f[maxn][3],g[maxn],Mid; void dfs(int k,int last,int c) { double rl=((v[k]+c)%L)/Mid, fir=0, sec=0, sum=0; for(int p=f1[k]; p; p=next[p]) if (go[p]!=last) { dfs(go[p],k,c); sum+=g[go[p]]; double t=f[go[p]][1]-g[go[p]]; if (t-fir>eps) { sec=fir; fir=t; } else if (t-sec>eps) sec=t; } f[k][0]=sum; f[k][1]=sum+fir+rl; f[k][2]=sum+fir+sec+rl; g[k]=max(f[k][0],max(f[k][1]-1,f[k][2]-1)); }
bool check(double mid,double c) { Mid=mid; memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); dfs(1,0,c); return (g[1]>1); }
int vt[maxn],vt0; bool bz[maxL]; int main() { srand(101); scanf("%d %d",&n,&L); fo(i,1,n) scanf("%d",&v[i]); fo(i,1,n-1) { int x,y; scanf("%d %d",&x,&y); ins(x,y), ins(y,x); } scanf("%d",&T); vt[++vt0]=0; vt[++vt0]=T; fo(i,1,n) { v[i]%=L; if (L-1-v[i]<=T && !bz[v[i]]) vt[++vt0]=L-1-v[i], bz[v[i]]=1; } random_shuffle(vt+1,vt+1+vt0); double ans=0; fo(ci,1,vt0) { if (!check(ans,vt[ci])) continue; double l=0, r=(double)n*(L-1); while (l+eps<=r) { double mid=(l+r)/2; if (check(mid,vt[ci])) l=mid+eps; else r=mid-eps; } ans=max(ans,l-eps); } printf("%.7f\n",ans); }
|