传送门:280C - Game on Tree


不知道期望是啥的请自行Baidu或Google,(溜了

题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数

那我们就来想,如果要计算一个节点的期望的话每个节点和它的祖先是在决策范围内的,所以它的子树我们可以先不用管,需要预处理出每一个点有几个祖先,当然还要加上它本身

因为每一个点的随机变量都是1,所以只需要将概率计算出来求一个和就行,注意题目要求控制精度了。

代码(:逃

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int MAXN = (int(1e5)+3)*2; int N, F[MAXN];
double Ans;
int u[MAXN], v[MAXN], first[MAXN], next[MAXN]; inline int read() {
int x = 0, f = 1;
char c = getchar();
while(c < '0'||c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c <= '9'&&c >= '0') {
x = x*10+c-'0';
c = getchar();
}
return x * f;
} void dfs(int x, int from) {
int k = first[x];
while(k != -1) {
if(v[k] == from) {k = next[k]; continue;}
F[v[k]] = F[u[k]]+1;
dfs(v[k], u[k]);
k = next[k];
}
} int main() {
N = read();
memset(first, -1, sizeof(first));
for(int i=1; i<=(N-1)*2; i++) {
u[i] = read(), v[i] = read();
next[i] = first[u[i]];
first[u[i]] = i;
i++;
u[i] = v[i-1], v[i] = u[i-1];
next[i] = first[u[i]];
first[u[i]] = i;
}
F[1] = 1;
dfs(1, 0);
for(int i=1; i<=N; i++) {
Ans += 1.0/double(F[i]);
}
printf("%.10lf", Ans);
}

  

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 期望

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

  3. CF|codeforces 280C Game on Tree

    题目链接:戳我 大概题意:给一棵树,然后每次可以删除一个子树,问你期望多少次能把整棵树都删完? 概率和期望是个神仙..我不会 对于这个题,我们要有一个前置知识--期望的线性性,就是说总期望的值等于各个 ...

  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. my.os.ClickThisWindow.ClickThisPoint.py

    my.os.ClickThisWindow.ClickThisPoint.py

  2. go8---函数function

    package main /* 函数function Go 函数 不支持 嵌套.重载和默认参数. 但支持以下特性: 无需声明原型(C语言在使用函数之前需要声明函数的原型).不定长度变参.多返回值.命名 ...

  3. CodeFirst建模:DataAnotation

    示例一 新建一个控制台应用程序,并安装entityframework 新建一个文件Blog.cs类,输入以下代码: using System.ComponentModel.DataAnnotation ...

  4. Java 泛型 三

    一.泛型初衷 Java集合不会知道我们需要用它来保存什么类型的对象,所以他们把集合设计成能保存任何类型的对象,只要就具有很好的通用性.但这样做也带来两个问题: –集合对元素类型没有任何限制,这样可能引 ...

  5. swift中使用GCDMulticastDelegate

    在开源库XMPPFramework中提供了一个GCDMulticastDelegate类,使用它可以为一个对象添加多个被委托的对象,以前用oc编写的工程引入了这个类,使用起来十分方便.最近由于换了工作 ...

  6. 在 Vue 项目中(vue-cli2,vue-cli3)使用 pug 简化 HTML 的编写

    使用 pug 的原因: 使得 HTML 写起了来更加清晰和快捷 用法: Vue 的用法没有变化: <template lang="pug"> transition(na ...

  7. Java并发编程系列之CyclicBarrier详解

    简介 jdk原文 A synchronization aid that allows a set of threads to all wait for each other to reach a co ...

  8. 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头

    如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...

  9. 洛谷 P1880 [NOI1995]石子合并

    题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...

  10. WPF 添加 gif 图片

    1. 如何在wpf窗体中添加gif动态图片: https://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-wo ...