[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 ...
随机推荐
- L014-第三关课前linux命令及基础知识考试手把手实战解答小节
又是一周啊,以后保持一周一个微博吧. 这是一个堂解答考试题的课,那么就以题目来展开吧! 1.如何取得/etiantian文件的权限对应的数字内容,如-rw-r--r--为644,要求用命令获得644这 ...
- jquery.validate使用 - 3
自定义jquery-validate的验证行为 1: 自定义表单提交 设置submitHandler来自定义表单提交动作 $(".selector").validate({ ...
- java excel导出(表头合并,多行表头)
@RequestMapping(value="orderExcelList2") public void orderExcelList2forJava(Order order,Ht ...
- jsp 修改页面感受
什么事情只有做过才知道. 最近在负责官网的开发,有一些页面需要和前端商量着修改,但是看到jsp那繁杂的标签和各种css,js混到一起,实在觉得jsp已经是一种落后的技术了,在修改过程中频频出现各种格式 ...
- Spring Boot 整合JDBC 实现后端项目开发
一.前言 二.新建Spring Boot 项目 三.Spring Boot 整合JDBC 与MySQL 交互 3.1 新建数据表skr_user 3.2 Jdbcproject 项目结构如下 3.3 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- 启动sshd时,报“Could not load host key”错
原文发表于cu:2016-05-24 现象:启动sshd服务时,虽看似服务启动成功,但客户端并不能连接上sshd服务器端.如下: [root@aefe8007a17d ~]# /usr/sbin/ss ...
- 【ZABBIX】SNMPtrap实现主动监控的原理与安装配置
工欲善其事,必先利其器.作为一款强大的开源软件,Zabbix号称“Monitor Everything”,其所依赖的,很大程度上便是SNMP的数据采集支持.SNMP 协议是用来管理设备的协议,目前SN ...
- Literature Books
Lean In (Sheryl Sandberg) Option B (Sheryl Sandberg) Ready Player One
- WIN 7 发布项目
本人工作都快两年了不知道web 怎么发布 今天我自己非常恼火!我就暗暗研究.希望能给新手指导 准备工作:WIN7 系统 .IIS 6.0.MVC (vs 新建mvc项目自带的项目) OK begin ...