一、题目
二、解法
一看感觉就是贪心。
主要问题在于确定访问子树的顺序,先访问的子树的会给后访问的子树叠加边权和的两倍,设这个值为
f
f
f,但还要考虑到子树大小,因为子树中的每个点都会增加这么多,综上可知比较的方法为:
f
[
v
0
]
×
s
i
z
[
v
1
]
<
f
[
v
1
]
×
s
i
z
[
v
0
]
f[v_0]\times siz[v_1]<f[v_1]\times siz[v_0]
f[v0]×siz[v1]<f[v1]×siz[v0]然后就是据题意模拟统计即可。
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
const int M = 100005;
int read()
{
int x=0,flag=1;char c;
while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*flag;
}
int n,tot,f[M],f1[M],f2[M],siz[M];
struct edge
{
int v,c,next;
}e[2*M];
struct node
{
int x,y,z;
bool operator < (const node &r) const
{
return y*r.x<r.y*x;
}
};
void dfs(int u,int fa)
{
vector<node> t;siz[u]=1;
for(int i=f[u];i;i=e[i].next)
{
int v=e[i].v,c=e[i].c;
if(v==fa) continue;
dfs(v,u);
f1[u]+=f1[v];siz[u]+=siz[v];
t.push_back(node{siz[v],f2[v]+2*c,c});
}
sort(t.begin(),t.end());
for(int i=0;i<t.size();i++)
{
f1[u]+=(f2[u]+t[i].z)*t[i].x;
f2[u]+=t[i].y;
}
}
signed main()
{
n=read();
for(int i=1;i<n;i++)
{
int u=read(),v=read(),c=read();
e[++tot]=edge{v,c,f[u]},f[u]=tot;
e[++tot]=edge{u,c,f[v]},f[v]=tot;
}
dfs(1,0);
printf("%.10lf\n",1.0*f1[1]/(n-1));
}