Codeforces A. Game on Tree(期望dfs)
题目描述:
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)的更多相关文章
- Codeforces 620E New Year Tree(DFS序 + 线段树)
题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...
- Codeforces 280C Game on Tree 期望
Game on Tree 这种题好像在wannfly训练营讲过, 我怎么又不会写啦, 我好菜啊啊啊. 我们按每个点算贡献, 一个点有贡献就说明它是被选中的点, 那么它被选中的概率就为1 / depth ...
- Codeforces 348B:Apple Tree(DFS+LCM+思维)
http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点 ...
- CodeForces 620E"New Year Tree"(DFS序+线段树+状态压缩)
传送门 •题意 给你一颗 n 个节点的树,每个节点被染上了颜色: 有 m 次操作,每次操作的类型有两种 1 v c : 将以 v 为根的子树的结点全部涂成 c 2 v : 询问以 v 为根的子树的结点 ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
随机推荐
- phpspreadsheet 中文文档(一) 访问单元格
2019年10月11日11:45:09 访问单元格 访问电子表格中的单元格应该非常简单.本主题列出了一些访问单元的选项. 通过坐标设置单元格值 可以使用工作表的setCellValue()方法来按坐标 ...
- 最常见的Java面试题及答案汇总(三)
上一篇:最常见的Java面试题及答案汇总(二) 多线程 35. 并行和并发有什么区别? 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生. 并行是在不同实体上的多个事 ...
- Git 配置环境
安装 在Fedora/CentOS下安装 $ sudo yum install git 在Debian/Ubuntu Linux下载安装 $ sudo apt-get install git Wind ...
- 小程序onShow
/** * 生命周期函数--监听页面加载 */ onLoad: function(options) { let that = this; const openid = app.globalData.o ...
- 【剑指offer】删除链表中重复的结点
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针.例如,链表1->2->3->3->4->4->5 处理后为 ...
- 域名解析中的cname解析和显性URL跳转和隐性URL跳转三者有什么区别
通俗的来讲,cname解析还是属于dns解析,只是把某个域名解析到另外一个域名对应的某个IP的空间中,所以还需要在服务器端(比如nginx)做域名解析(比如把baidu.com做一个cname解析到i ...
- sqlserver 数据类型 C# clr 数据类型 映射
https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/linq/sql-clr-type-mapping#default- ...
- ajax中如何使用全局变量?
在ajax中一般都是采取默认的异步请求,但是有时候参数是需要做到全局通用,这时候发起同步请求. 如下: $.ajax({ type:"post", url:"url路径& ...
- Spring MVC之@ControllerAdvice详解
本文链接:https://blog.csdn.net/zxfryp909012366/article/details/82955259 对于@ControllerAdvice,我们比较熟知的用法是 ...
- 2019 拼多多java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.拼多多等公司offer,岗位是Java后端开发,最终选择去了拼多多. 面试了很多家公司,感觉大部分公司考察的点都差 ...