Codeforces 280 C. Game on Tree (概率与期望)
2017-06-14 14:44
519 查看
题目描述
传送门
题目大意:给定一棵有根树,每次随机选一个未被删除的点,将以它为根的子树删除。求删除整棵树所用的期望步数。题解
只有删除掉1号点才算结束。考虑一个点被选中删除的概率。P(i)=1dep[i],dep[i]表示i点的深度,其中1号根节点的深度为1.
只有他到根路径上的点都没有被选中,他才能被选中。
期望步数实际上就是选中的点的期望次数。
所以E(步数)=∑ni=1P(i)=∑ni=11dep[i]
代码
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 200003 using namespace std; int n,tot,point ,nxt ,v ,dep ; void add(int x,int y) { tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; } void dfs(int x,int fa) { dep[x]=dep[fa]+1; for (int i=point[x];i;i=nxt[i]){ if (v[i]==fa) continue; dfs(v[i],x); } } int main() { freopen("a.in","r",stdin); scanf("%d",&n); for (int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y); } dfs(1,0); double ans=0; for (int i=1;i<=n;i++) ans+=(double)(1.0/dep[i]); printf("%.9lf\n",ans); }
相关文章推荐
- Codeforces 280C Game on Tree 概率dp 树上随机删子树 求删完次数的期望
- CodeForces 280C Game on Tree 概率
- Codeforces 280C Game on Tree 树形期望dp
- Codeforces 280C - Game on Tree
- codeforces 280C Game on Tree
- codeforces 220 C. Game on Tree
- ZOJ 3329 One Person Game(概率DP,求期望)
- codeforces 16E Fish (概率-期望DP)
- CodeForces 29D - Ant on the Tree 暴力LCA
- codeforces 621C Wet Shark and Flowers (概率、期望)
- 【Codeforces Round 275 (Div 2)E】【状压DP 概率DP求期望 线性相加思想 二进制系统函数】Game with Strings n个串目标串随机猜位置随机的猜出答案的期望步
- CodeForces -Game on Paper
- [Codeforces741D]Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on the tree)
- Codeforces-Strange Game On Matrix(暴力直接写)
- AtCoder Grand Contest 017 D - Game on Tree
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
- Codeforces 28C Bath Queue - 动态规划 - 组合数学 - 概率与期望
- codeforces 246 E. Blood Cousins Return (set+dsu on the tree)
- [Dsu on tree]CodeForces 600 E
- Second price auction - CodeForces 513 C 概率期望