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. RMAN备份策略与异机恢复一例(续篇)

    本文是<RMAN备份策略与异机恢复一例>的续篇,继续实验验证,最终实现两个需求: 1.异机恢复临时测试的小库 2.传输归档时,实现增量传输 1.异机恢复临时测试的小库 之前异机恢复的需求已 ...

  2. js语法没有任何问题但是就是不走,检查js中命名的变量名,用 service-area错误,改service_area (原)

    js语法没有任何问题但是就是不走,检查js中命名的变量名,用 service-area错误,改service_area

  3. webapp定位

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. 从PHP官方镜像创建开发镜像

    https://xlange.com/post/dockerfile-baseon-official-php-image.html

  5. 解决乱码的方法是,在执行SQL语句之前,将MySQL以下三个系统参数设置为与服务器字符集character-set-server相同的字符集

    character-set-server/default-character-set:服务器字符集,默认情况下所采用的. character-set-database:数据库字符集. characte ...

  6. 依赖反转Ioc和unity,autofac,castle框架教程及比较

    1.依赖倒置的相关概念 http://www.cnblogs.com/fuchongjundream/p/3873073.html IoC模式(依赖.依赖倒置.依赖注入.控制反转) 2.依赖倒置的方式 ...

  7. Unity shader学习之逐顶点漫反射光照模型

    公式如下: Cdiffuse = Clight * mdiffuse * max(0, dot(n,l)); 其中,n 为表面法线,l 为指向光源的单位向量,mdiffuse 为材质温反射颜色,Cdi ...

  8. Discuz! 安装模板、插件提示“对不起,您安装的不是正版应用...

    iscuz 社区在更新到2.0以上后,增加了对插件的版本检测,在安装时,可能会出现:“对不起,您安装的不是正版应用,安装程序无法继续执行”的提示,要解决这个其实挺容易的,找到以下文件: /source ...

  9. Day10 Python网络编程 Socket编程

    一.客户端/服务器架构 1.C/S架构,包括: 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务)[QQ,SSH,MySQL,FTP] 2.C/S架构与socket的关系: 我们学习soc ...

  10. ==与Equals的作用

    string str1 = "Blackteeth"; string str2 = str1; string str3 = "Blackteeth"; Cons ...