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 ;
}

Codeforces Round #428 (Div. 2) C. dfs的更多相关文章

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

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

  2. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  3. CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)

    血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...

  4. Codeforces Round #428 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...

  5. Codeforces Round #428 (Div. 2)E. Mother of Dragons

    http://codeforces.com/contest/839/problem/E 最大团裸题= =,用Bron–Kerbosch算法,复杂度大多博客上没有,维基上查了查大约是O(3n/3) 最大 ...

  6. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  8. Codeforces Round #290 (Div. 2) B (dfs)

    题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...

  9. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

随机推荐

  1. 移动端适配(1)——viewport设置与初始化css

    <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=0 ...

  2. 一张图看懂offsetX, clientX, pageX, screenX的区别

    1.具体含义见下图1 2.浏览器的兼任情况

  3. js随堂初体验(一)

    Js初体验(-) 1 js的基础知识 A web三大标准:1 html:结构标准    2 css:表现标准  3 javascript:行为标准 B js三种书写方式:1 行内js:onclick ...

  4. python+selenium第一个脚本

    #coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport tim ...

  5. 在 eclipse 中调出其内置的浏览器

    两种方法: 1.点击工具栏中的浏览器图标,就会在主面板中出现浏览器: 跳出一个blank页面,如下: 第二种方法:点击Window——Show view——Other 输入 "browser ...

  6. http缓存基本介绍

    https://www.helloweba.com/view-blog-414.html

  7. 【js基础修炼之路】- 微任务,宏任务和Event-Loop

    一段代码让你了解Event-Loop console.log(1); setTimeout(() => { console.log(2); }, 0); new Promise((resolve ...

  8. 【BZOJ4025】二分图(LCT动态维护图连通性)

    点此看题面 大致题意: 给你一张图以及每条边的出现时间和消失时间,让你求每个时间段这张图是否是二分图. 二分图性质 二分图有一个比较简单的性质,即二分图中不存在奇环. 于是题目就变成了:让你求每个时间 ...

  9. 关于VMware给系统分区扩容的一点经验

    我的VMware版本是8.0.6 build-1035888,里面安装的是Windows XP SP3 首先,在VM关机状态下使用Hard disk设置中的Utilities下的Expand给整个磁盘 ...

  10. 整个trick

    数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...