题解:点击打开链接
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algorithm> #include <map> #include <cmath> using namespace std; const double eps = 1e-9; const int N = 10010; vector<int> G[N]; int n; double k[N], e[N], dp[N]; double A[N], B[N], C[N]; bool dfs(int u, int fa){ int m = G[u].size(); A[u] = k[u]; B[u] = (1 - k[u] - e[u])/m; C[u] = 1 - k[u] - e[u]; double tmp = (1 - k[u] - e[u]) / (double)m, self = 0; for(int i = G[u].size()-1; i >= 0; i--){ int v = G[u][i]; if(v == fa)continue; if(!dfs(v, u)) return false; A[u] += tmp * A[v]; self += tmp * B[v]; C[u] += tmp * C[v]; } if(fabs(self - 1.0) < eps) return false; A[u] /= 1-self; B[u] /= 1-self; C[u] /= 1-self; return true; } void input(){ cin>>n; for(int i = 1, u, v; i < n; i++){ scanf("%d %d", &u, &v); G[u].push_back(v); G[v].push_back(u); } for(int i = 1; i <= n; i++) { scanf("%lf %lf", &k[i], &e[i]); k[i] /= 100.0; e[i] /= 100.0; } } int main() { int T, Cas = 1; cin>>T; while(T--){ input(); bool ok = dfs(1, -1); printf("Case %d: ", Cas++); if(!ok || fabs(1.0-A[1]) < eps) puts("impossible"); else printf("%.6f\n", C[1] / (1.0-A[1])); for(int i = 1; i <= n; i++) G[i].clear(); } return 0; }
时间: 2024-10-12 19:09:36