Codeforces A. Game on Tree(期望dfs)

Subject description:

Game on Tree

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 ≤ \(10^5\)) — 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

Copy

21 2

Output

Copy

1.50000000000000000000

Input

Copy

31 21 3

Output

Copy

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

Ideas:

Entitled to say delete nodes in a tree, remove a node then its sub-tree must be removed, remove the root node 1 after the end of the process there is a delete operation the number of steps, different operations corresponds to operating a few steps. Asked the desired operation number of steps is.

Beginning: how to do this? After painting found several figures seems a bit mean, I think that I am now completely deleted from step 1 to start, assuming that step delete finished, there is an operation, that is, delete the root node. I want to delete a two-step complete, the final step necessary to keep delete the root node, the first step just delete a node. I want to complete three steps to delete, delete the last step of the root node, there are two steps to delete two nodes. So this shows the calculation formula, but then I found the problem, delete the two nodes are ordered ah, if it deleted a byte node points do not exist, and therefore can not be deleted. So I fell into chaos.

Later feel the order is deleted with the original node, node large depth than the depth of this step is to delete all of the nodes on the number of steps does not work, but on the other hand, since this can delete the node, indicating his all parent nodes have not been deleted. Node i contribution to the number of steps is a step, and the probability can be deleted to the node is \ (\ FRAC {1} {depth [i] + 1'd} \) (as long as the foregoing have not deleted through the line), so the final the expectation is \ (\ sum_i \ FRAC. 1 {{} depth [I] + 1'd} \) .

Source implementation of the tree represents the chain to use the former star. If you have questions you can fancy a blog (hee hee).

Code:

#include <iostream>
#include <iomanip>
using namespace std;
#define max_n 100005
int head[max_n];
struct edge
{
    int to;
    int next;
}e[max_n<<1];
int cnt = 0;
int d[max_n];
int n;
void add(int u,int v)
{
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
void dfs(int x,int from)
{
    for(int i = head[x];i;i=e[i].next)
    {
        if(e[i].to==from)
        {
            continue;
        }
        d[e[i].to] = d[x]+1;
        dfs(e[i].to,x);
    }
}
int main()
{
    cin >> n;
    for(int i = 1;i<n;i++)
    {
        int a,b;
        cin >> a >> b;
        add(a,b);
        add(b,a);
    }
    d[1] = 1;
    dfs(1,0);
    double ans = 0;
    for(int i = 1;i<=n;i++)
    {
        //cout << d[i] << endl;
        ans = ans+1.0/d[i];
    }
    cout.setf(ios_base::fixed,ios_base::floatfield);
    cout << setprecision(20) << ans << endl;
    return 0;
}

Reference article:

LeTri,Codeforces 280C. Game on Tree,https://www.cnblogs.com/LeTri/p/9157166.html

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11294160.html
Recommended