Codeforces Round #362 (Div. 2) B - Puzzles (树上概率dp)
来源:互联网 发布:建筑力学分析软件 编辑:程序博客网 时间:2024/06/05 16:31
Barney lives in country USC (United States of Charzeh). USC hasn cities numbered from1 through n andn - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number1. Thus if one will start his journey from city1, he can visit any city he wants by following roads.
Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses arandom DFS to search in the cities. A pseudo code of this algorithm is as follows:
let starting_time be an array of length ncurrent_time = 0dfs(v):current_time = current_time + 1starting_time[v] = current_timeshuffle children[v] randomly (each permutation with equal possibility)// children[v] is vector of children cities of city vfor u in children[v]:dfs(u)
As told before, Barney will start his journey in the root of the tree (equivalent to calldfs(1)).
Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every cityi, Barney wants to know the expected value ofstarting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.
The first line of input contains a single integern (1 ≤ n ≤ 105) — the number of cities in USC.
The second line contains n - 1 integersp2, p3, ..., pn (1 ≤ pi < i), wherepi is the number of the parent city of city numberi in the tree, meaning there is a road between cities numberedpi andi in USC.
In the first and only line of output print n numbers, where i-th number is the expected value ofstarting_time[i].
Your answer for each city will be considered correct if its absolute or relative error does not exceed10 - 6.
71 2 1 1 4 4
1.0 4.0 5.0 3.5 4.5 5.0 5.0
121 1 2 2 4 4 3 3 1 10 8
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0
题意:从节点1开始dfs,每次随机选个儿子继续dfs,求到达每个节点的经过节点的期望个数(包含该节点)。
思路:很显然,经过节点a一定经过父节点,必然与父节点有关,所以到达a节点的经过节点期望个数 = 到达a的父节点的经过节点的期望个数 + 从父节点(不包含父节点)到节点a的经过节点的期望个数。
而a的父节点到节点a,可能直接到达,也可能经过经过若干兄弟节点所在的子树再到达a,因此就可以根据在先到达节点a还是先经过兄弟节点b,想到到达节点a之前经过某一个兄弟节点b所在子树的概率为0.5。用num[a] 表示 节点a所在子树(包阔节点a)的节点个数,dp[a]表示到达节点a的经过节点的期望个数。那么状态转移方程就是:
dp[a]=dp[b]+(num[b]-num[a]-1)*0.5+1 ; //b代表a的父节点
详细见代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>using namespace std;const int maxn = 1e5+100;vector<int>G[maxn];int num[maxn];double ans[maxn];int n;void dfs(int a){int i;for(i=0;i<G[a].size();i++){ans[G[a][i]]=ans[a]+(num[a]-num[G[a][i]]-1)*0.5+1;dfs(G[a][i]);}}int main(){scanf("%d",&n);int i,j,a,b;for(i=2;i<=n;i++){scanf("%d",&a);G[a].push_back(i);}for(i=n;i>=1;i--){a=1;for(j=0;j<G[i].size();j++)a+=num[G[i][j]];num[i]=a;}ans[1]=1.0;dfs(1);for(i=1;i<=n;i++)printf("%.1f%s",ans[i],i==n ? "\n":" ");return 0;}
- Codeforces Round #362 (Div. 2) B - Puzzles (树上概率dp)
- Codeforces Round #362 (Div. 2) D. Puzzles(概率dp)
- Codeforces Round #362 (Div. 2) D Puzzles (DP)
- Codeforces Round #362 (Div. 1) B Puzzles
- Codeforces Round #362 (Div. 2) D Puzzles(树形dp)
- Codeforces Round #362 (Div. 2)-D. Puzzles
- (Codeforces Round #362 (Div. 2)) D. Puzzles
- Codeforces Round #362 (Div. 2)D. Puzzles
- Codeforces Round #362 (Div. 2) D Puzzles
- Codeforces 696B. Puzzles (概率DP求期望)
- Codeforces Round #362 (Div. 1) B. Puzzles(递推)
- Codeforces Round #301 (Div. 2)D (概率dp)
- Codeforces Round #301 (Div. 2) D(概率dp)
- Codeforces Round #362 (Div. 2) D. Puzzles(dfs)
- Codeforces Round #362 (Div. 2) Problem D——Puzzles
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #196 (Div. 2) A. Puzzles
- Codeforces Round #284 (Div. 1) B. Name That Tune(概率DP)(难)
- java反射要点
- root工具类
- Ubuntu14.04下安装google chrome浏览器
- PHP 字符串二进制相互转换
- CAS-比较&交换并发处理策略
- Codeforces Round #362 (Div. 2) B - Puzzles (树上概率dp)
- Cg(C for Graphic)标准函数库之数学函数与几何函数
- JAVA之类加载
- 细说JVM系列:JVM介绍
- va_start va_end
- Java Web Servlet.init() for servlet springMVC threw exception解决
- Android 仿IOS3D时间选择器城市选择器
- 如何解决在onCreate()中获取View的width和Height为0?
- hdu 2389 Rain on your Parade(二分匹配Hopcroft-Carp算法模版)