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

 /**
* Codeforces
* Problem#839C
* Accepted
* Time: 78ms
* Memory: 11800k
*/
#include <bits/stdc++.h>
using namespace std; int n;
vector<int> *g;
double *f; inline void init() {
scanf("%d", &n);
g = new vector<int>[(n + )];
f = new double[(n + )];
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
} void dfs(int node, int fa) {
f[node] = ;
int count = ;
for(int i = ; i < (signed)g[node].size(); i++)
if(g[node][i] != fa)
count++;
if(!count) return;
double P = 1.0 / count;
for(int i = ; i < (signed)g[node].size(); i++) {
int& e = g[node][i];
if(e == fa) continue;
dfs(e, node);
f[node] += (f[e] + ) * P;
}
} inline void solve() {
printf("%.12lf", f[]);
} int main() {
init();
dfs(, );
solve();
return ;
}

Codeforces 839C Journey - 树形动态规划 - 数学期望的更多相关文章

  1. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  2. Codeforces 839C Journey【DFS】

    C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...

  3. Codeforces 1000G Two-Paths 树形动态规划 LCA

    原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...

  4. Codeforces Round #259(div2)C(数学期望)

    数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...

  5. CodeForces Div1: 995 D. Game(数学期望)

    Allen and Bessie are playing a simple number game. They both know a function f:{0,1}n→Rf:{0,1}n→R, i ...

  6. [Codeforces 839C] Journey

    [题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc ...

  7. Codeforces 912 质因数折半 方格数学期望

    A B #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #d ...

  8. 动态规划之经典数学期望和概率DP

    起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...

  9. 【BZOJ2134】单位错选(数学期望,动态规划)

    [BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...

随机推荐

  1. 进入Linux单用户模式

    1.       在启动系统出现如下画面时按Enter键进入系统设置页面: 2.       系统设置页面: 3.       按下e键进入: 4.       选择第二项,按e键进入 5.      ...

  2. 《A Structured Self-Attentive Sentence Embedding》(注意力机制)

    Background and Motivation: 现有的处理文本的常规流程第一步就是:Word embedding.也有一些 embedding 的方法是考虑了 phrase 和 sentence ...

  3. sitecore系统教程之Item快速了解

    项目是Sitecore网站的基本构建块.项目可以表示构成网页的任何类型的信息,例如,一段文本,媒体文件,布局等. 项目始终具有名称,唯一标识项目的ID,并且它基于定义项目包含的字段的模板.此外,项目可 ...

  4. canvas添加水印

    <canvas id="canvas"></canvas><canvas id="water"></canvas> ...

  5. 功能的显著性分析——GO Enrichment Analysis

      Gene Ontology(GO)是基因功能国际标准分类体系.GO富集分析是对差异基因等按GO分类,并对分类结果进行基于离散分布的显著性分析.错判率分析.富集度分析,得到与实验目的有显著联系的.低 ...

  6. 了解一下UTF-16

    1)先啰嗦一下 UTF-16是一种编码格式.啥是编码格式?就是怎么存储,也就是存储的方式. 存储啥?存二进制数字.为啥要存二进制数字? 因为Unicode字符集里面把二进制数字和字符一一对应了,存二进 ...

  7. python 创建二维数组的方法

    废话不多说,直接上代码: #coding=utf-8 def two_di_demo1(): a=[] for i in range(10): a.append([]) for j in range( ...

  8. 设计模式之Proxy(代理)(转)

    理解并使用设计模式,能够培养我们良好的面向对象编程习惯,同时在实际应用中,可以如鱼得水,享受游刃有余的乐趣. Proxy是比较有用途的一种模式,而且变种较多,应用场合覆盖从小结构到整个系统的大结构,P ...

  9. js异步计时器

    js中同步和异步的区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 何时需要异步 1.在可能发生等待的情况 2.等待过程中不能像 alert 一样阻塞程 ...

  10. flask模板应用-自定义错误页面

    自定义错误页面 当程序返回错误响应时,会渲染一个默认的错误页面,我们可以注册错误处理函数来处理错误页面 错误处理函数和视图函数很相似,返回值将作为响应的主题,因此我们先要创建错误页面的模板文件.为了和 ...