Description:

​ 给你一棵初始只有根为1的树

​ 两种操作

1 x 表示加入一个新点以 x为父亲

2 x 表示以 x 为根的子树期望最深深度

​ 每条边都有 \(\frac{1}{2}\) 的概率断裂。

Solution:

\[E(\max\{A\}) \not=\max\{E(A)\}
\]

​ 所以一般会从定义出发,设 \(dp[x][i]\) 表示以 \(x\) 为根,深度为 \(i\) 的概率。

​ 然后不好确定这个深度是在哪取到,所以可以设 \(dp[x][i]\) 为深度 \(\le i\) 的概率,不难发现这样每个子树就是独立的了。

\[dp[x][i] = \prod_{v\in son(x)}\frac{dp[x][i - 1] + 1}{2}
\]

​ 加1是因为 \((x, v)\) 这条边可能会断,那么如果断了,那么 \(dep\le i - 1\) 的概率一定是1。

​ 深度较大时期望值很小(它的缩减率是指数级的), 因为允许精度误差所以可以忽略. 加入每个点时把上面 50 个祖先的 \(dp\) 值更新一下即可。

Summary:

​ 在难以刻画细小的状态时可以将状态设广范些,但要保证前后可以互相转换。

Code:

#include <vector>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm> typedef long long LL;
typedef unsigned long long uLL; #define fir first
#define sec second
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define PB(x) push_back(x)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;
#define DE(x) cerr << x << endl;
#define rep(i, a, b) for (register int i = (a), i##_end_ = (b); (i) <= i##_end_; ++ (i))
#define drep(i, a, b) for (register int i = (a), i##_end_ = (b); (i) >= i##_end_; -- (i))
#define REP(i, a, b) for (register int i = (a), i##_end_ = (b); (i) < i##_end_; ++ (i)) inline int read() {
register int x = 0; register int f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
return x * f;
} template<class T> inline void write(T x) {
static char stk[30]; static int top = 0;
if (x < 0) { x = -x, putchar('-'); }
while (stk[++top] = x % 10 xor 48, x /= 10, x);
while (putchar(stk[top--]), top);
} template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } using namespace std; const int maxN = 5e5 + 1;
const int D = 50; int Q, fa[maxN], ncnt;
double dp[maxN][D]; void clear(int x, int son, int cnt)
{
if (!x || cnt >= D) return;
clear(fa[x], x, cnt + 1);
for (int i = 1; i < D; ++i)
dp[x][i] /= 0.5 * (dp[son][i - 1] + 1);
} void update(int x, int son, int cnt)
{
if (!x || cnt >= D) return;
for (int i = 1; i < D; ++i)
dp[x][i] *= 0.5 * (dp[son][i - 1] + 1);
update(fa[x], x, cnt + 1);
} double ask(int x)
{
double ans(0);
for (int i = 1; i < D; ++i)
ans += (double) i * (dp[x][i] - dp[x][i - 1]);
return ans;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("xhc.in", "r", stdin);
freopen("xhc.out", "w", stdout);
#endif
Q = read();
ncnt = 1;
fa[1] = 0;
fill(dp[1], dp[1] + D, 1);
while (Q--)
{
int op = read(), x = read();
if (op == 1)
{
fa[++ncnt] = x;
clear(fa[x], x, 1);
fill(dp[ncnt], dp[ncnt] + D, 1);
dp[x][0] *= 0.5;
update(x, ncnt, 0);
} else
{
printf("%.7lf\n", ask(x));
}
}
return 0;
}

