CF 101D Castle

D. Castle


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

const int N=100005;
typedef long long LL;
struct edge{
	int v,next,d;
}e[N*2];
int cnt=1,start[N];
LL num[N],len[N],curLen[N],totLen[N];//cur表示当前树的时间,tot表示累加的总时间
void add(int u,int v,int d){
	e[cnt].v=v;
	e[cnt].d=d;
	e[cnt].next=start[u];
	start[u]=cnt++;
}
int cmp(int a,int b){//a和b哪个先--总体个数是错的
	return curLen[a]*num[b]<curLen[b]*num[a];
}
void dfs(int fa,int x){
	num[x]=1;
	vector<int> son;
	for(int i=start[x];i;i=e[i].next){
		int y=e[i].v;
		if(y==fa) continue;
		len[y]=e[i].d;
		dfs(x,y);
		curLen[y]+=2*len[y];
		son.push_back(y);
	}
	sort(son.begin(),son.end(),cmp);
	for(int i=0;i<son.size();i++){
		int y=son[i];
		totLen[x]+=totLen[y]+num[y]*(curLen[x]+len[y]);//加上空闲时间(之前的子树的时间+进入y的时间)
		curLen[x]+=curLen[y];
		num[x]+=num[y];
	}
}
int main(){
	int n;
	cin>>n;
	for(int i=1,u,v,d;i<n;i++){
		scanf("%d %d %d",&u,&v,&d);
		add(u,v,d),add(v,u,d);
	}
	dfs(0,1);
	printf("%lf\n",totLen[1]*1.0/(n-1));
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值