题目链接:戳我


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

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

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

    1. Grid Layout Group(网格布局) Hierachy: Game: 属性和功能: 2. 根据鼠标位置旋转界面实现: public class TiltWindow : MonoBeh ...

  2. hibernate多个主键

    在hibernate中同一张表里面存在多个主键,必须要实现序列化接口(Serializable )

  3. 【296】Python 默认 IDE 修改

    参考:找回Python IDLE Shell里的历史命令(用上下键翻历史命令怎么不好用了呢?) 参考:Python IDLE快捷键一览 参考:Python IDLE 自动提示功能 参考:如何让pyth ...

  4. Finite State Transducers

    一, 简介 Finite State Transducers 简称 FST, 中文名:有穷状态转换器.在自然语言处理等领域有很大应用,其功能类似于字典的功能(STL 中的map,C# 中的Dictio ...

  5. hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java ...

  6. Nginx源码完全注释(5)core/ngx_cpuinfo.c

    /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...

  7. java实现文件的拷贝以及文件的删除

    /** * 将文件拷贝到指定目录 * @param oldAddress 文件所在目录(文件的全路径) * @param newAddress 指定目录(包含复制文件的全名称) * @throws E ...

  8. copy&mutableCopy 浅拷贝(shallow copy)深拷贝 (deep copy)

    写在前面 其实看了这么多,总结一个结论: 拷贝的初衷的目的就是为了:修改原来的对象不能影响到拷贝出来得对象 && 修改拷贝出来的对象也不能影响到原来的对象 所以,如果原来对象就是imm ...

  9. Ubuntu Server 12.04 LTS搭建SVN服务及修改端口

    采用了apache结合svn的方式. 首先安装apache.subversion.svn-apache sudo apt-get install apache2 sudo apt-get instal ...

  10. 谈谈Spring 注入properties文件总结

    本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...