[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)如果需 ...
随机推荐
- poj3373--Changing Digits(DFS+剪枝///记忆化)
题目链接:点击打开链接 题目大意:给出一个n和一个k 求m 要求1.m要和n相同的位数 要求2.m要整除k 要求3.如果1和2满足,那么m要和n有尽量少的不同位 要求4.如果1.2.3满足,要使m尽量 ...
- react 父子组件通信
import React from 'react'; import B from './B'; class A extends React.Component{ state = { msg:'我来自于 ...
- Git抽取版本之间的差异,打包解压
patch.sh文件代码 #!/bin/bash MY_SAVEIFS=$IFS #IFS=$(echo -en "\n\b") IFS=$'\n' build_dir=" ...
- 为什么越来越多的人偏爱go语言
如果你是一个开发者或者程序员,你大概应该听过Go语言或者Golang语言.当然,如果没有听过也没关系,看到这篇文章的同学,就说明你对Golang是关注的,只需要这一点就够了.今天来聊聊关于Golang ...
- 利用 postMessage 进行数据传递 (iframe 及web worker)及问题
一 postMessage应用于主页面和iframe之间进行数据的传递 1 子iframe页面向主页面进行数据传递: // 多个子iframe需要将自己的计数统计到主页面进行数据上报 window. ...
- 3.JAVA基础复习——JAVA中的类与对象
什么是对象: 就是现实中真实的实体,对象与实体是一一对应的,现实中每一个实体都是一个对象在. JAVA中的对象: Java中通过new关键字来创建对象. 类: 用JAVA语言对现实生活中的事物进行描述 ...
- 防止sql注入(简单)
(1)mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 使用方法如下: $sql = "select count ...
- linux常用英文单词记录
1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...
- 自制操作系统Antz(8)——实现内核 (中) 扩展内核
Antz系统更新地址: https://www.cnblogs.com/LexMoon/category/1262287.html 在前几天的任务中,我们已经简单实现了MBR,直接操作显示器和硬盘操作 ...
- 关于matplotlib绘制直方图偏移的问题
在使用pyplot绘制直方图的时候我发现了一个问题,在给函数.hist()传参的时候,如果传入的组数不是刚刚好(就是说这个组数如果是使用(最大值-最小值)/组距计算出来,而这个数字不是整除得来而是取整 ...
