CF643E Bear and Destroying Subtrees
题解
我们可以先写出\(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的更多相关文章
- CF643E. Bear and Destroying Subtrees 期望dp
题目链接 CF643E. Bear and Destroying Subtrees 题解 dp[i][j]表示以i为根的子树中,树高小于等于j的概率 转移就是dp[i][j] = 0.5 + 0.5 ...
- 笔记-CF643E Bear and Destroying Subtrees
CF643E Bear and Destroying Subtrees 设 \(f_{i,j}\) 表示节点 \(i\) 的子树深度为 \(\le j\) 的概率,\(ch_i\) 表示 \(i\) ...
- [CF643E]Bear and Destroying Subtrees(期望,忽略误差)
Description: 给你一棵初始只有根为1的树 两种操作 1 x 表示加入一个新点以 x为父亲 2 x 表示以 x 为根的子树期望最深深度 每条边都有 \(\frac{1}{ ...
- CF 643 E. Bear and Destroying Subtrees
E. Bear and Destroying Subtrees http://codeforces.com/problemset/problem/643/E 题意: Q个操作. 加点,在原来的树上加一 ...
- Codeforces.643E.Bear and Destroying Subtrees(DP 期望)
题目链接 \(Description\) 有一棵树.Limak可以攻击树上的某棵子树,然后这棵子树上的每条边有\(\frac{1}{2}\)的概率消失.定义 若攻击以\(x\)为根的子树,高度\(ht ...
- [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} ...
- 一句话题解&&总结
CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...
- 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 ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- 【Linux开发】Linux动态链接库搜索路径问题
说明:下列内容是从网上获取的,未经验证,仅作参考之用 动态库的搜索路径搜索的先后顺序是: (1).编译目标代码时指定的动态库搜索路径:(2).环境变量LD_LIBRARY_PATH指定的动态库搜索路径 ...
- 推荐Calendar操作日期
package com.example.demo.Calender; import java.text.SimpleDateFormat;import java.util.Calendar;impor ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli2/Option
今天,在做canopy算法实例时,遇到这个问题,所以记录下来.下面是源码: package czx.com.mahout; import java.io.IOException; import org ...
- JDK集合框架源码分析 - 简单概要
1.类继承体系 在集合框架的类继承体系中,最顶层有两个接口Collection.Map: Collection 表示一组纯数据 Map 表示一组key-value对 Collection的类继承体系: ...
- HTML-图片和多媒体
1.图片和多媒体 (1) 图片:img元素 src 属性:图片路径: alt 属性:图片无法显示时使用的替代文字: title:鼠标悬停时显示的文字 : <img src="图片 ...
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/
- Thinkphp5 手册
thinkphp5 手册 https://www.kancloud.cn/manual/thinkphp5/118003
- PAT Advanced 1011 World Cup Betting (20 分)
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- Linux日常之Ubuntu系统中sendmail的安装、配置、发送邮件
一. 安装 1. sendmail必须先要安装两个包 (1)sudo apt-get install sendmail (2)sudo apt-get install sendmail-cf 2. u ...
- centos7安装mxnet
pip install mxnet-cu90 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 安装sklearn时总报错 ...