1. 程式人生 > >Codeforces 697D

Codeforces 697D

clu == 產生 void int ORC turn ack dfs

題意略。

思路:

對於隨機產生的一個數列,對於某個兒子,其兄弟在其前面的概率為 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; }

Codeforces 697D