CodeForces 696B Puzzles

思维,简单树$dp$。

首先计算出每一个子树包含多少个节点,记为$f[i]$。然后就可以从$root$开始推出所有节点的期望了。

现在已知$fa$节点的答案为$ans[fa]$,假设要计算$fa$的一个儿子$v$的期望,那么$ans[v]=ans[fa]+1.0+(f[fa]-f[v]-1)/2.0$。

我画了一下样例,然后猜测了一下发现是对的......

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
}

const int maxn=100010;
double ans[maxn];
vector<int>g[maxn];
int n,f[maxn];

void dfs(int x)
{
    f[x]=0;
    for(int i=0;i<g[x].size();i++)
    {
        dfs(g[x][i]);
        f[x]=f[x]+f[g[x][i]];
    }
    f[x]++;
}

void get(int x)
{
    for(int i=0;i<g[x].size();i++)
    {
        ans[g[x][i]]=ans[x]+1.0+1.0*(f[x]-f[g[x][i]]-1)/2.0;
        get(g[x][i]);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        int fa; scanf("%d",&fa);
        g[fa].push_back(i);
    }
    dfs(1);  ans[1]=1.0; get(1); 
    for(int i=1;i<=n;i++) printf("%.6lf ",ans[i]);
    printf("\n");
    return 0;
}

 

posted @ 2016-08-23 10:53  Fighting_Heart  阅读(216)  评论(0编辑  收藏  举报