Non-column formulas for leaf nodes and non-leaf nodes.
Then, simplify the formula.
Similar to non-tree ..
# Include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> # include <queue> # include <math. h ># include <vector> using namespace STD; # define EPS 1e-9 # define zero (x) (FABS (x) <EPS? 0: X) # define maxn 11000 # define Pb push_backdouble A [maxn], B [maxn], C [maxn]; Double K [maxn], E [maxn], P [maxn]; vector <int> VEC [maxn]; int DFS (int x, int pre) {int M = VEC [X]. size (); double pp = 1.0 * P [x]/m; A [x] = K [X]; B [x] = pp; c [x] = P [X]; double TMP; TMP = 1.0; For (INT I = 0; I <m; I ++) {int y = VEC [x] [I]; If (y = pre) continue; If (! DFS (Y, x) return false; A [x] + = pp * A [y]; C [x] + = pp * C [y]; TMP-= pp * B [y];} If (FABS (TMP) <EPS) return false; A [x] = A [x]/tmp; B [x] = B [x]/tmp; C [x] = C [x]/tmp; return true;} int main () {int T, CAS; CAS = 0; scanf ("% d", & T); While (t --) {CAS ++; int n, x, y; scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) VEC [I]. clear (); For (INT I = 1; I <n; I ++) {scanf ("% d", & X, & Y ); VEC [X]. pb (y); VEC [Y]. pb (x) ;}for (INT I = 1; I <= N; I ++) {scanf ("% lf", & K [I], & E [I]); K [I] = K [I]/100.0; E [I] = E [I]/100.0; P [I] = 1-k [I]-E [I];} printf ("case % d:", CAS); If (DFS (1,-1) & FABS (A [1]-1)> EPS) {printf ("%. 6lf \ n ", C [1]/(1-A [1]);} else {puts (" impossible ") ;}} return 0 ;}