codeforces#428(div2)C Journey(树上的dfs水题)
来源:互联网 发布:js 调用支付宝接口 编辑:程序博客网 时间:2024/05/21 05:24
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 ≤ n, ui ≠ 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.
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 ≤ n, ui ≠ 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.
题意:求一棵树上根节点到每个叶子的距离的期望(PS:一开始题目读错了,以为是求到各叶子距离的平均值,其实每个分叉点都有概率这一说的,如果有两个儿子那么到每个儿子的概率都是0.5,然后一路算下去 0.0,最后发现挺多人都错在这一组数据,就怀疑题目读错了,果然。。。在最后几分钟发现了。。。然后没时间改了,苦恼 ..0.0...)
思路:直接dfs出到叶子的距离和概率就好了
代码:
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <numeric>#include <set>#include <string>#include <cctype>#include <sstream>#define INF 0x3f3f3f3f#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;typedef long long LL;typedef pair<LL, LL> P;const int maxn = 1e5 + 5;const int mod = 1e8 + 7;int n,fa[maxn];vector<int>G[maxn];double cnt=0,sum=0;void dfs(int x,int d,double ss) { if (G[x].size()==1&&x!=1) { sum += (d-1)*ss; return; } for (int i=0; i<G[x].size(); i++) { int son=G[x][i]; if (son==fa[x]) continue; fa[son]=x; double sss ; if(x == 1) sss = ss* 1.0/(G[x].size()); else sss = ss* 1.0/(G[x].size()-1); dfs(son,d+1,sss); } return;}int main() { //freopen ("in.txt", "r", stdin); while (~scanf ("%d",&n)) { if (n==1) { printf ("0.000000000000000\n"); continue; } for (int i=0; i<=n; i++) G[i].clear(); cnt=0; sum=0; memset(fa,0,sizeof(fa)); int u,v; n--; while (n--) { scanf ("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } dfs(1,1,1.0); printf ("%.15lf\n",sum); } return 0;}
阅读全文
0 0
- codeforces#428(div2)C Journey(树上的dfs水题)
- codeforces 428div2 C journey dfs+期望
- Codeforces#374(div2)- C. Journey(二分答案+dp)
- Codeforces #428 (Div. 2) C. Journey (DFS
- Codeforces 721C Journey (简单dp,dfs)
- Codeforces Round #428 (Div. 2) C.Journey(dfs求期望)
- C. Journey(dfs求期望+Codeforces Round #428 (Div. 2))
- Codeforces Round #428 (Div. 2) C. Journey(【dfs】求期望值)
- 【Codeforces 839 C. Journey】& dfs
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces 839C Journey (树的DFS+期望)
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- Codeforces Round #428 (Div. 2) C. Journey(dfs)
- Codeforces 839C Journey(dfs+概率)
- CodeForces-839C Journey(DFS+概率)
- Codeforces 721C. Journey(DAG上的DP)
- Codeforces-721C:Journey(最短路上的DP)
- CodeForces 721C Journey(DP)
- Codeforces Round #427 (Div. 2) C,D,E
- ucos2中的英文和相关的代码风格
- hdu6113.度度熊的01世界
- 2017 最新 ERROR Deployer not found: git hexo d 报错
- Java从控制台输入数据
- codeforces#428(div2)C Journey(树上的dfs水题)
- 常用sql语句记录
- 微信红包问题:找出某个出现次数超过红包总数一半的红包的金额(面试题)
- Robberies(01背包的概率问题)
- "win7便笺元数据损坏,便笺已将其恢复为默认值"如何修复
- python if真假判断
- 54.NIS服务器
- QML 发光呼吸动画字体
- 没有什么是学不会的,关键是你敢不敢豁出去——《向着光亮那方》读后感