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 ...
随机推荐
- 千万数据条 用户特征数据 写入mysql
from mysql_tool import * import copy s = ''' INSERT INTO `qqzone`.`myu` (`id`, `uid`, `age`, `gender ...
- 使用Zabbix监控ZooKeeper服务的健康状态
一 应用场景描述 在目前公司的业务中,没有太多使用ZooKeeper作为协同服务的场景.但是我们将使用Codis作为Redis的集群部署方案,Codis依赖ZooKeeper来存储配置信息.所以做好Z ...
- 【410】Linux 系统 makefile 文件
makefile 主要是用来合并编译文件 CC = gcc puzzle: puzzle.c boardADT.o $(CC) puzzle.c boardADT.o -o puzzle -lm bo ...
- bzoj 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果【tarjan+记忆化搜索】
对这个奇形怪状的图tarjan,然后重新连边把图变成DAG,然后记忆化搜索即可 #include<iostream> #include<cstdio> using namesp ...
- codevs1669(dfs)子集和目标值
1692 子集和的目标值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给定n个整数in和目标值T,求某一非空子集 ...
- laravel生命周期和核心思想
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- 2017杭电多校第六场1011Classes
传送门 Classes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- unix_12c_db_init
sample 1: add a new cdb 1.for the new db Aprod please apply two new direcotry in Cdb3/Cdb4/Aprod 5 ...
- [ Nowcoder Contest 167 #C ] 部分和
\(\\\) \(Description\) 给出一个长度为\(N\)的数组\(A[i]\),保证\(N\)为 \(2\) 的整次幂. 对于每个 \(i\ (i\in [0,N))\)求所有满足\(( ...
- jQuery中$this和$(this)的区别
要写一个点击弹窗任意地方,关闭弹窗.点击事件写标签在元素上 onclick = closepop(this),这时候很容易搞不清楚怎么去获取当前元素 function closepop(e){ va ...