CF696B Puzzles 树形dp+期望

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

Copy

7
1 2 1 1 4 4

Output

Copy

1.0 4.0 5.0 3.5 4.5 5.0 5.0 

Input

Copy

12
1 1 2 2 4 4 3 3 1 10 8

Output

Copy

1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 

dp[ i ]表示访问到节点i时的时间期望;

考虑节点u及其一个子节点v

由于等概率地访问儿子节点,所以其他子节点在v以前访问或以后访问地概率都是0.5

自然:

dp[v]=dp[u]+1+(size[u]-size[v]-1)*0.5

一次dfssize,另一次求dp;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 100003
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n;
vector<int>vc[maxn];
double dp[maxn];
int SSize[maxn];

void dfs(int rt) {
	int Size = vc[rt].size();
	SSize[rt] = Size;
	for (int i = 0; i < Size; i++) {
		int to = vc[rt][i];
		dfs(to); SSize[rt] += SSize[to];
	}
}

void Treedp(int rt) {
	int tmp = vc[rt].size();
	for (int i = 0; i < tmp; i++) {
		int to = vc[rt][i];
		dp[to] = dp[rt] + 1.0 + 0.5*(SSize[rt] - SSize[to] - 1);
		Treedp(to);
	}
}

int main()
{
	//ios::sync_with_stdio(false);
	rdint(n);
	for (int i = 2; i <= n; i++) {
		int x; rdint(x); vc[x].push_back(i);
	}
	dfs(1);
	dp[1] = 1.0;
	Treedp(1);
	for (int i = 1; i <= n; i++) {
		printf("%.7lf ", 1.0*dp[i]);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/83689589