题里那个直接充电和间接充电是一样的。。。
除0挂了半天还以为是卡精。。。
设
f[i]
表示i和i的子树使点i充电的概率。
设
g[i]
表示除掉i的子树的剩余的部分把i的父亲充电的概率。
转移看代码吧。。。。
#include <bits/stdc++.h>
using namespace std;
#define N 510000
#define eps 1e-15
int n,tot;
int head[N],nex[N<<1],to[N<<1],cnt[N];
double val[N<<1],a[N],f[N],g[N],f1[N];
double ans;
void add(int x,int y,int p)
{
tot++;
nex[tot]=head[x];head[x]=tot;
to[tot]=y;val[tot]=p/100.0;
}
void mul(int x,double v)
{
if(v<eps)f[x]*=v,cnt[x]++;
else f[x]*=v,f1[x]*=v;
}
double div(int x,double v)
{
if(v>eps)return f[x]/v;
if(cnt[x]>1)return 0;
return f1[x];
}
void dfs(int x,int y)
{
f[x]=f1[x]=1-a[x];
for(int i=head[x];i;i=nex[i])
if(to[i]!=y)
{
dfs(to[i],x);
mul(x,1-(1-f[to[i]])*val[i]);
}
}
void dfs1(int x,int y,double v)
{
double sum=(1-a[x])*(1-(1-g[x])*v);
for(int i=head[x];i;i=nex[i])
if(to[i]!=y)
{
g[to[i]]=(1-(1-g[x])*v)*div(x,(1-(1-f[to[i]])*val[i]));
sum*=1-(1-f[to[i]])*val[i];
dfs1(to[i],x,val[i]);
}
ans+=1-sum;
}
int main()
{
//freopen("tt.in","r",stdin);
scanf("%d",&n);
for(int i=1,x,y,p;i<n;i++)
{
scanf("%d%d%d",&x,&y,&p);
add(x,y,p);add(y,x,p);
}
for(int i=1,p;i<=n;i++)
scanf("%d",&p),a[i]=p/100.0;
dfs(1,0);
g[1]=1;dfs1(1,0,0);
printf("%.6lf\n",ans);
return 0;
}