Codeforces 697D

题意略。

思路:

对于随机产生的一个数列,对于某个儿子,其兄弟在其前面的概率为 1 / 2。

所以这个兄弟对期望的贡献为son[v] / 2,所有兄弟加起来即为(tot - 1) / 2。

 

详见代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;

int son[maxn],n,fa;
vector<int> graph[maxn];
double ans[maxn];

void dfs(int cur){
    son[cur] = 1;
    for(int i = 0;i < graph[cur].size();++i){
        int v = graph[cur][i];
        dfs(v);
        son[cur] += son[v];
    }
}
void dfs1(int cur,double cur_e){
    double tot = son[cur] - 1;
    for(int i = 0;i < graph[cur].size();++i){
        int v = graph[cur][i];
        ans[v] = 1 + cur_e + (tot - son[v]) / 2;
        dfs1(v,ans[v]);
    }
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 2;i <= n;++i){
        scanf("%d",&fa);
        graph[fa].push_back(i);
    }
    dfs(1);
    ans[1] = 1;
    dfs1(1,1);
    for(int i = 1;i <= n;++i){
        printf("%lf%c",ans[i],i == n ? '\n' : ' ');
    }
    return 0;
}

 

posted @ 2018-08-03 20:52  温和的提比略  阅读(116)  评论(0编辑  收藏  举报