*【CodeForces - 280C】Game on Tree(期望模型,期望的线性性)

题干:

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 aibi(1 ≤ ai, bi ≤ nai ≠ 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.

Examples

Input

2
1 2

Output

1.50000000000000000000

Input

3
1 2
1 3

Output

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

题目大意:

给一颗n个点有根的树,每次任意删一个当前还存在的点,并删掉其子树,问删完整颗树的删点次数的数学期望。

解题报告:

琢磨了半天,似懂非懂,先记下来以后再加深理解吧。

考虑等价问题:对于每一个点,假设考虑第i个点,选择删第i个点的平均次数 的和。

既然最终第i个点肯定要被删除的,那么肯定是他和他到根节点这一条链上的节点 都可以做到,那么其中假设深度为d,那么选择这个点的概率就是\frac{1}{d} \times 1 + \frac{d-1}{d} \times 0 = \frac{1}{d},那么对所有的点求个和就是答案。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int dep[MAX],n;
vector<int> vv[MAX];
void dfs(int cur,int fa,int d) {dep[cur] = d;for(int i = 0; i<vv[cur].size(); i++) {if(vv[cur][i] == fa) continue;dfs(vv[cur][i],cur,d+1);}
}
int main()
{cin>>n;for(int a,b,i = 1; i<=n-1; i++) cin>>a>>b,vv[a].pb(b),vv[b].pb(a);dfs(1,-1,1);double ans = 0;for(int i = 1; i<=n; i++) {ans += 1.0/dep[i];}printf("%.10f\n",ans);return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440066.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

武侠风云(基础版)

基本任务&#xff1a; 1 建立角色类&#xff0c;角色拥有生命值的属性和攻击的方法&#xff0c;攻击值是随机的。 2 建立职业子类&#xff0c;刀客&#xff0c;&#xff08;伤害少&#xff0c;血量多&#xff09;剑客&#xff08;伤害正常&#xff0c;血量正常&#xff0c;有几…

机器学习笔记(十六):大规模机器学习

目录 1&#xff09;Learning with large datasets 2&#xff09;Stochastic gradient descent 3&#xff09;Mini-batch gradient descent 4&#xff09;Stochastic gradient descent convergence 1&#xff09;Learning with large datasets 回顾一下我们之前提到的这句…

【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)

题干&#xff1a; There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1, K…

智能聊天机器人系统

# 智能聊天机器人系统 # 1.系统简介 # 随着社会的各个公司以及大学对人工智能技术的深入研究和快速发展&#xff0c;人工智能技术将逐步应用到 # 方方面面。智能聊天机器人系统是基于各类传感器收集人类语音数据&#xff08;智能电视、智能空调&#xff0c; # 智能冰箱、智能音…

机器学习笔记:总结

下面是我们本课程学到的要点&#xff1a; 1&#xff09;监督学习&#xff1a;线性回归&#xff0c;逻辑回归&#xff0c;神经网络&#xff0c;SVM&#xff1b; 2&#xff09;无监督学习&#xff1a;k均值&#xff0c;PCA&#xff0c;异常检测 3&#xff09;特别应用&#xf…

ROS探索总结(十二)——坐标系统

在机器人的控制中&#xff0c;坐标系统是非常重要的&#xff0c;在ROS使用tf软件库进行坐标转换。 相关链接&#xff1a;http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一、tf简介 我们通过一个小小的实例来介绍tf的作用。 1、安装turtle包 <span>$ rosdep instal…

【BZOJ - 3036】绿豆蛙的归宿(概率DAG图dp,拓扑排序,概率dp,期望的线性性)

题干&#xff1a; 随着新版百度空间的下线&#xff0c;Blog宠物绿豆蛙完成了它的使命&#xff0c;去寻找它新的归宿。 给出一个有向无环的连通图&#xff0c;起点为1终点为N&#xff0c;每条边都有一个长度。绿豆蛙从起点出发&#xff0c;走向终点。 到达每一个顶点时&#x…

【LightOJ - 1079】Just another Robbery(概率dp,概率背包)

题干&#xff1a; As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermi…

强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)

目录 一、TensorFlow 二、Keras 三、Pytorch 总结 本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 TensorFlow、Keras和Pytorch是目前深度学习的主要框架&#xff0c;也是入门深度学习必须掌握的三大框架&#xff0c;但…

【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干&#xff1a; You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors. If you choose the ith door, it can either take you back to the same position wh…

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…

【BZOJ - 3450】Tyvj1952 Easy(数学期望,期望的线性性)

题干&#xff1a; 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&#xff0c;分数是按comb计算的&#xff0c;连续a个comb就有a*a分&#xff0c;comb就…

吐血推荐收藏的学位论文排版教程(完整版)

目录 01.保存的高级选项设置 02.纸张设置为A4纸大小 03.设置页边距和装订线距离 04.度量衡的设置 05.创建“论文正文”样式 06.修改论文正文样式 07.设置并修改标题样式 08.给中英文摘要、附录、等大标题套用样式 09.两个Word文档之间进行并排查看 12.一键生成多级列…

由浅到深理解ROS(7)-URDF

转自 相信许多爱好者止步于昂贵的机器人价格。虽然有了Arduino&#xff0c;但一个载重能力很弱的机器人小车成本也得一两百块钱。搭建自己的机器人更是费时费力。 所以如果你只是普通机器人爱好者&#xff0c;或者还没想好要拿机器人做什么&#xff0c;那我们还是从模拟器开始…