解析:处理出公差和公比序列,然后尺取找出最大覆盖范围即可。
[code]:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
using namespace std;
const int maxn = 1e4+5;
const double eps = 1e-6;
struct Nod{
int b,next;
void init(int b,int next){
this->b=b;this->next=next;
}
}buf[2*maxn];
int n,len,E[maxn],deg[maxn],inque[maxn];
double A[maxn],B[maxn],dp[maxn];
queue<int> que;
void init(){
len = 0;
memset(E,-1,n*sizeof(int));
memset(deg,0,n*sizeof(int));
}
void add_edge(int a,int b){
deg[a]++;deg[b]++;
buf[len].init(b,E[a]);E[a]=len++;
buf[len].init(a,E[b]);E[b]=len++;
}
double get(int u){
double tmp = 1-A[u]-B[u],res = A[u]*dp[0]+tmp;
int i,v;
for(i = E[u];i != -1;i = buf[i].next){
v = buf[i].b;
res += tmp*dp[v]/deg[u];
}
return res;
}
bool spfa(){
int i,j,u,v,cnt = 0;
while(!que.empty()) que.pop();
for(i = 0;i < n;i++){
dp[i] = 0;
inque[i] = 1;
que.push(i);
}
while(!que.empty()){
u = que.front();que.pop();
inque[u] = 0;
if(fabs(dp[u]-get(u))<eps) continue;
dp[u] = get(u);cnt++;
if(cnt>5e5) return false;
for(i = E[u];i != -1;i = buf[i].next){
v = buf[i].b;
if(!inque[v]){
inque[v] = 1;
que.push(v);
}
}
}
return true;
}
int main(){
int i,j,cas,u,v;
scanf("%d",&cas);
for(int T=1;T<=cas;T++){
scanf("%d",&n);
init();
for(i = 1;i < n;i++){
scanf("%d%d",&u,&v);u--,v--;
add_edge(u,v);
}
for(i = 0;i < n;i++){
scanf("%d%d",&u,&v);
A[i] = u/100.0,B[i] = v/100.0;
}
printf("Case %d: ",T);
if(spfa()) printf("%.6f\n",dp[0]);
else printf("impossible\n");
}
return 0;
}