[codeforces696B]Puzzles
1 second
Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.

Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:
let starting_time be an array of length n
current_time = 0
dfs(v):
current_time = current_time + 1
starting_time[v] = current_time
shuffle children[v] randomly (each permutation with equal possibility)
// children[v] is vector of children cities of city v
for u in children[v]:
dfs(u)
As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).
Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.
The first line of input contains a single integer n (1 ≤ n ≤ 105) — the number of cities in USC.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC.
In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].
Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.
7
1 2 1 1 4 4
1.0 4.0 5.0 3.5 4.5 5.0 5.0
12
1 1 2 2 4 4 3 3 1 10 8
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 题解:这道题还相对比较简单……
题意大概就是求每个点的期望dfs序,显然f[1]=1是递推边界
我们考虑对于某一个节点i,设dfs序为f[i],子树大小为size[i],那么f[i]=f[father]+兄弟的贡献+1
而兄弟产生贡献的话,就取决于dfs的先后顺序,如果先dfs兄弟,会产生size[兄弟]的贡献
那么的兄弟的总贡献就是兄弟的子树大小之和/2,即(size[father]-1-size[rt])/2
那么本题就得到了解决,代码见下:
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
int n,fa[N],size[N],e,adj[N];
struct node{int zhong,next;}s[N];
double f[N];
inline void add(int qi,int zhong)
{s[++e].zhong=zhong;s[e].next=adj[qi];adj[qi]=e;}
void dfs1(int rt)
{
size[rt]=;
for(int i=adj[rt];i;i=s[i].next)
dfs1(s[i].zhong),size[rt]+=size[s[i].zhong];
}
void dfs2(int rt)
{
for(int i=adj[rt];i;i=s[i].next)
{int u=s[i].zhong;f[u]=f[rt]+(size[rt]--size[u])/2.0+;dfs2(u);}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&fa[i]),add(fa[i],i);
dfs1();f[]=;dfs2();
for(int i=;i<=n;i++)
printf("%.8lf ",f[i]);
}
[codeforces696B]Puzzles的更多相关文章
- 【codeforces 696B】 Puzzles
http://codeforces.com/problemset/problem/696/B (题目链接) 题意 给出一棵树,随机dfs遍历这棵树,求解每个节点的期望dfs序. Solution 考虑 ...
- codeforces A. Puzzles 解题报告
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...
- What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?
QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...
- C puzzles详解
题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- 《algorithm puzzles》——谜题
这篇文章开始正式<algorithm puzzles>一书中的解谜之旅了! 狼羊菜过河: 谜题:一个人在河边,带着一匹狼.一只羊.一颗卷心菜.他需要用船将这三样东西运至对岸,然而,这艘船空 ...
- 《algorithm puzzles》——概述
这个专题我们开始对<algorithm puzzles>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. ...
- Puzzles
Puzzles Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 thr ...
随机推荐
- rem自适应布局
rem自适应原理 rem是根据html的font-size大小来变化,正是基于这个出发,我们可以在每一个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局.更多介绍请看这篇文章:rem是 ...
- Oracle的集合运算符
Oracle的集合运算符有并集union.union all,交集intersect,差集minus 先建表myemp,进行集合运算的测试 create table myemp as select * ...
- ubuntu18.04安装mongoDB 4.0
STEP 1: 在终端输入GPK码 $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334B ...
- 【洛谷】题解 P1056 【排座椅】
题目链接 因为题目说输入保证会交头接耳的同学前后相邻或者左右相邻,所以一对同学要分开有且只有一条唯一的通道才能把他们分开. 于是可以吧这条通道累加到一个数组里面.应为题目要求纵列的通道和横列的通道条数 ...
- hbase和ZooKeeper集群安装配置
一:ZooKeeper集群安装配置 1:解压zookeeper-3.3.2.tar.gz并重命名为zookeeper. 2:进入~/zookeeper/conf目录: 拷贝zoo_sample.cfg ...
- JavaScript学习笔记(四)——DOM
第五章 网页交互——文本对象模型[Document object model] 1 简单介绍DOM,dom是将html与javascript进行交互的工具. [使用innerHTML时注意:html中 ...
- Mysql 单表主从同步
先配主从同步,后将主库表老数据传输到从库 说明:api-server的数据库为主,其他harbor为从 1.master 配置文件更改 [mysqld] log-bin = mysql-bin ser ...
- Maven私有仓库搭建以及使用
一.使用Docker安装Nexus Docker search nexus docker pull docker.io/sonatype/nexus3 mkdir -p /usr/local/nexu ...
- Python学习小目录汇总
python其他知识目录 python基础知识-1 1.typora软件使用 2.python解释器安装 3.Python解释器环境变量添加 4.计算机编码知识: 5.输出print(): 6.变量 ...
- 5 种使用 Python 代码轻松实现数据可视化的方法
数据可视化是数据科学家工作中的重要组成部分.在项目的早期阶段,你通常会进行探索性数据分析(Exploratory Data Analysis,EDA)以获取对数据的一些理解.创建可视化方法确实有助于使 ...