题解

我们可以先写出\(dp\)式来。

设\(dp[u][i]\)表示以\(u\)为根的子树深度不超过\(i-1\)的概率

\(dp[u][i]=\prod (dp[v][i-1]+1)*\frac{1}{2}\)

然后因为这道题精度要求比较低,所以我们对于每个\(u\),保留第二维60个就行了。

所以每次加入一个节点的时候,我们只需要更新父链上60个\(dp\)值就好了,复杂度\(O(n*60)\)。

代码

#include<bits/stdc++.h>
#define N 500002
using namespace std;
typedef long long ll;
const int maxd=60;
int q,n,fa[N];
double dp[N][61];
inline ll rd(){
ll x=0;char c=getchar();bool f=0;
while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f?-x:x;
}
int main(){
q=rd();
int type,x;
n=1;
for(int i=0;i<=maxd;++i)dp[1][i]=1;
while(q--){
type=rd();x=rd();
if(type==1){
++n;
for(int i=0;i<=maxd;++i)dp[n][i]=1;
fa[n]=x;
double pre=1;int s=n;
for(int dep=0,now=x;now&&dep<=60;dep++,now=fa[now]){
double nw=dp[now][dep];
dp[now][dep]/=0.5*(1+pre);
if(dep)dp[now][dep]*=0.5*(1+dp[s][dep-1]);
else dp[now][dep]*=0.5;
s=now;pre=nw;
}
}
else{
double ans=0;
for(int i=1;i<=60;++i)ans+=(dp[x][i]-dp[x][i-1])*i;
printf("%.10lf\n",ans);
}
}
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(期望,忽略误差)

    Description: ​ 给你一棵初始只有根为1的树 ​ 两种操作 ​ 1 x 表示加入一个新点以 x为父亲 ​ 2 x 表示以 x 为根的子树期望最深深度 ​ 每条边都有 \(\frac{1}{ ...

  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. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks 水题

    B. Bear and Blocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pr ...

  9. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. ubuntu 新建root用户

    1. sudo passwd :设置root用户密码 2. 切换用户 方式一:su 方式二: su root 3. 新增普通用户

  2. Yii2.0 limit(1)与one()

    ActiveRecord中的limit(1)与one()的区别 (new \yii\db\Query())->from('user')->limit(1)->one() public ...

  3. Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式

    Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...

  4. tensorflow学习笔记二----------变量

    tensorflow里面的变量表示,需要使用特定的语法进行.如果想构造一个行(列)向量,需要调用Variable函数进行.对两个变量进行操作,也要调用相应的函数. import tensorflow ...

  5. 管道(Pipe)----计算机进程间通信

    参至他人博客:https://blog.csdn.net/u011583316/article/details/83419805

  6. 迁移数据库mysql

    文档 <http://site.clairvoyantsoft.com/migrate-cloudera-scm-database-from-postgresql-to-mysql/> h ...

  7. 关于微信小程序 modal弹框组件的介绍

    微信小程序 modal: 这里对微信小程序中 modal组件进行详细解析,我想开发微信小程序的小伙伴可以用到,这里小编就记录下modal的知识要点. modal modal类似于javascript中 ...

  8. [七月挑选]使用idea创建spring boot 项目

    title: 使用idea创建spring boot 项目 参考lindaZ的IntelliJ IDEA 创建spring boot 的Hello World 项目 1.Open IDEA,choos ...

  9. Python multiprocessing使用详解

    multiprocessing包是Python中的多进程管理包. 与threading.Thread类似,它可以利用multiprocessing.Process对象来创建一个进程. 该进程可以运行在 ...

  10. SQL常见面试题-行列互换

      有一个SQL题在面试中出现的概率极高,最近有学生出去面试仍然会遇到这样的题目,在这里跟大家分享一下. 题目:数据库中有一张如下所示的表,表名为sales. 年 季度 销售量 1991 1 11 1 ...