zoukankan      html  css  js  c++  java
  • 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 ? '
    ' : ' ');
        }
        return 0;
    }
  • 相关阅读:
    正向代理与反向代理
    uniapp
    js
    js
    uniapp
    uniapp
    uniapp
    uniapp
    关于资源获取(请把https改为http)
    uniapp
  • 原文地址:https://www.cnblogs.com/tiberius/p/9416240.html
Copyright © 2011-2022 走看看