CodeForces 280C Game on Tree题解理解

题目链接
C. Game on Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree.

The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let’s denote it by v) and removes all the subtree nodes with the root in node v from the tree. Node v gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1.

Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game.

Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of nodes in the tree. The next n - 1 lines contain the tree edges. The i-th line contains integers a i, b i (1 ≤ a i, b i ≤ n; a i ≠ b i) — the numbers of the nodes that are connected by the i-th edge.

It is guaranteed that the given graph is a tree.

Output
Print a single real number — the expectation of the number of steps in the described game.

The answer will be considered correct if the absolute or relative error doesn’t exceed 10 - 6.

Examples
input
2
1 2
output
1.50000000000000000000
input
3
1 2
1 3
output
2.00000000000000000000
Note
In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are:

1 × (1 / 2) + 2 × (1 / 2) = 1.5
In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are:

1 × (1 / 3) + (1 + 1.5) × (2 / 3) = (1 / 3) + (5 / 3) = 2

题解:
在这里插入图片描述
考虑简单情况如果树为链状

A
B
C
D
E

试图理解为
对于根节点A来说必定需要期望花1步来去掉他
(在A点没被去除的基础上,即用了1/2的概率)
对于节点B来说期望 2 − 1 2 × 0 + 1 2 × 1 = 1 2 \frac{2-1}{2} \times 0+ \frac{1}{2} \times 1 = \frac{1}{2} 221×0+21×1=21步来去掉他
(在A、B点没被去除的基础上,即用了1/3的概率)
对于节点C来说期望 3 − 1 3 × 0 + 1 3 × 1 = 1 3 \frac{3-1}{3} \times 0+ \frac{1}{3} \times 1 = \frac{1}{3} 331×0+31×1=31步来去掉他
对于深度为 d d d的节点来说期望 d − 1 d × 0 + 1 d × 1 = 1 d \frac{d-1}{d} \times 0+ \frac{1}{d} \times 1 = \frac{1}{d} dd1×0+d1×1=d1步来去掉他
所以最终 a n s = ∑ i = 0 n 1 d e p t h [ i ] ans = \sum_{i=0}^{n}\frac{1}{depth[i]} ans=i=0ndepth[i]1
即多出来一个节点就需要多花一点期望步数去去除他

现考虑B节点,对于比B节点更深的节点(C、D等)他们的去除并不会造成B节点的去除,那么他们对去除B的期望步数没有贡献

或者理解为大问题化小问题
去除E需要花1/5的概率挑到他然后花一步去掉他(期望1/5*1=1/5)
然后树链就变成A->B->C->D
然后继续重复考虑

还在思考QAQ

其他参考
1.CodeForces 280C 浅谈期望线性性的树上问题实际运用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值