#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 10000 + 5;
#define eps 1e-10
int n;
double k[maxn],e[maxn];
double a[maxn],b[maxn],c[maxn];
vector<int> v[maxn];
bool dfs(int i,int fa)
{
if(v[i].size()==1&&fa!=-1)
{
a[i]=k[i];
b[i]=1-k[i]-e[i];
c[i]=1-k[i]-e[i];
return true;
}
a[i]=k[i];
b[i]=(1-k[i]-e[i])/v[i].size();
c[i]=1-k[i]-e[i];
double tmp=0;
for(int j=0;j<v[i].size();j++)
{
if(v[i][j]==fa) continue;
if(!dfs(v[i][j],i)) return false;
a[i]+=a[v[i][j]]*b[i];
c[i]+=c[v[i][j]]*b[i];
tmp+=b[v[i][j]]*b[i];
}
if(fabs(1-tmp)<eps) return false;
a[i]/=1-tmp;
b[i]/=1-tmp;
c[i]/=1-tmp;
return true;
}
int main()
{
int t;
int x,y;
scanf("%d",&t);
int kase=1;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) v[i].clear();
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++) scanf("%lf%lf",&k[i],&e[i]);
for(int i=1;i<=n;i++)
{
k[i]/=100.;
e[i]/=100.;
}
if(dfs(1,-1)&&fabs(1-a[1])>eps)
printf("Case %d: %.6lf\n",kase++,c[1]/(1-a[1]));
else
printf("Case %d: impossible\n",kase++);
}
return 0;
}
HDU 4035 Maze
最新推荐文章于 2021-03-04 12:40:09 发布