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 ...
随机推荐
- bzoj4974: [Lydsy1708月赛]字符串大师
脑洞题...玄学 假如是a[i]!=i,说明构成了循环节长为a[i]的循环,那就去%一下(别想多)看一下是循环节中的第几个咯. 否则新填的这个不能和前面构成任何循环,那就不停往前跳去把不能填的标记一下 ...
- HDU 1517:A Multiplication Game
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- oracle数据库的导入 导出实例
oracle数据库的导入 导出实例 分类: DataBase2011-09-07 23:25 377人阅读 评论(0) 收藏 举报 数据库oraclefileusercmdservice 我要从另外一 ...
- 【HDU 2010】水仙花数
http://acm.hdu.edu.cn/showproblem.php?pid=2010 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位 ...
- searchView 颜色 icon 设置
public void initSearchViewActions() { searchView.setMaxWidth(Integer.MAX_VALUE); searchView.onAction ...
- RDA DEBUG
DEBUG寄存器:word 0xa0000010 word 0xa0000010 1 //debug开 word 0xa0000010 0 //debug关 当然也可以按模块打开/关闭debug信息, ...
- 如何精通javascript
http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...
- SQL Server 添加描述
添加描述的格式 exec sys.sp_addextendedproperty @name = N'MS_Description' ,@value = 'value',@level0type=N'SC ...
- 洛谷P3383 【模板】线性筛素数 (埃拉托斯特尼筛法)
题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...
- java 读取word
读取word文件 import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org ...