[CF643E]Bear and Destroying Subtrees(期望,忽略误差)的更多相关文章

  1. CF643E. Bear and Destroying Subtrees 期望dp

    题目链接 CF643E. Bear and Destroying Subtrees 题解 dp[i][j]表示以i为根的子树中,树高小于等于j的概率 转移就是dp[i][j] = 0.5 + 0.5 ...

  2. 笔记-CF643E Bear and Destroying Subtrees

    CF643E Bear and Destroying Subtrees 设 \(f_{i,j}\) 表示节点 \(i\) 的子树深度为 \(\le j\) 的概率,\(ch_i\) 表示 \(i\) ...

  3. CF643E Bear and Destroying Subtrees

    题解 我们可以先写出\(dp\)式来. 设\(dp[u][i]\)表示以\(u\)为根的子树深度不超过\(i-1\)的概率 \(dp[u][i]=\prod (dp[v][i-1]+1)*\frac{ ...

  4. CF 643 E. Bear and Destroying Subtrees

    E. Bear and Destroying Subtrees http://codeforces.com/problemset/problem/643/E 题意: Q个操作. 加点,在原来的树上加一 ...

  5. Codeforces.643E.Bear and Destroying Subtrees(DP 期望)

    题目链接 \(Description\) 有一棵树.Limak可以攻击树上的某棵子树,然后这棵子树上的每条边有\(\frac{1}{2}\)的概率消失.定义 若攻击以\(x\)为根的子树,高度\(ht ...

  6. [cf674E]Bear and Destroying Subtrees

    令$f_{i,j}$表示以$i$为根的子树中,深度小于等于$j$的概率,那么$ans_{i}=\sum_{j=1}^{dep}(f_{i,j}-f_{i,j-1})j$ 大约来估计一下$f_{i,j} ...

  7. 一句话题解&&总结

    CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...

  8. lecture9-提高模型泛化能力的方法

    HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...

  9. lecture10-模型的结合与全贝叶斯学习

    这是Hinton的第10课 这节课有两篇论文可以作为背景或者课外读物<Adaptive mixtures of local experts>和<Improving neural ne ...

随机推荐

  1. Docker实战部署应用——Tomcat

    Tomcat 部署 拉取tomcat镜像 docker pull tomcat:8 创建tomcat容器 创建tomcat容器用于 Web应用,并且进行目录映射 docker run -id --na ...

  2. 82-基于Xilinx Virtex-5LXT FPGA的四路光纤PCIE卡(4路光纤卡)

    基于Xilinx Virtex-5LXT FPGA的四路光纤PCIE卡(4路光纤卡) 1.板卡特点: 1)  主芯片采用Xilinx Virtex-5LXT FPGA. 前面板提供4路光纤接口,速率可 ...

  3. 基于Redis实现简单的分布式锁【理论】

    摘要 分布式锁在很多应用场景下是非常有效的手段,比如当运行在多个机器上的不同进程需要访问同一个竞争资源的时候,那么就会涉及到进程对资源的加锁和释放,这样才能保证数据的安全访问.分布式锁实现的方案有很多 ...

  4. RabbitMQ拓展学习 自定义配置RabbitMQ连接属性

    最近研究RabbitMQ从本地获取配置,主要场景是RabbitMQ的连接配置如:ip地址这些需要从外部的配置服务器获取.面对这个问题,有两个解决方案,一个是用RabbitMQ原生的连接方式,但是如果使 ...

  5. 2018-09-25-weekly

    Algorithm 两数相加 What 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. How 两个链表长度相同的部分,对位相加, ...

  6. Linux安装mysql5.6.33

    1.下载mysql安装包: 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6.33,通用版,lin ...

  7. [洛谷P3958] NOIP2017 奶酪

    问题描述 现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪 中间有许多 半径相同 的球形空洞.我们可以在这块奶酪中建立空间坐标系,在坐标系中, 奶酪的下表面为z = 0,奶酪 ...

  8. CABasicAnimation来做心跳动画

    CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; ...

  9. 原来在UNITY中使用system.io下的所有函数都可以用相对路径 : Assets/xx

    代码如下图,这样就不用在绝对路径和相对路径之间不断转换了. 想要得到绝对路径时就傅 Application.dataPath  + xxx using System.Collections; usin ...

  10. day23—JavaScript实现DIV盒子拖拽(原生方式)

    转行学开发,代码100天——2018-04-08 <!doctype html> <html> <head> <meta charset="utf- ...