题目描述:

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

  1. 21 2

Output

Copy

  1. 1.50000000000000000000

Input

Copy

  1. 31 21 3

Output

Copy

  1. 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}\)。

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

代码:

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. #define max_n 100005
  5. int head[max_n];
  6. struct edge
  7. {
  8. int to;
  9. int next;
  10. }e[max_n<<1];
  11. int cnt = 0;
  12. int d[max_n];
  13. int n;
  14. void add(int u,int v)
  15. {
  16. e[++cnt].to = v;
  17. e[cnt].next = head[u];
  18. head[u] = cnt;
  19. }
  20. void dfs(int x,int from)
  21. {
  22. for(int i = head[x];i;i=e[i].next)
  23. {
  24. if(e[i].to==from)
  25. {
  26. continue;
  27. }
  28. d[e[i].to] = d[x]+1;
  29. dfs(e[i].to,x);
  30. }
  31. }
  32. int main()
  33. {
  34. cin >> n;
  35. for(int i = 1;i<n;i++)
  36. {
  37. int a,b;
  38. cin >> a >> b;
  39. add(a,b);
  40. add(b,a);
  41. }
  42. d[1] = 1;
  43. dfs(1,0);
  44. double ans = 0;
  45. for(int i = 1;i<=n;i++)
  46. {
  47. //cout << d[i] << endl;
  48. ans = ans+1.0/d[i];
  49. }
  50. cout.setf(ios_base::fixed,ios_base::floatfield);
  51. cout << setprecision(20) << ans << endl;
  52. return 0;
  53. }

参考文章:

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. 记录一次TraceId的问题

    多服务部署的时候,各个服务通过httpClient进行调用时候,有时候出现问题,需要进行追查.但是如果没有一个标记,就会很迷茫,特别是多个服务来回调用,就无法快速定位问题.这个时候一般是使用MDC的 ...

  2. 消息中间件 kafka rabbitmq 选型差异

    https://www.zhihu.com/question/43557507 https://baijiahao.baidu.com/s?id=1610644333184173190&wfr ...

  3. WIFI-Direct(Wifi直连)、AirPlay、DLAN、Miracast功能介绍

    不知道大家对无线同屏技术有多少了解,当这种技术普及的时候,我想我们的工作与生活又会方便很多吧!下面是目前三种主流同屏技术的介绍: 目前这种将终端信号经由WiFi传输到电视.电视盒的技术有三种:DLNA ...

  4. Selenium+java - 操作滚动条

    前言 在写脚本时,总会遇到一种情况,就是当滚动拉倒最下面了,表单或者下拉框.按钮这些元素未在当前页面展示,而webdriver提供的方法都是操作当前页面可见的元素,这时我们使用JavaScript操作 ...

  5. P4Merge的使用

    (官网: https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge 可以作为一个stand alone app ...

  6. Git_从远程branch取回所有最新代码,暴力覆盖本地 && GIT基本结构

    假设你本地有一个xx分支对应着远端的xx分支,当前,你在本地的xx分支进行了修改(可以是执行了add, commit,但不要push),然后,现在想从远端的xx分支拿到最新的代码,可以用下图方法覆盖掉 ...

  7. c和c++中的枚举和 区别

    1.c中的枚举 c语言枚举 void test(){ // enum 枚举类型名字{枚举值, 枚举值, 枚举值}; enum WEEK { Mon, Tue };// 枚举类型定义 enum WEEK ...

  8. Delphi 开发微信公众平台 (三)- 获取微信服务器IP地址

    如果公众号基于安全等考虑,需要获知微信服务器的IP地址列表,以便进行相关限制,可以通过该接口获得微信服务器IP地址列表或者IP网段信息. 接口调用请求说明 http 请求方式: GET https:/ ...

  9. Java的访问修饰符的作用范围

    访问修饰符: private default protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 ...

  10. CentOS7 安装 Docker、最佳Docker学习文档

    目录 一.Docker支持 二.安装Docker -1.在新主机上首次安装Docker CE之前,需要设置Docker存储库.之后,就可以从存储库安装和更新Docker. 0.卸载旧版 1.正式安装 ...