题目描述:

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

思路:

题目是说在一棵树上删除节点,删除一个节点那么它的子树都要被删除,删除根节点1后就结束,删除的过程有个操作步数,不同的操作对应着操作步数。问这个操作步数的期望是多少。

刚开始:这怎么做?在画了几个图后发现好像有点意思,我想的是我现在从1步删完开始,假设一步删完,有一种操作,即直接删除根节点。我要两步删完,就要最后一步留着删根节点,第一步随便删一个节点。我要三步删完,最后一步删根节点,还有两步要删掉两个节点。于是以此列出了计算公式,然而之后我发现了问题,这个删两个节点是有顺序的啊,如果删了一个节点后它的字节点都不存在了,因此也不能删除了。于是我陷入了混乱中。

后来感觉到这个删除的顺序是跟节点本来的深度有关的,比这一步删的节点深度大的节点全部对步数不起作用,而另一方面,既然能删到这个节点,说明他的所有父结点都没有被删除。节点i对步数的贡献是1步,而能删到这个节点的概率是\(\frac{1}{depth[i]+1}\)(只要保证前面的没删过就行),因此最终的期望是\(\sum_i\frac{1}{depth[i]+1}\)。

代码的实现树的表示用到了链式前向星。如果有疑问可以看上一篇博客(嘻嘻)。

代码:

#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;
}

参考文章:

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

Codeforces A. Game on Tree(期望dfs)的更多相关文章

  1. Codeforces 620E New Year Tree(DFS序 + 线段树)

    题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...

  2. Codeforces 280C Game on Tree 期望

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

  3. Codeforces 348B:Apple Tree(DFS+LCM+思维)

    http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...

  4. CodeForces 620E"New Year Tree"(DFS序+线段树+状态压缩)

    传送门 •题意 给你一颗 n 个节点的树,每个节点被染上了颜色: 有 m 次操作,每次操作的类型有两种 1 v c : 将以 v 为根的子树的结点全部涂成 c 2 v : 询问以 v 为根的子树的结点 ...

  5. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  6. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  7. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  8. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  9. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

随机推荐

  1. swoole实验版聊天室

    “swoole实验版聊天室”是依据一堂swoole培训课内容改编的,结合了bootstrap前端框架.redis数据库.jquery框架等实现基本功能,只是体现了swoole的应用,并不是为了专门写个 ...

  2. kubernetes之Scheduler原理分析

    scheduler在整个系统承担了承上启下的重要功能 承上值负责接受Controller Manager创建新的pod,安排目标Node 旗下指安置工作完成后,目标Node上的kubelet服务进程接 ...

  3. Prometheus监控实战day1-监控简介

    福利 Prometheus监控实战PDF电子书下载 链接:https://pan.baidu.com/s/1QH4Kvha5g70OhYQdp4YsfQ 提取码:oou5 若你喜欢该资料,请购买该资料 ...

  4. Spring的@Autowired和@Resource注入

    @Autowired的原理 Spring@Autowired注解与自动装配 @Autowired 与@Resource的区别(详细) spring不但支持自己定义的@Autowired注解,还支持几个 ...

  5. c++动态链接问题

    https://blog.csdn.net/liu0808/article/details/81169173 https://blog.csdn.net/f110300641/article/deta ...

  6. LINGO与EXCEL之间的数据传递

    前言 LINGO 作为非线性规划运算的专用软件,得出结果一般都是纯文本的一列数据,要想将数据呈现到论文当中,需要整理到 EXCEL 中,使用复制粘贴容易出错还费时,所以必须要动用函数来提高效率! 案例 ...

  7. Linux下将.Asp Core 部署到 Docker容器中

    我们来部署一个简单的例子: 将一个简单的.Aps Core项目部署到Docker容器中并被外网访问 说明: 下面的步骤都是建立在宿主服务器系统已经安装配置过Docker容器,安装Docker相对比较简 ...

  8. LeetCode 1259. Handshakes That Don't Cross - Java - DP

    题目链接:https://leetcode-cn.com/problems/handshakes-that-dont-cross/ You are given an even number of pe ...

  9. K8s-yaml的使用及命令

    YAML配置文件管理对象 对象管理: # 创建deployment资源 kubectl create -f nginx-deployment.yaml # 查看deployment kubectl g ...

  10. Codeforces Round #499 (Div. 1)

    Codeforces Round #499 (Div. 1) https://codeforces.com/contest/1010 为啥我\(\rm Div.1\)能\(A4\)题还是\(\rm s ...