• Codeforces Round #362 (Div. 2) D. Puzzles


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

    题意:求到达点1~n每个点的时间的期望。

    对于一个点 to 我们可以知道有两种方案到达这个点   (1)从父亲节点直接到达,这部分的概率为1(2)经过了不含to子树的那些点后到达to的父亲节点后再到达to,走这部分的概率为0.5

    结合样例1

    那么 可以得到 递推方程 ans[to]=ans[i]+1+(ans[i]-ans[to]-1)*0.5

    自上而下递推就行了。

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/7/24 16:25:35
    File Name     :1.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 100010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    vector<int>v[maxn];
    int sz[maxn];
    double ans[maxn];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n,x;
        while(cin>>n){
            for(int i=2;i<=n;i++){
                scanf("%d",&x);
                v[x].push_back(i);
            }
            for(int i=n;i>=1;i--){
                sz[i]=1;//由于题目中建树的特点 我们可以这样求sz[i]  dfs一下也可以
                for(int j=0;j<v[i].size();j++){
                    int to=v[i][j];
                    sz[i]+=sz[to];
                }
            }
            //cout<<sz[1]<<endl;
            int i=1;i<=n;i+
            ans[1]=1.0;
            for(int i=1;i<=n;i++){
                for(int j=0;j<v[i].size();j++){
                    int to=v[i][j];
                    ans[to]=ans[i]+1+(sz[i]-1-sz[to])*0.5;
                }
            }
            for(int i=1;i<=n;i++){
                printf("%.7f ",ans[i]);
            }
        }
        return 0;
    }
  • 相关阅读:
    数据库开发及ADO.NET
    C#典型案例及分析
    Visual Studio 2017 常用快捷键
    C#方法(函数)
    Linux常用命令----RPM包管理
    Linux常用命令----VIM命令
    Linux常用命令----压缩解压命令
    Linux常用命令----用户管理命令
    文件搜索命令
    Linux常用命令----权限管理命令
  • 原文地址:https://www.cnblogs.com/pk28/p/5701565.html
Copyright © 2020-2023  润新知