Codeforces 839C Journey - 树形动态规划 - 数学期望
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.
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.
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 .
4
1 2
1 3
2 4
1.500000000000000
5
1 2
1 3
3 4
2 5
2.000000000000000
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个节点的树,某个人骑着一匹等概率走向任意相连的未经过节点的马从1号点开始旅行,当到某个点的无法移动旅行结束。求期望的旅行长度(每条边的长度为1)。
显然到了某个点就不能到它的父节点。所以考虑动态规划。
f[i]表示当到达节点i后,期望还能走的步数。显然某个叶节点的f值为0.
现在考虑转移。
显然是每个子节点的f值加1再乘走到这个节点的概率。
Code
/**
* Codeforces
* Problem#839C
* Accepted
* Time: 78ms
* Memory: 11800k
*/
#include <bits/stdc++.h>
using namespace std; int n;
vector<int> *g;
double *f; inline void init() {
scanf("%d", &n);
g = new vector<int>[(n + )];
f = new double[(n + )];
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
} void dfs(int node, int fa) {
f[node] = ;
int count = ;
for(int i = ; i < (signed)g[node].size(); i++)
if(g[node][i] != fa)
count++;
if(!count) return;
double P = 1.0 / count;
for(int i = ; i < (signed)g[node].size(); i++) {
int& e = g[node][i];
if(e == fa) continue;
dfs(e, node);
f[node] += (f[e] + ) * P;
}
} inline void solve() {
printf("%.12lf", f[]);
} int main() {
init();
dfs(, );
solve();
return ;
}
Codeforces 839C Journey - 树形动态规划 - 数学期望的更多相关文章
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- Codeforces 839C Journey【DFS】
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...
- Codeforces 1000G Two-Paths 树形动态规划 LCA
原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...
- Codeforces Round #259(div2)C(数学期望)
数学题. 关键是求最大值为k时有多少种情况,结果是kn-(k-1)n-1.可以这么想:每一次都从1至k里选,共kn种,这里需要再减去每一次都从1至k-1里面选的情况.当然也可以分类计数法:按出现几次k ...
- CodeForces Div1: 995 D. Game(数学期望)
Allen and Bessie are playing a simple number game. They both know a function f:{0,1}n→Rf:{0,1}n→R, i ...
- [Codeforces 839C] Journey
[题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc ...
- Codeforces 912 质因数折半 方格数学期望
A B #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #d ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...
- 【BZOJ2134】单位错选(数学期望,动态规划)
[BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...
随机推荐
- vue滚动事件销毁,填坑
eg:富文本的头部固定,当滚轮大于200时(举例)固定在浏览器头部,距离大于富文本时,头部消失 效果: 在富文本下面加一个空div 这么写: mounted() { $(window).scroll( ...
- cocos2d-x JS 字符串
1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...
- BCB6.0 清除TPanel面板上的所有控件
方法一: panel->ComponentCount属性获得panel所拥有的控件个数 panel->Components[i]属性获得某一个控件 delete panel->Com ...
- 如何用vue组件做个机器人?有趣味的代码
<!DOCTYPE html> <html lang="en"> <div> <meta charset="UTF- ...
- Unity shader学习之屏幕后期效果之调整屏幕亮度,饱和度,对比度
Unity的屏幕后期处理效果,使用MonoBehaviour.OnRenderImage来实现. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7228063 ...
- sitecore系列教程之营销人员和技术人员如何策划与消费者的对话以提升体验?
“每次良好的交谈都要从良好的倾听开始.” - 未知 你是如何听取网站访问者的?你是在倾听还是只是回复? 拥有内容管理系统只是良好网站战略的一个要素.毕竟,内容必须是动态的,及时的和相关的. 当网站访问 ...
- Linux——CentOS7安装gcc编译器详解
使用yum安装gcc 使用yum命令安装还是非常easy的. yum -y install gcc gcc-c++ kernel-devel //安装gcc.c++编译器以及内核文件 手动安装gcc ...
- mysql 问题:Unknown system variable 'query_cache_size'
报错:Unknown system variable 'query_cache_size' mysql 的 java 驱动等级比较低,与mysql 数据库不匹配.
- python相关工具
1.matlab与python之间的数据传递 import scipy.io as sio import numpy as np ###下面是讲解python怎么读取.mat文件以及怎么处理得到的 ...
- c# 图像呈现控件PictureBox
在c#中可以使用PictureBox控件来呈现图像,图像资源可以来自文件,也可以是存在内存中的位图对象.可以显示本地图像文件或来自网络的图片,也可以来自项目文件中的图像. 从URI加载图像文件. 调用 ...