题目链接:戳我


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

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

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

    起因 因为工作需要用到,所以需要学习hadoop,所以记录这篇文章,主要分享自己快速搭建hadoop环境与运行一个demo 搭建环境 网上搭建hadoop环境的例子我看蛮多的.但是我看都比较复杂,要求 ...

  3. Lists、Sets、Maps和Collections2的使用

    1.Lists //Lists System.out.println("### Lists ###"); ArrayList<String> arrayList = L ...

  4. 【原】使用puppeteer爬虫下载Midi文件

    The Beatles 乐队的 Midi文件下载地址 puppeteer官方github地址 midi文件爬取示例代码github地址 1.安装npm 参考:安装npm及cnpm(Windows) 修 ...

  5. Elasticsearch-PHP 安装

    安装 Elasticsearch-PHP只有三个要求你需要担心: PHP 5.3.9 或更高版本(查看更多信息) Composer ext-curl: Libcurl的PHP扩展 其它的依赖会通过Co ...

  6. springmvc基本知识点

    springmvc高级知识:

  7. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  8. 获取网页上的所有QQ号码,并生成exel报表

    需要的jar如下: package jsoup.zr.com.utils; /** * * @author LF * */ public class Constant { /** * 网站链接地址ַ ...

  9. Qt自定义插件编程小结

    qt自定义组件开发步骤演示.以下所有步骤的前提是自己先编译Qtcreator源码,最好生成release版的QtCreator,否则自定义的插件嵌入QtCreator会失败!!!(这个网上教程很多) ...

  10. Qt5.7学习

    一 Qt简介(Build your world with Qt) 二 Qt5.7.0的安装 三 Qt系统构造库及常用类 四 信号(signal)与槽(slot)通信机制 五 QtDesigner开发工 ...