题目描述:

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. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

  2. jmeter—建立测试计划

    一个测试计划描述了一系列 Jmeter 运行时要执行的步骤.一个完整的测试计划包含 一个或者多个线程组,逻 辑控制,取样发生控制,监听器,定时器,断言和配置元件. 一. 建立测试计划 在这一部分,你将 ...

  3. 缓解DDoS && cc 的最佳Linux内核设置 (转)

    https://javapipe.com/blog/iptables-ddos-protection/ kernel.printk = 4 4 1 7 kernel.panic = 10 kernel ...

  4. MySQL报错Packet for query is too large问题解决

    今天用java写了批量插入运行时,报错: Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigExcep ...

  5. 【jquery】【jqGrid】设置不能多选

    onSelectAll:function(rowids,statue){ layui.layer.msg("请选择单条记录"); $("#jqGrid").jq ...

  6. Ubuntu下重启mysql

    启动mysql: 方式一:sudo /etc/init.d/mysql start 方式二:sudo service mysql start 停止mysql: 方式一:sudo /etc/init.d ...

  7. 理解SQL Server中索引的概念,原理以及其他(转载)

    简介 在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能.但索引可以在大多数情况下大大提升查询性能,在OLAP中尤其明显.要完全理解索 ...

  8. [转帖]从0开始的高并发(一)--- Zookeeper的基础概念

    从0开始的高并发(一)--- Zookeeper的基础概念 https://juejin.im/post/5d0bd358e51d45105e0212db 前言 前面几篇以spring作为主题也是有些 ...

  9. PV、UV、UIP、VV、DAU、CTR指的是什么?

    PV(page view) 网站浏览量,指网页的浏览次数,用户每打开一次页面就记录一次PV,多次打开则累加. UV(unique vistor) 独立访客数,指的是某一天访问某站点的人数,以cooki ...

  10. elasticsearch插件

    bigdisk安装: 1.下载地址http://bigdesk.org/,注意和elasticsearch的版本对应关系 2.将文件上传到服务器elasticsearch的plugin目录下,解压 3 ...