比赛 cdcqの省选膜你赛 评测结果 WWTTTAWWWWWAWWAWWWWW
题目名称 秘术「天文密葬法」 最终得分 15
用户昵称 Marvolo 运行时间 9.405 s
代码语言 C++ 内存使用 14.05 MiB
提交时间 2017-04-11 21:53:03
显示代码纯文本
#pragma GCC optimize(3)
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<vector>
#define N 200010
#define M 1000010
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

int i,x,y,lsum=1,n,m;
int a[N],b[N],v[N],z[N],dis[N],head[N],d[N],fa[N];
double Ans=1000000000;

struct Line{
	int t,next;
}e[M];

inline double Min(double x,double y){return (x<y)?x:y;}
	
inline void Add(int s,int t){
	e[lsum].t=t;	e[lsum].next=head[s];	head[s]=lsum++;
}

inline void TP(){
	int i=0,t=1,w=0,x=0,mx=0,p=0;
	z[1]=v[1]=1;
	while (t!=w){
		w++;	x=z[w];
		for (i=head[x];i;i=e[i].next)
			if (!v[e[i].t]){
				v[e[i].t]=1;	dis[e[i].t]=dis[x]+1;
				z[++t]=e[i].t;
			}
	}
	for (i=1;i<=n;i++)	(dis[i]>mx)?mx=dis[i],p=i:0;
	memset(dis,0,sizeof(dis));	memset(v,0,sizeof(v));
	z[1]=p;	v[p]=1;
	t=1;	w=0;
	while (t!=w){
		w++;	x=z[w];
		for (i=head[x];i;i=e[i].next)
			if (!v[e[i].t]){
				v[e[i].t]=1;	dis[e[i].t]=dis[x]+1;
				z[++t]=e[i].t;
			}
	}
	mx=0;
	for (i=1;i<=n;i++)	mx=(dis[i]>mx)?dis[i]:mx;
	if (mx<m)	{printf("-1\n");	exit(0);}
}

inline void Maketree(int x){
	int i=0;
	v[x]=1;
	for (i=head[x];i;i=e[i].next)
	if (!v[e[i].t])	{
		fa[e[i].t]=x;	d[e[i].t]=d[x]+1;	Maketree(e[i].t);
	}
}

inline void Work(){
	int i=0,j=0,x=0,y=0,cnt=0,A=0,B=0,g=0;
	memset(v,0,sizeof(v));
	Maketree(1);	m++;
	for (i=1;i<n;i++)
		for (j=i+1;j<=n;j++){
			x=i;	y=j;	cnt=0;
			memset(v,0,sizeof(v));
			while (x!=y){
				v[x]=v[y]=1;
				if (d[x]==d[y])	{x=fa[x];	y=fa[y];}
				(d[x]>d[y])?x=fa[x]:y=fa[y];
			}
			A=0;	B=0;
			for (g=1;g<=n;g++)	(v[g])?A+=a[g],B+=b[g],cnt++:0;
			if (cnt!=m)	continue;
			Ans=Min(Ans,(double)(A*1.0/B));
		}
	printf("%.2lf\n",Ans);
}

int main(){
	freopen("cdcq_b.in","r",stdin);
	freopen("cdcq_b.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)	scanf("%d",&a[i]);
	for (i=1;i<=n;i++)	scanf("%d",&b[i]);
	for (i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		Add(x,y);	Add(y,x);
	}
	m--;	TP();
	if (n>10000)	{printf("0.00\n");	return 0;}
	else	Work();
	return 0;
}