/*
lxhgww被困在迷宫里,迷宫是一棵n顶点的树,lxhgww初始在点1。每个点三种可能
1.被杀,回到起点1(概率为k[i])
2.逃脱,即逃出迷宫(概率为e[i])
3.随机的走一条与改点相连的边(包括它与它父亲相连的那条边
求逃出迷宫期望的步数。
设E[i]表示在结点i时期望的步数, fi表示点i的父亲, m表示点i的度数,j表示i的子节点
当i为叶子结点时,E[i] = k[i] * E[1] + e[i] * 0 + (1-k[i]-e[i]) * (E[fi] + 1)
= k[i] * E[1] + (1-k[i]-e[i]) * E[fi] + (1-k[i]-e[i])
当i为非叶子节点时, E[i] = k[i] * E[1] + e[i] * 0 + (1-k[i]-e[i]) * ( (E[fi]+∑E[j]) / m + 1 )
= k[i] * E[1] + (1-k[i]-e[i])/m * E[fi] + (1-k[i]-e[i]) / m * ( ∑E[j] + m )
令E[i] = A[i] * E[1] + B[i] * E[fi] + C[i]
当i为叶子结点时, A[i] = k[i], B[i] = (1-k[i]-e[i]), C[i] = (1-k[i]-e[i])
当i为非叶子结点时, E[i] = k[i] * E[1] + (1-k[i]-e[i]) * ( (E[fi] + ∑(A[j]*E[1] + B[j]*E[i] + C[j])) / m + 1 )
= (k[i]+(1-k[i]-e[i])/m*∑A[j]) * E[1] + (1-k[i]-e[i])/m * E[fi] + ((1-k[i]-e[i])/m*∑B[j]) * E[i] + (∑C[j]+m)*(1-k[i]-e[i])/m;
令(1-k[i]-e[i])/m*∑B[j] = X, AA = (k[i]+(1-k[i]-e[i])/m*∑A[j]), BB = (1-k[i]-e[i])/m, CC = (∑C[j]+m)*(1-k[i]-e[i])/m
则E[i] = AA * E[1] + BB * E[fi] + X * E[i] + CC
= AA/(1-X) * E[1] + BB/(1-X) * E[fi] + CC/(1-x)
因此, A[i] = AA/(1-X), B[i] = BB/(1-X), C[i] = CC/(1-x)
逆推出A[1], B[1], C[1],即可得到答案
*/
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1e4 + 10;
const double eps = 1e-12;
double A[maxn], B[maxn], C[maxn], dp[maxn];
double K[maxn], E[maxn];
vector<int> G[maxn];
int n;
void Cal(int u, int fa)
{
double m = (1-K[u]-E[u]) / G[u].size();
if(G[u].size() == 1 && G[u][0] == fa) {
A[u] = K[u];
B[u] = (1-K[u]-E[u]);
C[u] = (1 - K[u] - E[u]);
return;
}
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(v == fa) continue;
Cal(v, u);
}
double a = 0, b = 0, c = 0, x;
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(v == fa) continue;
a += A[v]; b += B[v]; c += C[v];
}
b *= m;
x = 1 - b;
A[u] = (K[u] + m*a) / x;
B[u] = m / x;
C[u] = (c + G[u].size()) * m / x;
}
int main()
{
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i <= n-1; i++) {
int u, v;
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;
}
Cal(1, 0);
printf("Case %d: ", kase);
if(1 - A[1] < eps) printf("impossible\n");
else printf("%.6f\n", C[1] / (1.0 - A[1]));
}
return 0;
}
/*
lxhgww被困在迷宫里,迷宫是一棵n顶点的树,lxhgww初始在点1。每个点三种可能
1.被杀,回到起点1(概率为k[i])
2.逃脱,即逃出迷宫(概率为e[i])
3.随机的走一条与改点相连的边(包括它与它父亲相连的那条边
求逃出迷宫期望的步数。
设E[i]表示在结点i时期望的步数, fi表示点i的父亲, m表示点i的度数,j表示i的子节点
当i为叶子结点时,E[i] = k[i] * E[1] + e[i] * 0 + (1-k[i]-e[i]) * (E[fi] + 1)
= k[i] * E[1] + (1-k[i]-e[i]) * E[fi] + (1-k[i]-e[i])
当i为非叶子节点时, E[i] = k[i] * E[1] + e[i] * 0 + (1-k[i]-e[i]) * ( (E[fi]+∑E[j]) / m + 1 )
= k[i] * E[1] + (1-k[i]-e[i])/m * E[fi] + (1-k[i]-e[i]) / m * ( ∑E[j] + m )
令E[i] = A[i] * E[1] + B[i] * E[fi] + C[i]
当i为叶子结点时, A[i] = k[i], B[i] = (1-k[i]-e[i]), C[i] = (1-k[i]-e[i])
当i为非叶子结点时, E[i] = k[i] * E[1] + (1-k[i]-e[i]) * ( (E[fi] + ∑(A[j]*E[1] + B[j]*E[i] + C[j])) / m + 1 )
= (k[i]+(1-k[i]-e[i])/m*∑A[j]) * E[1] + (1-k[i]-e[i])/m * E[fi] + ((1-k[i]-e[i])/m*∑B[j]) * E[i] + (∑C[j]+m)*(1-k[i]-e[i])/m;
令(1-k[i]-e[i])/m*∑B[j] = X, AA = (k[i]+(1-k[i]-e[i])/m*∑A[j]), BB = (1-k[i]-e[i])/m, CC = (∑C[j]+m)*(1-k[i]-e[i])/m
则E[i] = AA * E[1] + BB * E[fi] + X * E[i] + CC
= AA/(1-X) * E[1] + BB/(1-X) * E[fi] + CC/(1-x)
因此, A[i] = AA/(1-X), B[i] = BB/(1-X), C[i] = CC/(1-x)
逆推出A[1], B[1], C[1],即可得到答案
*/
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1e4 + 10;
const double eps = 1e-12;
double A[maxn], B[maxn], C[maxn], dp[maxn];
double K[maxn], E[maxn];
vector<int> G[maxn];
int n;
void Cal(int u, int fa)
{
double m = (1-K[u]-E[u]) / G[u].size();
if(G[u].size() == 1 && G[u][0] == fa) {
A[u] = K[u];
B[u] = (1-K[u]-E[u]);
C[u] = (1 - K[u] - E[u]);
return;
}
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(v == fa) continue;
Cal(v, u);
}
double a = 0, b = 0, c = 0, x;
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(v == fa) continue;
a += A[v]; b += B[v]; c += C[v];
}
b *= m;
x = 1 - b;
A[u] = (K[u] + m*a) / x;
B[u] = m / x;
C[u] = (c + G[u].size()) * m / x;
}
int main()
{
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++)
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i <= n-1; i++) {
int u, v;
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;
}
Cal(1, 0);
printf("Case %d: ", kase);
if(1 - A[1] < eps) printf("impossible\n");
else printf("%.6f\n", C[1] / (1.0 - A[1]));
}
return 0;
}