# Codeforces123E. Maze【树形dp】【概率dp】【证明题】

## 题目大意

DFS(x)
if x == exit vertex then
finish search
flag[x] <- TRUE
random shuffle the vertices' order in V(x) // here all permutations have equal probability to be chosen
for i <- 1 to length[V] do
if flag[V[i]] = FALSE then
count++;
DFS(y);
count++;

## 思路

$\sum_{(u,v)\in E}siz_v*sump_v$
$sump_v$是子树内每个节点作为起始节点的概率和

#include<bits/stdc++.h>

using namespace std;

typedef double db;

const int N = 1e5 + 10;

int n, siz[N];
vector<int> g[N];
db p[N], q[N], sump = 0, sumq = 0, ans;

void dfs(int u, int fa) {
siz[u] = 1;
for (auto v : g[u]) {
if (v == fa) continue;
dfs(v, u);
siz[u] += siz[v];
p[u] += p[v];
ans += q[u] * siz[v] * p[v];
}
ans += q[u] * (n - siz[u]) * (sump - p[u]);
}

int main() {
scanf("%d", &n);
for (int i = 1; i < n; 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", &p[i], &q[i]);
sump += p[i], sumq += q[i];
}
dfs(1, 0);
printf("%.15lf", ans / (sump * sumq));
return 0;
} 
posted @ 2019-01-01 22:09  Dream_maker_yk  阅读(...)  评论(...编辑  收藏