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 .

Example

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.

树形DP

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define INF 0x3f3f3f3f
#define MAXN 100009 vector<int> E[MAXN];
bool vis[MAXN];
int n;
double dp(int k,int len)
{
vis[k] = true;
double rate = 1.0, ans = ;
int cnt = ;
for (int i = ; i < E[k].size(); i++)
{
if (!vis[E[k][i]])
cnt++;
}
if (cnt == )
return len;
rate /= cnt;
for (int i = ; i < E[k].size(); i++)
{
if (vis[E[k][i]]) continue;
ans += dp(E[k][i], len + )*rate;
}
return ans;
}
int main()
{
scanf("%d", &n);
int f, t;
for (int i = ; i < n - ; i++)
{
scanf("%d%d", &f, &t);
E[f].push_back(t);
E[t].push_back(f);
}
printf("%.10lf\n", dp(, ));
}

Journey CodeForces - 839C的更多相关文章

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

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

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

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...

  3. Codeforces 839C Journey【DFS】

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

  4. [Codeforces 839C] Journey

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

  5. 【题解】Weird journey Codeforces 788B 欧拉路

    传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...

  6. Weird journey CodeForces - 788B (路径计数)

    大意:$n$结点$m$条边无向图, 满足 $(1)$经过$m-2$条边$2$次 $(2)$经过其余$2$条边$1$次 的路径为好路径, 求所有好路径数 相当于边加倍后再删除两条边, 求欧拉路条数 首先 ...

  7. codeforces721C

    Journey CodeForces - 721C Recently Irina arrived to one of the most famous cities of Berland — the B ...

  8. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  9. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

随机推荐

  1. TCP协议三次握手和四次握手

    前言 先说一下IP协议和TCP协议,IP协议是无连接的通信协议,IP不会占用两个设备之间通信的线路,IP实际上主要负责将每个数据包路由至目的地,但是IP协议并没有能够确保数据包是否到达,传过去的数据包 ...

  2. 建造者模式以及php实现

    建造者模式: 造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对 ...

  3. koa2实现简易的webpack-dev-server

    koa2实现简易的webpack-dev-server热更新 原文https://github.com/zhuangZhou/Blog/issues/3 闲来无事,用koa2撸了一个简易的webpac ...

  4. 在WIndowsPhone8 上制作的简单的计算器

    今天,闲着没事,就自己做了一个小小的计算器...虽说自己刚学wp8开发没多长时间,望大神多多指教..1.这是前台页面的代码 <Grid x:Name=" Margin="10 ...

  5. iOS循环引用

    iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...

  6. 手机端打开调试工具,模拟console.log

    将下列代码考入需要调试页面即可 <script src="//cdn.jsdelivr.net/npm/eruda"></script> <scrip ...

  7. SqlServer2012学习 - 基本数据类型认知

    精确数字: 1.整数 int是Sql Server主要整数类型.tinyint,smallint,int 不会自动转成bigint. 大于 2,147,483,647 的整数常量将转换为 decima ...

  8. https://quotefancy.com/ 经典句子(英语) 真是特别好~

    https://quotefancy.com/ 经典句子(英语)

  9. Mac OS 使用asio库

    下载地址:http://sourceforge.net/projects/asio/files/asio/1.12.2%20%28Stable%29/ 本人下载的版本:asio-1.12.2 1,本人 ...

  10. java解决动态的锁顺序死锁的方案

    直接上代码 public class Test3 { public static Object fromAccount = new String("1"); public stat ...