Codeforces 839C Journey - 树形动态规划 - 数学期望

There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

Input

The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.

Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities connected by the i-th road.

It is guaranteed that one can reach any city from any other by the roads.

Output

Print a number — the expected length of their journey. The journey starts in the city 1.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples
input
4
1 2
1 3
2 4
output
1.500000000000000
input
5
1 2
1 3
3 4
2 5
output
2.000000000000000
Note

In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.

In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.


  题目大意 给定一棵有n个节点的树,某个人骑着一匹等概率走向任意相连的未经过节点的马从1号点开始旅行,当到某个点的无法移动旅行结束。求期望的旅行长度(每条边的长度为1)。

  显然到了某个点就不能到它的父节点。所以考虑动态规划。

  f[i]表示当到达节点i后,期望还能走的步数。显然某个叶节点的f值为0.

  现在考虑转移。

  显然是每个子节点的f值加1再乘走到这个节点的概率。

Code

 1 /**
 2  * Codeforces
 3  * Problem#839C
 4  * Accepted
 5  * Time: 78ms
 6  * Memory: 11800k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 
11 int n;
12 vector<int> *g;
13 double *f;
14 
15 inline void init() {
16     scanf("%d", &n);
17     g = new vector<int>[(n + 1)];
18     f = new double[(n + 1)];
19     for(int i = 1, u, v; i < n; i++) {
20         scanf("%d%d", &u, &v);
21         g[u].push_back(v);
22         g[v].push_back(u);
23     }
24 }
25 
26 void dfs(int node, int fa) {
27     f[node] = 0;
28     int count = 0;
29     for(int i = 0; i < (signed)g[node].size(); i++)
30         if(g[node][i] != fa)
31             count++;
32     if(!count)    return;
33     double P = 1.0 / count;
34     for(int i = 0; i < (signed)g[node].size(); i++) {
35         int& e = g[node][i];
36         if(e == fa)    continue;
37         dfs(e, node);
38         f[node] += (f[e] + 1) * P;
39     }
40 }
41 
42 inline void solve() {
43     printf("%.12lf", f[1]);
44 }
45 
46 int main() {
47     init();
48     dfs(1, 0);
49     solve();
50     return 0;
51 }
posted @ 2017-08-15 14:54  阿波罗2003  阅读(578)  评论(0编辑  收藏  举报