题目链接:

B. Puzzles

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 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 number 1. Thus if one will start his journey from city 1, 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 a random DFS to search in the cities. A pseudo code of this algorithm is as follows:

let starting_time be an array of length n
current_time = 0
dfs(v):
current_time = current_time + 1
starting_time[v] = current_time
shuffle children[v] randomly (each permutation with equal possibility)
// children[v] is vector of children cities of city v
for u in children[v]:
dfs(u)

As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).

Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC.

The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC.

Output

In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].

Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.

Examples
input
7
1 2 1 1 4 4
output
1.0 4.0 5.0 3.5 4.5 5.0 5.0 
input
12
1 1 2 2 4 4 3 3 1 10 8
output
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 

题意:

给一棵有根树,dfs给这些节点编号,问每个节点编号的期望是多少?

思路:

在每个子树中,可以发现每个节点cur ,dp[cur]=dp[fa]+f(cur);
f这个函数可以发现是把fa当根节点,得到的每个子节点的期望值,这个值跟这棵子树的节点数有关;反正我最后发现可以变成一个等差数列;最后变成了(num[fa]-num[cur]-1)/2+1;
哈哈哈,反正就是一阵乱猜猜出来的; AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e6+;
const int maxn=3e6;
const double eps=1e-; int n,num[N];
double dp[N];
vector<int>ve[N]; int cnt = ;
void dfs(int cur,int fa)
{
num[cur]=;
int len = ve[cur].size();
For(i,,len-)
{
int y=ve[cur][i];
dfs(y,cur);
num[cur]+=num[y];
}
}
void dfs1(int cur,int fa)
{
int len=ve[cur].size();
dp[cur]=dp[fa]+(num[fa]-num[cur]-)*1.0/+;
For(i,,len-)dfs1(ve[cur][i],cur);
}
int main()
{
read(n);
For(i,,n)
{
int x;
read(x);
ve[x].push_back(i);
}
dp[]=;
num[]=n+;
dfs(,);
dfs1(,);
For(i,,n)printf("%.10lf ",dp[i]);
return ;
}

codeforces 696B B. Puzzles(树形dp+概率)的更多相关文章

  1. Codeforces 123E Maze(树形DP+期望)

    [题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...

  2. [CF697D]Puzzles 树形dp/期望dp

    Problem Puzzles 题目大意 给一棵树,dfs时随机等概率选择走子树,求期望时间戳. Solution 一个非常简单的树形dp?期望dp.推导出来转移式就非常简单了. 在经过分析以后,我们 ...

  3. CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )

    A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和. 我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉. 后来查了题解,首 ...

  4. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  5. BZOJ 3566: [SHOI2014]概率充电器 [树形DP 概率]

    3566: [SHOI2014]概率充电器 题意:一棵树,每个点\(q[i]\)的概率直接充电,每条边\(p[i]\)的概率导电,电可以沿边传递使其他点间接充电.求进入充电状态的点期望个数 糖教题解传 ...

  6. codeforces 709E E. Centroids(树形dp)

    题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  7. CodeForces 77C Beavermuncher-0xFF (树形dp)

    不错的树形dp.一个结点能走多次,树形的最大特点是到达后继的路径是唯一的,那个如果一个结点无法往子结点走,那么子结点就不用考虑了. 有的结点不能走完它的子结点,而有的可能走完他的子节点以后还会剩下一些 ...

  8. bzoj 4424: Cf19E Fairy && codeforces 19E. Fairy【树形dp】

    参考:https://blog.csdn.net/heheda_is_an_oier/article/details/51131641 这个找奇偶环的dp1真是巧妙,感觉像tarjan一样 首先分情况 ...

  9. Codeforces 482C Game with Strings(dp+概率)

    题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...

随机推荐

  1. gdb 远程调试android进程

    原文:http://blog.csdn.net/xinfuqizao/article/details/7955346?utm_source=tuicool 什么是gdb 它是gnu组织开发的一个强大的 ...

  2. linux网络性能评估

    Linux网络性能评估 参考自:自学it网,http://www.zixue.it/. 网络性能评估(1)通过ping命令检测网络的连通性.(2)通过netstat -i 组合检测网络接口状况.(3) ...

  3. C# 用this修饰符为原始类型扩展方法

    特点:1.静态类 2.静态方法 3.第一个参数前加this 例如:public static List<T> ToList<T>(this string Json),就是为th ...

  4. SecureCRT 配置文件中 找密码

    打开本地电脑如下路径 C:\Users\XXX\AppData\Roaming\VanDyke\Config\Sessions 找到配置文件. 运行命令:python SecureCRTDecrypt ...

  5. Netty构建游戏服务器(一)--基本概念与原理

    一,Netty是什么 1,Netty是由 JBOSS提供的一个 java开源框架. 2,Netty是JAR包,一般使用ALL-IN-ONE的JAR包就可以开发了. 3,Netty不需要运行在Tomca ...

  6. Codeforces Gym 100418K Cards 组合数学

    CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  7. 一个球,初始高度100,每次落下回弹一半高度,求第n次落下球走的距离

    def get_height(n): if n==1: eturn 150 return 100+sum([200*pow(0.5,i) for i in range(1,n)])+100*pow(0 ...

  8. Python基础语法06--文件

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  9. vs2015使用Git管理项目

    初级 1,在码云上去注册一个帐号(码云的私有库是免费的,安全性怎么样我不知道) 2,在码云上新建一个项目,把相关的开发人员加到这个项目里,会得到这个项目在码云上的远程仓库的地址. 3,打开vs2015 ...

  10. python实现QQ机器人(自己主动登录,获取群消息,发送群消息)

    一次偶然的机会我看见了一个群里的一个QQ号总是依据你所发的消息自己主动回复,当时非常感觉到奇妙.我知道能够模拟登录站点,没想到居然也能模拟登录QQ,首先自己想到的就是怎样实现模拟登录PC端的QQ, 開 ...