This question is in three States at each position.
Return to the starting point of death: K [I]
Locate Exit End E [I]
Do not move P [I]
K [I] + E [I] + P [I] = 1;
Because we only gave n-1 links to connect all of them together, we can naturally regard this figure as a tree structure.
Based on Father's Day and leaf nodes
Derivation
For more information, see: http://blog.csdn.net/morgan_xww/article/details/6776947/
1 # include <cstdio> 2 # include <cstring> 3 # include <vector> 4 using namespace STD; 5 # define n 10005 6 # define del 1e-10 7 vector <int> G [N]; 8 double A [n], B [N], C [N], K [N], E [N], p [N]; 9 int N; 10 bool solve (int u, int FA) 11 {12 A [u] = K [u]; 13 B [u] = P [u]/g [u]. size (); 14 C [u] = P [u]; 15 16 if (G [u]. size () = 1 & Fa! = 0) 17 return true; 18 19 double TMP = 0; 20 for (INT I = 0; I <G [u]. size (); I ++) {21 Int J = G [u] [I]; 22 if (J! = FA) 23 {24 // This is both a judgment process and a process for finding leaf nodes. First, recursion is performed to update the leaf node as the underlying information, 25 // used to push the upper layer information 26 if (! Solve (J, u) 27 return false; 28 A [u] + = B [u] * A [J]; 29 C [u] + = B [u] * C [J]; 30 TMP + = B [u] * B [J]; 31} 32} 33 34 TMP = 1-tmp; 35 if (TMP <del) 36 return false; 37 38 A [u]/= TMP; 39 B [u]/= TMP; 40 C [u]/= TMP; 41 42 return true; 43} 44 45 int main () 46 {47 int t, a, B, c, d; 48 scanf ("% d", & T); 49 for (INT Kase = 1; Kase <= T; Kase ++) {50 scanf ("% d ", & N); 51 52 for (INT I = 1; I <= N; I ++) 53g [I]. clear (); 54 55 for (INT I = 1; I <n; I ++) {56 scanf ("% d % D ", & A, & B); 57G [A]. push_back (B); 58G [B]. push_back (a); 59} 60 for (INT I = 1; I <= N; I ++) {61 scanf ("% d", & C, & D); 62 k [I] = C * 1.0/100; 63 E [I] = D * 1.0/100; 64 p [I] = 1-k [I]-E [I]; 65} 66 67 printf ("case % d:", Kase); 68 69 If (! Solve (1, 0) | 1-A [1] <del) {70 printf ("impossible \ n"); 71 continue; 72} 73 74 printf ("%. 6f \ n ", C [1]/(1-A [1]); 75} 76}
HDU 4035 expectation DP