好题啊!很明显要从子树算一遍,然后从父亲算一遍
令
f
(
u
)
f(u)
f(u) 表示直接充上电或从子树充上电的概率
概率合并需要容斥一下
P
(
A
∣
B
)
=
P
(
A
)
+
P
(
B
)
−
P
(
A
)
∗
P
(
B
)
P(A|B)=P(A)+P(B)-P(A)*P(B)
P(A∣B)=P(A)+P(B)−P(A)∗P(B)
然后将
f
(
u
)
f(u)
f(u) 扩展到可以从父亲走过来
显然如果是从父亲走过来不能是自己走上去在走下来
也就是说概率是只考虑父亲以及父亲其他儿子的概率
P
(
A
)
=
P
(
A
∣
B
)
−
P
(
B
)
1
−
P
(
B
)
P(A)=\frac{P(A|B)-P(B)}{1-P(B)}
P(A)=1−P(B)P(A∣B)−P(B) 就是父亲从它的父亲以及其他儿子充上电的概率
然后从父亲走下来合并依然容斥合并
结合考察了树形 d p dp dp 的套路 及 概率 d p dp dp 容斥转移
#include<bits/stdc++.h>
#define cs const
using namespace std;
cs int N = 5e5 + 5;
int read(){
int cnt = 0, f = 1; char ch = 0;
while(!isdigit(ch)){ ch = getchar(); if(ch == '-') f = -1; }
while(isdigit(ch)) cnt = cnt*10 + (ch-'0'), ch = getchar();
return cnt * f;
}
int first[N], nxt[N << 1], to[N << 1], tot; double w[N << 1];
int n; double a[N], f[N];
void add(int x, int y, double z){nxt[++tot] = first[x], first[x] = tot, to[tot] = y, w[tot] = z; }
void dfs1(int u, int fa){
f[u] = a[u];
for(int i = first[u]; i; i = nxt[i]){
int t = to[i]; if(t == fa) continue;
dfs1(t, u); double p = w[i] * f[t];
f[u] = f[u] + p - f[u] * p;
}
}
void dfs2(int u, int fa){
for(int i = first[u]; i; i = nxt[i]){
int t = to[i]; if(t == fa) continue;
double pa = f[t] * w[i];
if(pa < 1){
double pc = (f[u] - pa) / (1 - pa);
double p = pc * w[i];
f[t] = f[t] + p - p * f[t];
} dfs2(t, u);
}
}
int main(){
n = read();
for(int i = 1; i < n; i++){
int x = read(), y = read(); double z = read(); z = z/100.0;
add(x, y, z); add(y, x, z);
}
for(int i = 1; i <= n; i++) a[i] = 1.0 * read() / 100.0;
dfs1(1, 0); dfs2(1, 0); double ans = 0;
for(int i = 1; i <= n; i++) ans += f[i];
printf("%.6lf", ans); return 0;
}