【codeforces 696B】 Puzzles
http://codeforces.com/problemset/problem/696/B (题目链接)
题意
给出一棵树,随机dfs遍历这棵树,求解每个节点的期望dfs序。
Solution
考虑对于节点u,其某个儿子节点v的期望是多少。
首先,节点u的儿子的dfs的顺序是其儿子数son[x]的全排列。考虑在排列中有多少个节点在v的前面,不妨设x排在v的前面,那么满足的排列数为:${P_n^{n-2}}$,于是x对v的期望的贡献为:$${\frac{P_n^{n-2}×size[x]} {P_n^n}=\frac{1}{2}×size[x]}$$
因为节点u的每一个节点都会对v产生贡献,再算上v自己的贡献,所以v的期望:
$${f_v=\frac{size_u-1-size[v]}{2}+1+f[u]}$$
代码
// codeforces696B
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct edge {int to,next;}e[maxn<<1];
int head[maxn],size[maxn],n,cnt;
double f[maxn]; void link(int u,int v) {
e[++cnt]=(edge){v,head[u]};head[u]=cnt;
}
void dfs(int x) {
size[x]=1;
for (int i=head[x];i;i=e[i].next) {
dfs(e[i].to);
size[x]+=size[e[i].to];
}
}
void dp(int x) {
for (int i=head[x];i;i=e[i].next) {
f[e[i].to]=(double)(size[x]-1-size[e[i].to])/2+1+f[x];
dp(e[i].to);
}
}
int main() {
scanf("%d",&n);
for (int x,i=2;i<=n;i++) {
scanf("%d",&x);
link(x,i);
}
dfs(1);
f[1]=1;dp(1);
for (int i=1;i<=n;i++) printf("%.6lf ",f[i]);
return 0;
}
【codeforces 696B】 Puzzles的更多相关文章
- 【64.52%】【codeforces 697D】Puzzles
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- PowerDesigner 常用设置
1.使用 JDBC 方式连接 Oracle 逆向生成数据库 PDM 使用 ODBC 方式连接 Oracle 数据库可以借鉴这位兄弟的博客:http://www.cnblogs.com/clivehua ...
- LDAP注入与防御解析
[目录] 0x1 LDAP介绍 0x2 LDAP注入攻击及防御 0x3 参考资料 0x1 LDAP介绍 1 LDAP出现的背景 LDAP(Lightweight Directory Access Pr ...
- Atitit 衡量项目的规模
Atitit 衡量项目的规模 1. 预估衡量项目的规模的方法1 1.1. 方法一.Delphi 法1 1.2. 方法二. 类比法1 1.3. 方法三.功能点估计法1 1.4. 方法四.PERT估计法2 ...
- [Android]使用Dagger 2依赖注入 - API(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5092525.html 使用Dagger 2依赖注入 - API ...
- 【代码笔记】iOS-用户发布后能保存崩溃
一,工程图. 二,代码. AppDelegate.m #import "AppDelegate.h" #import "RootViewController.h" ...
- 模块化之Spring3.0 web fragment和gradle构建项目
1.背景 模块化开发很久以前就开始普及的概念.但是到了企业实际情况中,真正把模块化作为系统架构的核心的不多.或者说对模块化有这个意识,但是具体到底该如何实现,有些模糊,同时也许因为项目紧.任务中. ...
- 完全删除TFS2013上的项目
必备条件:您必须是 Team Foundation Server Administrators 组的成员或待删除项目的 Project Administrators 组的成员. 解决方法:打开&quo ...
- java中可定制的序列化过程 writeObject与readObject
来源于:[http://bluepopopo.iteye.com/blog/486548] 什么是writeObject 和readObject?可定制的序列化过程 这篇文章很直接,简单易懂.尝试着翻 ...
- RedHat 和 Mirantis OpenStack 产品的版本和功能汇总和对比(持续更新)
Mirantis 和 Red Hat 作为 OpenStack 商业化产品领域的两大领军企业,在行业内有重要的地位.因此,研究其产品版本发布周期和所支持的功能,对制定 OpenStack 产品的版本和 ...
- linux命令-文件命令
1.解压.tar文件 tar -vxf *.tar 2.把一个文件夹下的内容复制到另一个文件夹 将aaa内所有内容复制到bbb cp -a aaa/* /bbb/ * 3.复制文件时不改变文件的时间 ...