Title: http://www.lydsy.com/JudgeOnline/problem.php?id=3566
First of all, the problem is to think about, consider complementary set transformation.
DFS first, so that f[u]= (1-p[u]) *∏ (n (1-f[v]) *w) F[u] means that the point of you through its sub-tree and can not be connected to the probability.
Then consider v from his father connected to the situation, set x=1-f[u]/(1-f[v]) *w to remove the probability of V,u Unicom, then f[v]*= (1-x*w)
And then add up is the answer.
#include <cstring>#include<iostream>#include<algorithm>#include<cstdio>#defineMAXN 500500#defineRep (i,l,r) for (int i=l;i<=r;i++)#defineDown (i,l,r) for (int i=l;i>=r;i--)#defineCLR (x, y) memset (x,y,sizeof (×))#defineEPS 1e-8using namespacestd;structdata{intObj,pre;DoubleC;} E[MAXN*2];intHead[maxn],tot,n;DoubleF[MAXN],ANS,P[MAXN];intRead () {intx=0, f=1;CharCh=GetChar (); while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=GetChar ();} while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}voidInsertintXintYDoublez) {e[++tot].obj=y; E[tot].c=z; E[TOT].PRE=HEAD[X]; head[x]=tot;}voidDfsintUintFA) {F[u]=1-P[u]; for(intj=head[u];j;j=e[j].pre) { intv=E[j].obj; if(v!=FA) {DFS (V,U); F[u]=f[u]* (1-(1-F[V]) *e[j].c); } }}voidGointUintFA) { for(intj=head[u];j;j=e[j].pre) { intv=E[j].obj; if(v!=FA) { Doublex=1-f[u]/(1-(1-F[V]) *e[j].c); if(x>eps&&f[v]>eps) f[v]=f[v]* (1-x*e[j].c); Go (v,u); } }}intMain () {n=read (); Rep (I,1, N-1){ intX=read (), Y=read (), z=read (); Insert (x, Y,1.0*z/ -); Insert (Y,x,1.0*z/ -); } Rep (I,1, N) {intX=read (); p[i]=1.0*x/ -;} DFS (1,0); Go (1,0); Rep (I,1, N) ans+=1-F[i]; printf ("%.6lf\n", ans); return 0;}
Bzoj 3566: [SHOI2014] Probability charger