CF|codeforces 280C Game on Tree
题目链接:戳我
大概题意:给一棵树,然后每次可以删除一个子树,问你期望多少次能把整棵树都删完?
概率和期望是个神仙。。我不会
对于这个题,我们要有一个前置知识——期望的线性性,就是说总期望的值等于各个子期望的和。即——E(u+v)=E(u)+E(v)。
整个地处理整棵树对于n的数据范围来说肯定不现实(重要是我不知道怎么做??),那我们就拆成每个点的期望。
每个点的期望等于它操作的概率*单次贡献(在这个题里面这个单次贡献显然就是操作次数,即1),所以我们求出来它的操作概率即可。
根据题意,我们知道对于一个点,只有它的所有祖先都没有被选择,它才有可能被选择。而它下面的节点无论怎么选择都不会影响到它。所以它被选择的概率就是\(\frac{1}{dep[i]}\)。
所以。。。。一个dfs就完了。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,t;
int head[MAXN];
double ans,dep[MAXN];
struct Edge{int nxt,to;}edge[MAXN<<1];
inline void add(int from,int to){edge[++t].nxt=head[from],edge[t].to=to,head[from]=t;}
inline void dfs(int x,int f)
{
dep[x]=dep[f]+1.0;
for(int i=head[x];i;i=edge[i].nxt)
{
if(edge[i].to==f) continue;
dfs(edge[i].to,x);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs(1,1);
for(int i=1;i<=n;i++)
ans+=1.0/dep[i];
printf("%.7lf\n",ans);
return 0;
}
CF|codeforces 280C Game on Tree的更多相关文章
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces 280C - Game on Tree
传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...
- Codeforces 280C Game on Tree 期望
Game on Tree 这种题好像在wannfly训练营讲过, 我怎么又不会写啦, 我好菜啊啊啊. 我们按每个点算贡献, 一个点有贡献就说明它是被选中的点, 那么它被选中的概率就为1 / depth ...
- 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 ...
随机推荐
- 1001.害死人不偿命的(3n+1)猜想
题目截图: 思路: 简单模拟.具体见另一篇博客. 代码: /* 1001.害死人不偿命的(3n+1)猜想 */ #include <stdio.h> #include <string ...
- MySQL数据库篇之单表查询
主要内容: 一.单表查询的语法 二.关键字的执行优先级 三.简单查询 四.where约束 五.分组查询 group by 六.having过滤 七.查询排序 order by 八.限制查询的记录数 l ...
- split files test
python split_upload.py /root/github/python/s3_test/test.txt dbelt dumps python download_to ...
- 求Half向量
[求Half向量] 给定入射向量与视角向量.把入射向量与视角向量相加即可,如下: 前提是 lightDir.ViewDir 都是单位向量.
- C#自省
[C#自省] 1.根据string,获取type.Type.GetType 方法,获取具有指定名称的 Type,执行区分大小写的搜索. 2.根据obj,获取type.Object.GetType 方法 ...
- Flume NG 配置详解
配置 设置代理 Flume代理配置存储在本地配置文件.这是一个文本文件格式,是Java属性文件格式.在相同的配置文件,可以指定一个或多个代理的配置.配置文件包括每个源,接收器和通道,把它们连接在一起, ...
- 为什么要使用href=”javascript:void(0);”
为什么要使用href=”javascript:void(0);” href=”javascript:void(0);”这个的含义是,让超链接去执行一个js函数,而不是去跳转到一个地址,而void( ...
- Oracle VM VirtualBox 部署CS devcloud2 开发环境
Setting up (VirtualBox) 1. Get the new DevCloud 2.0 virtual appliance. The new image was created usi ...
- 关于jdk7中 使用Collections的排序方法时报Comparison method violates its general contract!异常
参考: Comparison method violates its general contract Comparison method violates its general contract! ...
- DecoratorPattern(23种设计模式之一)
参考书籍:设计模式-可复用面向对象软件基础(黑皮书) 书中写到,装饰者模式的意图是动态的给对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活.装饰者模式的另一个别名是包 ...