Codeforces 280C - Game on Tree
不知道期望是啥的请自行Baidu或Google,(溜了
题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数
那我们就来想,如果要计算一个节点的期望的话每个节点和它的祖先是在决策范围内的,所以它的子树我们可以先不用管,需要预处理出每一个点有几个祖先,当然还要加上它本身
因为每一个点的随机变量都是1,所以只需要将概率计算出来求一个和就行,注意题目要求控制精度了。
代码(:逃
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int MAXN = (int(1e5)+3)*2; int N, F[MAXN];
double Ans;
int u[MAXN], v[MAXN], first[MAXN], next[MAXN]; inline int read() {
int x = 0, f = 1;
char c = getchar();
while(c < '0'||c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c <= '9'&&c >= '0') {
x = x*10+c-'0';
c = getchar();
}
return x * f;
} void dfs(int x, int from) {
int k = first[x];
while(k != -1) {
if(v[k] == from) {k = next[k]; continue;}
F[v[k]] = F[u[k]]+1;
dfs(v[k], u[k]);
k = next[k];
}
} int main() {
N = read();
memset(first, -1, sizeof(first));
for(int i=1; i<=(N-1)*2; i++) {
u[i] = read(), v[i] = read();
next[i] = first[u[i]];
first[u[i]] = i;
i++;
u[i] = v[i-1], v[i] = u[i-1];
next[i] = first[u[i]];
first[u[i]] = i;
}
F[1] = 1;
dfs(1, 0);
for(int i=1; i<=N; i++) {
Ans += 1.0/double(F[i]);
}
printf("%.10lf", Ans);
}
Codeforces 280C - Game on Tree的更多相关文章
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces 280C Game on Tree 期望
Game on Tree 这种题好像在wannfly训练营讲过, 我怎么又不会写啦, 我好菜啊啊啊. 我们按每个点算贡献, 一个点有贡献就说明它是被选中的点, 那么它被选中的概率就为1 / depth ...
- CF|codeforces 280C Game on Tree
题目链接:戳我 大概题意:给一棵树,然后每次可以删除一个子树,问你期望多少次能把整棵树都删完? 概率和期望是个神仙..我不会 对于这个题,我们要有一个前置知识--期望的线性性,就是说总期望的值等于各个 ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- win32gui.EnumWindows my.os.EnumWindows.py
import win32guidef _MyCallback(hwnd, extra): windows = extra temp = [] temp.append(hex(hwnd)) temp.a ...
- 定时删除clientmqueue
* * */1 * * cd /var/spool; cp -r clientmqueue /home/data/xl_project/var_spool_clientmqueue_$(date + ...
- Grunt学习日记
Grunt和 Grunt 插件是通过npm安装并管理的, npm是Node.js的包管理器. 第一步:先安装node.js环境 第二步:安装Grunt-CLI 在node.js命令工具中输入npm i ...
- 解决Eclipse alt+/不出来提示的问题
1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the proposal ...
- 安装Node.js 以及命令行使用
安装 官方的安装包 安装完成之后,会自动添加到环境变量中 通过visual studio安装 命令行 查看版本 PS C:\Users\clu\Desktop> node --version v ...
- hdu3018 Ant Trip (并查集+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:给你一个图,每条路只能走一次.问至少要多少个人才能遍历所有的点和所有的边. 这是之前没有接 ...
- E20180121
signature n. 签名; 署名; 识别标志,鲜明特征; [医] 药的用法说明;
- Linux day01(二)虚拟机快照和克隆的用法介绍
一:快照 优点:运行虚拟机后不用担心系统会被弄崩溃了,点击快照会立即恢复到初始状态 缺点:回滚会带来数据的丢失,所以要考虑数据恢复的成本和找回数据时进行操作的成本 1. 在导航栏中找虚拟机快照的小图标 ...
- Linux安装MySQL标准教程
导读: 本文主要介绍 CentOS 系统二进制安装 MySQL 5.7.23 版本的安装步骤,其他版本安装过程相似. 1.前置准备 卸载旧版MySQL 查看rpm包 rpm -qa|grep mysq ...
- lnmp环境的nginx的tp5配置
php7.1 server { listen 80; server_name www.tp5.com; access_log /home/wwwroot/access.log combined; er ...