[cf contest697] D - Puzzles
[cf contest697] D - Puzzles
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputIn 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.
Examplesinput71 2 1 1 4 4output1.0 4.0 5.0 3.5 4.5 5.0 5.0input121 1 2 2 4 4 3 3 1 10 8output1.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的更多相关文章
- 暑假集训单切赛第一场 CF 191A Dynasty Puzzles
题意不说了,看原题吧,思路见代码: #include <iostream> #include <stdio.h> #include <string.h> #incl ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 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 ...
- 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 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
随机推荐
- FileOutputSream文件字节输出流
1.FileOutputSream文件字节输出流: 输入--写出--使用: 输出--写入--存储: 写出写入是对硬盘而言: 其中,OutputStream为所有类型的字节输出流的超类: FileO ...
- 基于PLC1850平台的ICMP包请求与响应
一.以太网IP包报文格式 IP包是连接在以太网首部(以太网目的MAC地址(6个字节)+以太网源MAC地址(6个字节)+帧类型(2个字节))之后. IP报文中各个字段分析如下: ①.版本:在IP报文中, ...
- 高校表白APP-冲刺第三天
第三天,我们进行了第三次会议. 一.任务: 昨日任务:完成APP基本的登陆框架. 今日任务:完成登录界面修改界面注册界面,并解决闪退. 明日任务:完成界面跳转,并彻底解决闪退. 二.遇到的困难: 今天 ...
- JavaScript之事件的绑定与移除
对于事件的绑定的方法有多种多样,但是在解除绑定事件的时候,就要注意使用的是那种绑定事件的方法,因为不同的绑定方法所对应的解除事件是不同的. 1. 原始写法 1.1 绑定事件:对象.事件=事件处理函数 ...
- Linux笔记 #10# 用于支持Web应用开发&部署&配置的一些自定义脚本
索引 一.本地开发与测试相关脚本 1.startup.sh 2.shutdown.sh 3.catalina-out.sh 4.localhost_access_log.sh 5.上传本地文件到服务器 ...
- AHOI2019游记
day0 早上八点钟出发,下午一点左右到了(高速好堵啊) 下午试机,打了一发线段树,堆和其他一些东西,测试完没有问题就走了 惊奇发现旁边竟然是合肥市队的WC银牌hzy,%%% 晚上%了一发金牌爷yg就 ...
- 剑指offer(65)矩阵中的路径
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- 打包加载 AssetBundle
1.先创建Asset序列化(单个文件夹所在文件夹路径,会遍历这个文件夹所有的Prefab,所有的Prefab名字不能重复,必须保证名字得唯一性),配置好ConfigAB表 /* ######### # ...
- VFIO PF SRIOV IOMMU UIO概念解释、关联
1.UIO出现的原因 第一,硬件设备可以根据功能分为网络设备,块设备,字符设备,或者根据与CPU相连的方式分为PCI设备,USB设备等.它们被不同的内核子系统支持.这些标准的设备的驱动编写较为容易而且 ...
- MySql中not in的优化
最近项目上用select查询时使用到了not in来排除用不到的主键id一开始使用的sql如下: select s.SORT_ID, s.SORT_NAME, s.SORT_STATUS, s.SOR ...
