[CF643E]Bear and Destroying Subtrees(期望,忽略误差)
Description:
给你一棵初始只有根为1的树
两种操作
1 x 表示加入一个新点以 x为父亲
2 x 表示以 x 为根的子树期望最深深度
每条边都有 \(\frac{1}{2}\) 的概率断裂。
Solution:
\]
所以一般会从定义出发,设 \(dp[x][i]\) 表示以 \(x\) 为根,深度为 \(i\) 的概率。
然后不好确定这个深度是在哪取到,所以可以设 \(dp[x][i]\) 为深度 \(\le i\) 的概率,不难发现这样每个子树就是独立的了。
\]
加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(期望,忽略误差)的更多相关文章
- 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
题解 我们可以先写出\(dp\)式来. 设\(dp[u][i]\)表示以\(u\)为根的子树深度不超过\(i-1\)的概率 \(dp[u][i]=\prod (dp[v][i-1]+1)*\frac{ ...
- 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 计 ...
- lecture9-提高模型泛化能力的方法
HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...
- lecture10-模型的结合与全贝叶斯学习
这是Hinton的第10课 这节课有两篇论文可以作为背景或者课外读物<Adaptive mixtures of local experts>和<Improving neural ne ...
随机推荐
- hadoop项目开发运行报错(log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).)
使用hadoop+myeclipse开发项目是测试运行报错: log4j:WARN No appenders could be found for logger (org.apache.hadoop. ...
- Mybatis中dao层实现
在上一个笔记中继续: 因为要基于dao层,那么我们只需要又一个dao的接口,和一个mapper的文件就可以测试了. 但是基于dao层的时候需要规范: Mapper.xml文件中的namespace与m ...
- 使用国外 DNS 造成国内网站访问慢的解决方法
本文原载于 wzyboy's blog,转载请注明本文地址: https://wzyboy.im/post/874.html ,谢谢合作. 为什么要用国外 DNS 由于众所周知的问题,国内 DNS 服 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 06.队列、python标准库中的双端队列、迷宫问题
class QueueUnderflow(ValueError): """队列为空""" pass class SQueue: def __ ...
- ES6基本用法
es6是JS(JavaScript)的下一个版本. 新增了let命令,用来声明变量.变量在第一个花括号内有用,先声明后引用.不允许重复声明.存在暂时性死区. const声明一个只读的常量.一旦声明,常 ...
- elememt-ui 的 el-icon-iconName 图标 显示问题!
今天想在按钮处添加一个图标,但是显示不出.自己找了半天,终于找到了,希望帮到大家! 1,首先是没有报错的,但是有警告⚠ 意思是说什么拦截了之类的问题,但是到底是哪里问题导致拦截了呢?找了好久,原来是我 ...
- webdriver显式和隐式等待、强制等待
implicitly_wait() 方法是隐式等待,用来设置超时,一般把implicitly_wait()方法调用在加载测试地址后,等待所测试的应用程序加载WebDriverWait() 是显式等待, ...
- shp文件导入数据库
数据库服务器(引擎) sql server oracle nosql sql语句... 从数据库端导入:新建数据库,导入shp文件 发布地图服务 jdbc.sdk
- PHPStorm + Xdebug 调试PHP代码 有大用
星期四, 12/26/2013 - 19:54 - shipingzhong PHPStorm + Xdebug 调试PHP代码 http://e.v-get.com/2013-11-20 16:55 ...