题目链接:戳我


大概题意:给一棵树,然后每次可以删除一个子树,问你期望多少次能把整棵树都删完?

概率和期望是个神仙。。我不会

对于这个题,我们要有一个前置知识——期望的线性性,就是说总期望的值等于各个子期望的和。即——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的更多相关文章

  1. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  2. Codeforces 280C - Game on Tree

    传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...

  3. Codeforces 280C Game on Tree 期望

    Game on Tree 这种题好像在wannfly训练营讲过, 我怎么又不会写啦, 我好菜啊啊啊. 我们按每个点算贡献, 一个点有贡献就说明它是被选中的点, 那么它被选中的概率就为1 / depth ...

  4. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  6. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  7. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. sql中问号是干什么的??

    第一次在后台 程序中遇到sql语句中的问号: /** * * 方法描述 : 通过账号id更新该账号状态 * @param state 状态 * @param id 账号id */ @Modifying ...

  2. Centos7安装jekyll

    1.首先需要安装相应的依赖包及所需要的工具 sudo yum install nodejs npm ruby ruby-devel rubygems git 2.修改gem源 国内 使用的淘宝的更新源 ...

  3. WPF Shader 正片叠底

    新建文本文档拷贝以下代码 //正片叠底sampler2D implicitInput : register(s0);sampler2D oldInput : register(s1);float4 m ...

  4. Python shutil 模块学习笔记

    学于https://automatetheboringstuff.com shutil 名字来源于 shell utilities,有学习或了解过Linux的人应该都对 shell 不陌生,可以借此来 ...

  5. 【BZOJ3238】差异【后缀自动机+dp】

    题意 分析 这个题目还是很优秀的.sigma(len(Ti)+len(Tj))的值是一定的=n*(n+1)*(n-1)/2.那么关键就是求任意两个后缀的lcp的和了. 我们怎么求两个后缀的lcp?如果 ...

  6. 69. Sqrt(x) (Divide-and-Conquer)

    Implement int sqrt(int x). Compute and return the square root of x. 注意: 计算平方的时候可能会溢出,所以mid要定义为long 另 ...

  7. 学习h5(开始)

    webstorm 下载地址:http://www.sdifenzhou.com/6176.html webstorm注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...

  8. Crack IDEA

    使用破解补丁 Crack IDEA→在http://idea.lanyus.com/上可以找到最新的破解补丁,下载并放到软件的bin目录下 →更改bin目录下的两个文件:Idea.exe.vmopti ...

  9. ubuntu Qt5 librealsense opencv

    08:11:47: 进程"/usr/bin/make"正常退出. 08:11:47: 配置没有改变, 跳过 qmake 步骤. 08:11:47: 正在启动 "/usr/ ...

  10. c++ std::unordered_set

    std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...