Game on Tree (Codeforces 280C)
Tags: Implement, probabilities

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 ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — 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.

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.5In 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

Sample Inputs:
2
1 2

3
1 2
1 3

Sample Outputs:
1.50000000000000000000

2.00000000000000000000

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    (u, v) = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)
ans = 0
q = [[0, -1, 1, 0, 0]]
while q:
    (i, fa, s, now_x, state) = q[-1]
    if state:
        q.pop()
        continue
    if now_x == 0:
        ans += 1 / s
    if now_x < len(g[i]):
        if g[i][now_x] != fa:
            q[-1][-2] += 1
            q.append([g[i][now_x], i, s + 1, 0, 0])
        else:
            q[-1][-2] += 1
    else:
        q[-1][-1] = 1
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    (u, v) = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)
ans = 0
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

@bootstrap
def f(i, fa, s):
    global ans
    ans += 1 / s
    for x in g[i]:
        if x != fa:
            yield f(x, i, s + 1)
    yield
f(0, -1, 1)
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    (u, v) = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)
ans = 0
from types import GeneratorType

def bootstrap(f):
    stack = []

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

@bootstrap
def f(i, fa, s):
    global ans
    ans += 1 / s
    for x in g[i]:
        if x != fa:
            yield f(x, i, s + 1)
    yield
f(0, -1, 1)
print(ans)