比赛 cdcqの省选膜你赛 评测结果 WWWWWAWWWWWAWWAWTTTT
题目名称 秘术「天文密葬法」 最终得分 15
用户昵称 liaoy 运行时间 17.017 s
代码语言 C++ 内存使用 1.60 MiB
提交时间 2017-04-11 19:47:01
显示代码纯文本
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int none=1e9+7;
const double eps=1e-4;
char ch; bool flr;
inline void read(int &a){
	flr=1;
	for(ch=getchar();ch>'9'||ch<'0';ch=getchar())if(ch=='-')flr=0;
	for(a=0;ch<='9'&&ch>='0';ch=getchar())a=((a+(a<<2))<<1)+(ch^'0');
	if(!flr)a=-a;
}
int n,m;
int A[30010],B[30010];
vector<int> Arre[30010];
double ans;
double val[30010];
int sig,nowmin,nowpos;
int Fm[30010],Size[30010];
bool cut[30010];
void focusDFS(int f,int x){
	int v; Size[x]=1; Fm[x]=0;
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]]&&Arre[x][v]!=f){
		focusDFS(x,Arre[x][v]); Size[x]+=Size[Arre[x][v]]; Fm[x]=max(Fm[x],Size[Arre[x][v]]);
	}
	Fm[x]=max(Fm[x],sig-Fm[x]);
	if(Fm[x]<nowmin){
		nowmin=Fm[x]; nowpos=x;
	}
}
int Getw(int x,int siz){
	nowmin=1e5+7; sig=siz;
	focusDFS(0,x);
	return nowpos;
}
double Bucket[30010];
void Count(int f,int x,int dist,double nowval){
	if(dist>m)return;
	ans=min(ans,Bucket[m-dist]+nowval);
	int v;
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]]&&Arre[x][v]!=f){
		Count(x,Arre[x][v],dist+1,nowval+val[x]);
	}
}
void Put(int f,int x,int dist){
	if(dist>m)return;
	Bucket[dist]=min(Bucket[dist],val[x]);
	int v;
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]]&&Arre[x][v]!=f){
		Put(x,Arre[x][v],dist+1);
	}
}
void Cancel_Size(int f,int x,int dist){
	Bucket[dist]=none; Size[x]=1;
	int v;
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]]&&Arre[x][v]!=f){
		Cancel_Size(x,Arre[x][v],dist+1); Size[x]+=Size[Arre[x][v]];
	}
}
void Div(int x){
	//求val之和最小的长度为m的路径 
	int v; Bucket[0]=min(Bucket[0],val[x]);
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]]){
		Count(x,Arre[x][v],1,val[x]); Put(x,Arre[x][v],1);
	}
	Cancel_Size(0,x,0);
	cut[x]=1;
	for(v=0;v<Arre[x].size();v++)if(!cut[Arre[x][v]])Div(Getw(Arre[x][v],Size[Arre[x][v]]));
}
void linkDFS(int f,int x,int d){
	int v; if(d>nowmin){
		nowmin=d; nowpos=x;
	}
	for(v=0;v<Arre[x].size();v++)if(Arre[x][v]!=f)linkDFS(x,Arre[x][v],d+1);
}
int main(){
	int i,u,v;
	freopen("cdcq_b.in","r",stdin); freopen("cdcq_b.out","w",stdout);
	read(n); read(m); m--;
	for(i=1;i<=n;i++)read(A[i]);
	for(i=1;i<=n;i++)read(B[i]);
	for(i=1;i<n;i++){
		read(u); read(v); Arre[u].push_back(v); Arre[v].push_back(u);
	}
	if(m==-1){
		for(i=1;i<=n;i++)ans=max(ans,A[i]*1.0/B[i]);
		printf("%.2lf\n",ans);
	}
	nowmin=0; nowpos=1; linkDFS(0,1,0);
	nowmin=0; linkDFS(0,nowpos,0);
	if(nowmin<m){
		printf("-1\n"); return 0;
	}
	double tl=0,tr=2e5+7,mid;
	while(tr-tl>eps){
		mid=(tl+tr)/2.0;
		for(i=1;i<=n;i++)val[i]=A[i]-B[i]*mid,cut[i]=0,Bucket[i]=none;
		ans=none; Div(Getw(1,n));
		if(ans<0)tr=mid;else tl=mid;
	}
	printf("%.2lf\n",tl);
}