前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cf643E. Bear and Destroying Subtrees(期望dp)

cf643E. Bear and Destroying Subtrees(期望dp)

作者头像
attack
发布2018-09-30 09:55:49
3890
发布2018-09-30 09:55:49
举报

题意

题目链接

Sol

这种dp是第一次见啊,interesting。

设$f[i][j]$表示第$i$个节点,深度$\leqslant j$的概率

转移的时候分两种情况讨论

$f[i][j] = \prod \frac{1}{2}f[son[i]][j-1] + \frac{1}{2}$

由于修改操作只会影响到一条链的dp值,因此暴力往上update即可

考虑到dp值与深度有关,当深度$h>70$时$\frac{1}{2^{70}} < 10^{-6}$

因此只dp 70层即可

代码语言:javascript
复制
#include<cstdio>
#include<algorithm>
#include<vector>
//#define int long long 
using namespace std;
const int MAXN = 5 * 1e5 + 10, MaxH = 60;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int T, Q, fa[MAXN], N;
vector<int> v[MAXN];
double f[MAXN][61];
void mem(double *f) {
    for(int i = 0; i <= MaxH; i++) f[i] = 1;
}
main() {
    Q = read(); N = 1;
    mem(f[1]);
    while(Q--) {
        int opt = read(), x = read();
        if(opt == 1) {
            fa[++N] = x; 
            mem(f[N]);
            double pre = f[x][0], now;
            f[x][0] *= 0.5;
            for(int h = 1; h <= MaxH; h++, x = fa[x]) {
                now = f[fa[x]][h];
                f[fa[x]][h] /= 0.5 + 0.5 * pre;
                f[fa[x]][h] *= 0.5 + 0.5 * f[x][h - 1];
                pre = now;
            }
        } else if(opt == 2) {
            double ans = 0;
            for(int i = 0; i <= MaxH; i++) ans += i * (f[x][i] - f[x][i - 1]);
            printf("%.10lf\n", ans);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档