[cf contest697] D - 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 ncurrent_time = 0dfs(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
71 2 1 1 4 4
output
1.0 4.0 5.0 3.5 4.5 5.0 5.0 
input
121 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 

哎,还是太菜。

不解释题意了。

像我数学那么菜。。。还是要学学奇技淫巧。

在一棵以x为根的子树中,对于x的子节点u和v,u有一半的概率rand到v前面访问,也有一半概率rand到后面。

所以u对v的贡献就是size[v]/2.

所以,E[y]=E[x]+1+(size[x]-size[y]-1)/2。

code:

 #include <cstdio>
 #include <algorithm>
 #include <vector>
 using namespace std;

 ;
 int n,size[N]; double E[N];
 vector <int> e[N];

 void dfs_size (int x,int p) {
     size[x]=;
     ,y; i<s; ++i) {
         if (e[x][i]==p) continue;
         y=e[x][i];
         dfs_size(y,p);
         size[x]+=size[y];
     }
 }
 void dfs_E (int x,int p) {
     ,y; i<s; ++i) {
         if (e[x][i]==p) continue;
         y=e[x][i];
         E[y]=E[x]++)/;
         dfs_E(y,x);
     }
 }

 int main () {
     int x;
     scanf("%d",&n);
     ; i<=n; ++i) {
         scanf("%d",&x);
         e[x].push_back(i);
     }
     dfs_size(,);
     E[]=;
     dfs_E(,);
     ; i<=n; ++i) {
         printf("%.6lf ",E[i]);
     }
     ;
 }

[cf contest697] D - Puzzles的更多相关文章

  1. 暑假集训单切赛第一场 CF 191A Dynasty Puzzles

    题意不说了,看原题吧,思路见代码: #include <iostream> #include <stdio.h> #include <string.h> #incl ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

随机推荐

  1. [动态规划]数字三角形(版本I-III)

    level 1 1.1题目 1.1.1题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大.每一步可以走到左下方的点也可以到达右下方的点. 在 ...

  2. JAVA课堂测试之查找字母和单词出现次数、频率

    代码如下:没有加注释,自己研究吧. import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;i ...

  3. SSM框架下 Failed to load resource: the server responded with a status of 404 (Not Found)错误

    这个错误提示的是js的引用路径有错: 1.检查应用路径是否正确(我的问题是路径是正确的但是去到页面就会提示404错误) 引用路径,最好都使用绝对路径 <script type="tex ...

  4. CCF CSP 201503-1 图像旋转 (降维)

    题目链接:http://118.190.20.162/view.page?gpid=T27 问题描述 试题编号: 201503-1 试题名称: 图像旋转 时间限制: 5.0s 内存限制: 256.0M ...

  5. iOS项目之iPhoneX遇到的坑

    问题一: 今天升级到Xcode 9.0,里面多了iPhone 8,iPhone 8P,iPhone X三款手机模拟器,而且发现最多可以同时运行五个模拟器.但随之而来的问题也出现了,就是 iPhone ...

  6. Vue PC后台系统组件大全

    1.https://vue.ant.design/ 2.http://element-cn.eleme.io/#/zh-CN 3.https://www.iviewui.com/ 4.https:// ...

  7. COMP 321

    COMP 321April 24, 2019Questions on this exam may refer to the textbook as well as to the manual page ...

  8. Oracle错误——ORA-39002:操作无效、ORA-39070:无法打开日志文件、ORA-06512:在“SYS.UTL_FILE”,line

    错误 在使用数据泵impdp导入文件时,出现错误,无法导入数据 Next 问题原因 初步猜测,应该是Oracle用户权限出现问题,是对Directory目录无操作权限所致,经过一番修改和测试,发现使用 ...

  9. SPOJ 1812 LCS2 - Longest Common Substring II

    思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...

  10. 使用win10的开始屏幕,在系统中设置简洁、快捷桌面

    前几天入手了一个本本,由于之前电脑使用的柠檬桌面软件和现在本本的分辨率不适应,意外发现win10自带的开始屏幕整理桌面也是很有意思,再加上触摸板的手势,瞬间觉得整个电脑都清洁许多.废话少说,开始上料. ...