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 ...
随机推荐
- 操作SQL Server的帮助类
可作为以后开发的参考代码,也可以再整理下,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
- Maven01 环境准备、maven项目结构、编译/测试/打包/清除、安装、
0 前提准备 0.1 安装java开发环境 0.2 安装maven工具 1 maven项目基本结构 如图所示,整个maven项目有业务文件.测试文件.POM依赖管理文件:其实还有一个资源文件resou ...
- 在tomcat中的conf文件夹下找server.conf 配置如下代码,可省略项目名路径访问 如 localhost:8080 即可访问到项目
<Context path="" docBase="C:\Workspaces\Projects\SLSaleSystem_dw\WebRoot" rel ...
- faster-rcnn训练自己的数据集参考文章
https://www.cnblogs.com/CarryPotMan/p/5390336.html
- redis 面试题1 有用
1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库 ...
- LINK : fatal error LNK1104: cannot open file "mfc42d.lib"
VC++6.0上建立了个基于MFC应用程序,在编译时候没出现错误,但在LINK的是时候出现这样的错误:Linking...LINK : fatal error LNK1104: cannot open ...
- java的内部编码
java运行时,内存中使用的字符编码是unicode. 在编译java程序时,若我们不指定源程序文件的编码格式,JDK首先获得操作系统的file.encoding参数(它保存的就是操作系统默认的编码格 ...
- Netty之Reactor模式
无论是C++还是Java编写的网络框架,大多数都是基于Reactor模式进行设计和开发,Reactor模式基于事件驱动,特别适合处理海量的I/O事件. 1. 单线程模型 Reactor单线程模型,指的 ...
- CF 1097D Makoto and a Blackboard
算是记一下昨天晚上都想了些什么 官方题解 点我 简单题意 给定两个正整数$n$和$k$,定义一步操作为把当前的数字$n$等概率地变成$n$的任何一个约数,求$k$步操作后的期望数字,模$1e9 + ...
- extends注意事项
属性可以在子类中被调用,而局部变量不可以