Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4035
Test instructions: A tree, starting from Node 1, at each node I have 3 possibilities: (1) Return to Node 1, probability Ki; (2) end, probability Ei; (3) randomly take an edge. (ki+ei+ random walk =1) to find the end of the desired number of sides to go.
Assuming E[i] is the expectation of the point I to the end of the walk, there is
(The following m is the degree of the point)
e[i]=ki*e[1]+ (1-ei-ki)/m* (e[fa[i]]+1) If I is a leaf node.
=Ki*e (1) + (1-ki-ei) *e (father) + (1-ki-ei)
e[i]=ki*e[1]+ (1-ei-ki)/m* (e[fa[i]]+1) + (1-ei-ki)/m* (Sum (e[son[i]]+1)) I is not a leaf node
=ki*e (1) + (1-ki-ei)/M *e (father) + (1-ki-ei)/m*sum (E (Child)) + (1-ki-ei) as 1
We find that this is very troublesome, if n small a large usable Gaussian elimination solution, but the problem of N is 10000, can not use Gaussian elimination.
For each e[i], we make e[i]=ai* (e[1]) +bi* (E[fa[i]]) +ci
E[child]=aj*e[1]+bj*e[i]+cj
Sum (E[child]) =sum (AJ*E[1]+BJ*E[I]+CJ)
Bring in 1: ki*e (1) + (1-ki-ei)/M *e (father) + (1-ki-ei)/m*sum (AJ*E[1]+BJ*E[I]+CJ) + (1-ki-ei)
Available: (ki+ (1-ki-ei)/m*sum (Aj)) *e (1) + (1-ki-ei)/M *e (father) + (1-ki-ei+ (1-ki-ei)/m*sum (CJ))
Compare with the e[i]=ai* (E[1]) +bi* (E[fa[i]) +ci:
Ai= (ki+ (1-ki-ei)/m*sum (Aj))
Bi= (1-ki-ei)/M
Ci= (1-ki-ei+ (1-ki-ei)/m*sum (CJ))
For leaf nodes, there are
Ai=ki
Bi=1-ki-ei
Ci=1-ki-ei
Backwards, and,
E (1) =a1*e (1) +b1*0+c1
E (1) =c1/(1-A1)
If the denominator in the above equation is 0, there is no solution.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <cmath>5#include <algorithm>6 Const Doubleeps=1e-9;7 inttot,go[500005],first[500005],next[500005];8 Doublea[500005],b[500005],c[500005],k[500005],e[500005];9 intn,du[500005];Ten voidInsertintXintY) {tot++;go[tot]=y;next[tot]=first[x];first[x]=tot;} One voidAddintXinty) {Insert (x, y); insert (y,x);} A BOOLDfsintXintFA) { - BOOLIsleave=1; - Doubletmp=0; thea[x]=K[x]; -B[x]=c[x]= (1-k[x]-e[x]); -B[x]/=Du[x]; - for(intI=first[x];i;i=Next[i]) { + intPur=Go[i]; - if(PUR==FA)Continue; +Isleave=0; A if(!dfs (pur,x))return false; ata[x]+=a[pur]* (1-K[X]-E[X])/Du[x]; -c[x]+=c[pur]* (1-K[X]-E[X])/Du[x]; -tmp+= (B[pur]) * (1-K[X]-E[X])/Du[x]; - } - if(Fabs (tmp-1) <=eps)return false; -A[x]/= (1-tmp); inB[x]/= (1-tmp); -C[x]/= (1-tmp); to return true; + } - intMain () { the intT,tcase=0; *scanf"%d",&T); $ while(t--){Panax Notoginsengtcase++; -tot=0; thememset (First,0,sizeofFirst ); +Memset (Du,0,sizeofdu); Ascanf"%d",&n); the for(intI=1; i<n;i++){ + intx, y; -scanf"%d%d",&x,&y); $ Add (x, y); $du[x]++; -du[y]++; - } the for(intI=1; i<=n;i++){ -scanf"%LF%LF",&k[i],&e[i]);WuyiK[i]/= -; e[i]/= -; the } -printf"Case %d:", Tcase); Wu if(Dfs (1,0) &&fabs (1-a[1]) >EPS) { -printf"%.6f\n", c[1]/(1-a[1])); About } $ Else{ -printf"impossible\n"); - } - } A}
HDU 4035 Maze (tree-shaped probability dp)