D. PuzzlesBarney lives in Country USC (with 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 what it is rooted). Root of the tree is the city number 1. Thus if one would start his journey from the City 1, he can visit any city he wants by following roads.
Some girl have stolen Barney ' s heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he's 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 is 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 a vector of children cities of City v
For u in Children[v]:
DFS (U)
As told before, Barney'll 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 on 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 to 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 P Arent City of city number i in the tree, meaning there is a road between cities numbered p i 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 would be considered correct if it absolute or relative error does not exceed -6.
Examplesinput
7
1 2 1 1 4 4
Output
Input
12
1 1 2 2 4 4 3 3 1 10 8
Output
Test instructions: give you a tree and then use DFS to label each point, asking for the expectation of each point marking;
IDEA: dfs+ probability dp;
We can know that the root node is expected to be 1;
Then, his child nodes are equal probabilities.
The DP array is the expectation of each node.
Let's give a diagram of sample one first:
Let's write the second-level arrangement.
2 4 5
2,1 2 5 4
3,1 4 2 5
4,1 4 5 2
5,1 5 2 4
6,1 5 4 2
So the expectation of Node 2 is dp[1]+ ((1+size (4) +size (5)) *2+size (4) +1+size (5) +1+1+1)/6;
According to this, we'll try to guess dp[v]=dp[u]+1+ (Size (u)-size (v)-1)/2;
This is the state transfer equation;
We first put the above to the next layer must add a step first, that is, dp[u]+1;
Then we can know all the following permutations, the sibling nodes of this node, either before or after the node, and the contribution of the other node to the node is size ()/2;
That is, the line in front and behind is equal probability;
Complexity O (N)
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <queue>6#include <stack>7#include <Set>8#include <stdlib.h>9#include <vector>Ten using namespacestd; Onevector<int>vec[100006]; A Long Longcnt[100006]; - Long LongDFS1 (intn); - Doubledp[100006]; the voidDfsintn); - intMainvoid) - { - inti,j,k; + while(SCANF ("%d", &k)! =EOF) - { + intN;memset (DP,0,sizeof(DP)); A for(i=0; i<100006; i++) at { -cnt[i]=0; - vec[i].clear (); - } - for(i=2; i<=k; i++) - { inscanf"%d",&n); - Vec[n].push_back (i); to } + Long LongT=DFS1 (1); -dp[1]=1.0; theDfs1);p rintf ("%.1f", dp[1]); * for(i=2; i<=k; i++) $ {Panax Notoginsengprintf"%.1f", Dp[i]); - } theprintf"\ n"); + } A return 0; the } + Long LongDFS1 (intN) - { $ Long Longsum; $ inti,j,k; - for(i=0; I<vec[n].size (); i++) - { thecnt[n]+=DFS1 (Vec[n][i]); - }Wuyicnt[n]+=1; the returnCnt[n]; - } Wu voidDfsintN) - { About inti,j; $ for(i=0; I<vec[n].size (); i++) - { - intx; -x=Vec[n][i]; Adp[x]=dp[n]+1.0; +dp[x]+=1.0* (cnt[n]-cnt[x]-1)/2.0; the dfs (x); - } $}
D. Puzzles (codeforces Round #362 (Div. 2))