Puzzles

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 
分析:不是太懂,貌似不在当前节点的子树中,却在他父亲的子树中的节点,先比他拜访的概率是0.5;
   官方题解:http://codeforces.com/blog/entry/46031
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,child[maxn],fa[maxn];
double vis[maxn];
vi p[maxn];
int dfs(int now)
{
int cnt=;
for(int x:p[now])cnt+=dfs(x);
return child[now]=cnt+;
}
void work(int now)
{
if(now!=)
vis[now]=vis[fa[now]]++(child[fa[now]]-child[now]-)/2.0;
for(int x:p[now])work(x);
}
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n)scanf("%d",&k),p[k].pb(i),fa[i]=k;
vis[]=1.0;
dfs();
work();
rep(i,,n)printf("%g ",vis[i]);
//system ("pause");
return ;
}
												

Puzzles的更多相关文章

  1. codeforces A. Puzzles 解题报告

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

  2. 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 ...

  3. C puzzles详解

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

  4. codeforces 377A. Puzzles 水题

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

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

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

  6. 《algorithm puzzles》——谜题

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

  7. 《algorithm puzzles》——概述

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

  8. [CF697D]Puzzles 树形dp/期望dp

    Problem Puzzles 题目大意 给一棵树,dfs时随机等概率选择走子树,求期望时间戳. Solution 一个非常简单的树形dp?期望dp.推导出来转移式就非常简单了. 在经过分析以后,我们 ...

  9. [cf contest697] D - Puzzles

    [cf contest697] D - Puzzles time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. android 线程池的使用

    转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...

  2. java代码如何读取properties文件

    我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改.这里列出了两种比较方便的方式. 一.在Spring配置文件中 ...

  3. IoC容器Autofac正篇之简单实例(四)

    先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...

  4. Git的安装

    两种方法: 1.命令行git Git preview是命令行Git的安装包,包名如下: Git-1.9.2-preview20140411.exe 2.UI类型的git 图形化界面的git,避免了枯燥 ...

  5. VS2008 安装后没有模板

    VS2008 安装过程没有任何报错  启动VS2008,新建项目时就成了这样,没有任何模板: 解决方法: 开始 –> 程序 –> Microsoft Visual Studio 2008– ...

  6. Spring中实现监听的方法

    在未使用框架进行编程的时候,我们常常在web.xml中加上这样一段话 <listener> <listener-class>XXX</listener-class> ...

  7. 在idea的maven项目使用el或jstl表达式

    必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...

  8. 【A + B + C + D】 问题

    A + B + C + D Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 软件开发常用的linux命令心得(ubuntu为例)

    软件开发过程中难免要经常对主机进行配置或者部署等操作,想到一些就写一些了,以后再更新 解压命令: a.如果是tar文件,则直接用 “tar zxvf 文件名”: b.如果是zip文件,用 “unzip ...

  10. js 关键字 in

    对于数组 ,迭代出来的是数组元 素,对于对象 ,迭代出来的是对象的属性: var x var mycars = new Array() mycars[0] = "Saab" myc ...