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. jquery + ajax 实现多条件查询

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JquerySort.aspx. ...

  2. 关于java的arrays数组排序示例AJPFX的分享

    Java API对Arrays类的说明是:此类包含用来操作数组(比如排序和搜索)的各种方法. 1.对基本数据类型的数组的排序 说明: (1)Arrays类中的sort()使用的是“经过调优的快速排序法 ...

  3. oracle 创建表

    --创建表 create table browser_track( btId number not null , opend_id ) not null, url_address ) not null ...

  4. filter 过滤器加载流程

    过滤器例子 <!--A过滤器--><filter> <filter-name>mdamptRightLimitFilter</filter-name> ...

  5. 掌握Spark机器学习库-05-spark中矩阵与向量的使用

    1)介绍 矩阵: Matrix,看做二维表,基本运算(+,-,*,T) 向量: Vectors,方向和大小,基本运算,范数 2)spark中向量的使用(主要使用breeze.linalg) 3)spa ...

  6. xmind8 Mac破解版(思维导图) 附序列号

    链接: https://pan.baidu.com/s/1PNdLRGpz_jhfPmWAIbLRfw 提取码: ruvm 复制这段内容后打开百度网盘手机App,操作更方便哦 小伙伴们XMind 8 ...

  7. R Programming week2 Control Structures

    Control Structures Control structures in R allow you to control the flow of execution of the program ...

  8. 读取.properties配置信息

    package com.ctcti.webcallcenter.utils; import java.io.FileInputStream;import java.io.FileNotFoundExc ...

  9. 面试必备【含答案】Java面试题系列(二

    1.写clone()方法时,通常都有一行代码,是什么?答:super.clone(),他负责产生正确大小的空间,并逐位复制. 2.GC 是什么? 为什么要有GC?答:GC 是垃圾收集的意思(Gabag ...

  10. thisInObject

    var k = { name:"kName" ,getName:function(){ console.info(this.name) } ,getParentName:funct ...