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 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,从1节点开始dfs获取时间戳,每次dfs的时候搜的是任意一个儿子节点(每个节点搜到概率相同),求每个儿子的时间戳的期望;



首先1号节点的时间戳肯定是1.

ans[1] = 1;

然后我们考虑上图中1号节点的一个儿子节点3;

则有以下6种搜索的顺序

(它的子树就不写出来了)

1 2 3 4 ···①

1 2 4 3 ···②

1 3 2 4 ···③

1 3 4 2 ···④

1 4 2 3 ···⑤

1 4 3 2 ···⑥

(以下size[x]表示以x为根节点的子树的节点个数)

先让ans[3] = ans[1];

然后

③和④都是直接一步到达三号节点的,则增加量为2;

①则增加了size[2]+1,

②则增加了size[2]+size[4]+1;

⑤增加了size[4]+size[2]+1;

⑥增加了size[4]+1;

所以ans[3]+= (2+1+1+1+1 + 3*size[2] + 3*size[4])/6;

(除6可以这样理解,每种增加量的概率都是1/6)

再多观察一下。可以发现A(3,3)的所有排列中,所有1的直系儿子节点中除了3号节点外,其他节点在3号节点前面的概率都是1/2;

设3号节点的兄弟节点为a1,a2;

则我们最后ans[3]+= x

则x必然是 t1*size[a1] + t2*size[a2]+6 的形式

6可以理解吧?->这个常熟肯定是等于A(3,3)的

那么t1和t2的系数是多少呢?

“除了3号节点外,其他节点在3号节点前面的概率都是1/2”

根据这句话可以知道,那些系数t1,t2肯定都是相同的且为A(3,3)/2;

这样除下去就是1/2了。

所以归纳一下

ans[x] = ans[x的父亲节点] + (∑size[兄弟节点])/2 + 1

#include <cstdio>
#include <vector>
#include <iostream> using namespace std; const int MAXN = 2e5; int n, cnt[MAXN] = { 0 };
double ans[MAXN] = { 0 };
vector <int> a[MAXN]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} void dfs(int x)
{
cnt[x] = 1;
int len = a[x].size();
for (int i = 0; i <= len - 1; i++){
int y = a[x][i];
dfs(y);
cnt[x] += cnt[y];
}
} void get_ans(int x)
{
int len = a[x].size();
for (int i = 0; i <= len - 1; i++){
int y = a[x][i];
ans[y] = ans[x] + 1.0 + ((cnt[x] - cnt[y] - 1)*1.0) / 2.0; //父亲节点的大小减去这y节点的大小再减去父亲节点本身,就是y的兄弟节点的大小了。
get_ans(y);
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n);
for (int i = 2; i <= n; i++){
int x;
input(x);
a[x].push_back(i);
}
dfs(1);
ans[1] = 1.0;
get_ans(1);
for (int i = 1; i <= n; i++)
printf("%.1lf%c", ans[i],i==n?'\n':' ');
return 0;
}

【64.52%】【codeforces 697D】Puzzles的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【52.49%】【codeforces 556A】Case of the Zeros and Ones

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【25.64%】【codeforces 570E】Pig and Palindromes

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【44.64%】【codeforces 743C】Vladik and fractions

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【codeforces 514E】Darth Vader and Tree

    [题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...

  6. 【74.00%】【codeforces 747A】Display Size

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  9. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

随机推荐

  1. python分解质因数

    将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...

  2. 【hdu 2328】Corporate Identity

    [链接]h在这里写链接 [题意] 找一个字典序最小的公共最长子串; [题解] 后缀数组. 把所有的串用不同的分隔符分开.(大于'z'的分隔符); 然后求出那几个固定的数组. 二分一下那个子串的长度. ...

  3. Java BigDecimal的基本使用方法

    1.对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类 2.运算速度比一般的+.-.*./要快 3.基本方 法描 述  ...

  4. php 如何写一个自己项目的安装程序

    版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...

  5. ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST

    ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...

  6. 13、虚拟驱动vivi.c注册过程分析及怎么写V4L2驱动及启动过程

    UVC设备也是一个usb设备,在uvc_driver.c中的init函数会调用usb_register注册,根据id_table发送可支持的设备后调用probe函数,其会去uvc_register_c ...

  7. 《SPA设计与架构》之MV*框架

    原文 简书原文:https://www.jianshu.com/p/39f8f0aefdc2 大纲 1.认识MV*框架 2.传统UI设计模式 3.对框架的本质认识——框架有效性和框架分类 4.MV*基 ...

  8. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. XHTML 结构化:使用 XHTML 重构网站 分类: C1_HTML/JS/JQUERY 2014-07-31 15:58 249人阅读 评论(0) 收藏

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  10. 洛谷 P3908 异或之和

    洛谷 P3908 异或之和 题目描述 求1⨁2⨁⋯⨁N 的值. A⨁B 即 AA, B 按位异或. 输入输出格式 输入格式: 1 个整数 N . 输出格式: 1 个整数,表示所求的值. 输入输出样例 ...