C. Journey
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy
4
1 2
1 3
2 4
output
1.500000000000000
input

Copy
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个点,n-1条边,每个点不能重复走,求走的路长度的数学期望。

思路:E(x)=l[i]*p[i](每条路的长度*这条路的概率)。

代码:

 //#include"bits/stdc++.h"
#include <sstream>
#include <iomanip>
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"time.h"
#include"iostream"
#include"stdlib.h"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<n;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
vector<int> g[N];
bool v[N];
int n;
db ans=;
void dfs(int u,ll s,db p)
{
v[u]=;
db cnt=;
for(int i=;i<g[u].size();i++) if(!v[g[u][i]]) cnt++;
for(int i=;i<g[u].size();i++){
int x=g[u][i];
if(!v[x]) v[x]=1,dfs(x,s+,p/cnt),v[x]=0;
}
if(g[u].size()==&&v[g[u][]]==) ans+=s*p;
}
int main()
{
ci(n);
for(int i=;i<n;i++){
int x,y;
ci(x),ci(y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(,,1.0);
pd(ans);
return ;
}

最新文章

  1. .NET基础架构方法—DataTableToExcel通用方法
  2. 用java下载hdfs文件报NullPointerException
  3. Spring-----定时任务Quartz配置
  4. mongodb 释放磁盘空间
  5. CSS组件架构的设计思想
  6. MFC 实现字符串的移动
  7. Keepalived+tomcat的HA配置
  8. Github学习之路-小试牛刀,练习Git 的基本操作
  9. City Game
  10. 在linq查询环境下通过sql语句来访问数据库
  11. C语言库函数大全及应用实例十四
  12. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
  13. Python之多进程篇
  14. windows系统命令行
  15. Sublime怎么安装Package control组件
  16. redis cluster的conf配置文件配置
  17. HDU - 3973 AC&#39;s String(Hash+线段树)
  18. pandas 初识(一)
  19. ios中uitableview上拉刷新和下拉刷新(1)
  20. adnanh webhook 框架request values 说明

热门文章

  1. 【工作中学习】CreateProcessAsUser失败,错误码:1314
  2. Eclipse: 导入项目乱码问题解决
  3. 中兴ZXR10 GER4核心路由器配置案例
  4. HashMap扩容
  5. 【BZOJ1858】[SCOI2010] 序列操作(ODT裸题)
  6. Codeforces 758B Blown Garland
  7. Oracle数据库几种启动方式及查询当前状态
  8. ORA-01262,oracle启动报错,及Oracle启动原理
  9. Java继承和访问修饰符
  10. maven解析xml+测试test+注解