"Codeforces Round 362 (Div 2) D"

Source: Internet
Author: User

Description
Barney lives in Country USC (with states of Charzeh). USC have 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.
This picture was so amazing~~

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 S Earch 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-pack a backpack and so he wants-know more on his upcoming journey:for every city I, Barney want s 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 parent city of City N Umber I in the tree, meaning there are 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 would be considered correct if it absolute or relative error does not exceed 10-6.

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

Ideas:
The number of steps required to reach a point is expected for its parent step count plus one in addition to the size of its sibling node tree/2.0. Because, from the parent node to this node and the probability of the node is equal to 1/2;

 #include <cstdio> #include <cstring> #include <vector> using namespace std;
int n,sz[100009];
Double ans[100009];
Vector<int> mp[100099];
    void dfs1 (int x)//calculates the size of the tree with x as the root {sz[x]=0;
        for (int i=0;i<mp[x].size (); i++) {int y=mp[x][i];
        DFS1 (y);
    Sz[x]+=sz[y];
} sz[x]++;
        } void Dfs2 (int x) {for (int i=0;i<mp[x].size (); i++) {int y=mp[x][i];
        int sum=sz[x]-sz[y]-1;//the size of the brother Tree ans[y]=ans[x]+1+sum/2.0;
    DFS2 (y);
    }} int main () {scanf ("%d", &n);
        for (int i=2;i<=n;i++) {int x;
        scanf ("%d", &x);
    Mp[x].push_back (i);
    } DFS1 (1);
    ans[1]=1.0;
    DFS2 (1);
    for (int i=1;i<=n;i++) printf ("%.1f", Ans[i]);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.