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序,显然f[1]=1是递推边界
我们考虑对于某一个节点i,设dfs序为f[i],子树大小为size[i],那么f[i]=f[father]+兄弟的贡献+1
而兄弟产生贡献的话,就取决于dfs的先后顺序,如果先dfs兄弟,会产生size[兄弟]的贡献
那么的兄弟的总贡献就是兄弟的子树大小之和/2,即(size[father]-1-size[rt])/2
那么本题就得到了解决,代码见下:
 #include <cstdio>
#include <cstring>
using namespace std;
const int N=;
int n,fa[N],size[N],e,adj[N];
struct node{int zhong,next;}s[N];
double f[N];
inline void add(int qi,int zhong)
{s[++e].zhong=zhong;s[e].next=adj[qi];adj[qi]=e;}
void dfs1(int rt)
{
size[rt]=;
for(int i=adj[rt];i;i=s[i].next)
dfs1(s[i].zhong),size[rt]+=size[s[i].zhong];
}
void dfs2(int rt)
{
for(int i=adj[rt];i;i=s[i].next)
{int u=s[i].zhong;f[u]=f[rt]+(size[rt]--size[u])/2.0+;dfs2(u);}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&fa[i]),add(fa[i],i);
dfs1();f[]=;dfs2();
for(int i=;i<=n;i++)
printf("%.8lf ",f[i]);
}

[codeforces696B]Puzzles的更多相关文章

  1. 【codeforces 696B】 Puzzles

    http://codeforces.com/problemset/problem/696/B (题目链接) 题意 给出一棵树,随机dfs遍历这棵树,求解每个节点的期望dfs序. Solution 考虑 ...

  2. codeforces A. Puzzles 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...

  3. What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?

    QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...

  4. C puzzles详解

    题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...

  5. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  6. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  7. 《algorithm puzzles》——谜题

    这篇文章开始正式<algorithm puzzles>一书中的解谜之旅了! 狼羊菜过河: 谜题:一个人在河边,带着一匹狼.一只羊.一颗卷心菜.他需要用船将这三样东西运至对岸,然而,这艘船空 ...

  8. 《algorithm puzzles》——概述

    这个专题我们开始对<algorithm puzzles>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. ...

  9. Puzzles

    Puzzles Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 thr ...

随机推荐

  1. 后端API入门到放弃指北

    后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一 ...

  2. a data verification error occurred, file load failed

    1. 调试创龙DSP6748的时候,下载.out文件出现这个错误 2. 换了其他板子,还有其他仿真器也不行,最后发现是没加载GEL文件

  3. Jmeter资源监控工具ServerAgent运行原理的一些研究

    用过Jmeter的应该都了解,有个ServerAgent工具,放在linux或者windows服务器上开启服务后,在Jmeter中配置下监视器,就可以抓取到服务器的一些资源信息,抓取的主要是cpu.内 ...

  4. [SHELL]查看端口,文件,服务关系的四个命令netstat,lsof,fuser,nmap

    一,netstat (1)简介 netstat主要是用来打印系统网络的状态信息,当输入netstat后,输出如下: 可以看出,netstat的输出分为两个部分组成: 一个是Active Interne ...

  5. IO模型浅析-阻塞、非阻塞、IO复用、信号驱动、异步IO、同步IO

    最近看到OVS用户态的代码,在接收内核态信息的时候,使用了Epoll多路复用机制,对其十分不解,于是从网上找了一些资料,学习了一下<UNIX网络变成卷1:套接字联网API>这本书对应的章节 ...

  6. hadoop之Shuffle和Sort

    MapRduce保证reducer的输入是按照key进行排过序的,原因和归并排序有关,在reducer接收到不同的mapper输出的有序数据后,需要再次进行排序,然后是分组排序,如果mapper输出的 ...

  7. Windows下使用WinRAR命令自动备份文件

    最近有一个需求:为了防止数据丢失,每天对固定文件夹下的文件进行打包压缩备份. 解决办法:使用Windows的任务计划程序,每天执行一下压缩命令: Windows任务计划程序在这里就不再介绍了,网上有很 ...

  8. Java反编译插件

    一.eclipse->help->Eclipse Marketplace 如下图:搜索JadClipse,install进行下载安装,

  9. sqoop 常用命令集

    sqoop是一个介于分布式数据系统与关系型系统之间数据转换的一个数据转换工具 常用命令集sqoop2中sqoop-shell 创建link.job sqoop:001> show link 显示 ...

  10. A Bug's Life(加权并查集)

    Description Background  Professor Hopper is researching the sexual behavior of a rare species of bug ...