# [HDU 4035] Maze

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define eps 1e-10//
4 #define maxn 10005
5 int cnt,v[maxn<<1],Next[maxn<<1],first[maxn];
6 int T,n,Case;
7 double K[maxn],E[maxn];
8 struct Xi{
9     double a,b,c,d;
10     Xi operator+(Xi t){
11         return (Xi){a+t.a,b+t.b,c+t.c,d+t.d};
12     }
13 };
15     v[++cnt]=end;
16     Next[cnt]=first[st];
17     first[st]=cnt;
18 }
19 Xi dfs(int x,int fa){
20     bool flag=true;
21     Xi ans,sum={0,0,0,fa?1.0:0.0};//
22     for(int e=first[x];e;e=Next[e])
23         if(v[e]!=fa){
24             flag=false;
25             sum=sum+dfs(v[e],x);
26         }
27     double re=1-K[x]-E[x];
28     if(flag)ans.a=K[x],ans.b=ans.c=re,ans.d=1;
29     else{
30         double p=1-re*sum.b/sum.d;
31         ans.a=(K[x]+re*sum.a/sum.d)/p;
32         ans.b=re/sum.d/p;
33         ans.c=(re*sum.c/sum.d+re)/p;
34         ans.d=1;
35     }
36     return ans;
37 }
38 bool eq(double x,double y){
39     return fabs(x-y)<eps;
40 }
41 int main(){
42     scanf("%d",&T);
43     while(T--){
44         scanf("%d",&n);
45         cnt=0,memset(first,0,sizeof(first));
46         int a,b;
47         for(int i=1;i<n;i++){
48             scanf("%d%d",&a,&b);
50         }
51         for(int i=1;i<=n;i++){
52             scanf("%lf%lf",&K[i],&E[i]);
53             K[i]/=100,E[i]/=100;
54         }
55         Xi ans=dfs(1,0);
56         printf("Case %d: ",++Case);
57         if(eq(ans.a,1))puts("impossible");
58         else printf("%.6lf\n",ans.c/(1-ans.a));
59     }
60     return 0;
61 }
View Code

posted @ 2016-05-25 13:05  Ngshily  阅读(143)  评论(0编辑  收藏  举报