D. Puzzles(Codeforces Round #362 (Div. 2))
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随即给每个点标号,求每个点标号的期望;
思路:dfs+概率dp;
我们可以知道根节点的期望为1;
然后,他的子节点都是等概率的。
dp数组是各个节点的期望。
先给样例一的图:
我们写第二层的排列
1,1 2 4 5
2,1 2 5 4
3,1 4 2 5
4,1 4 5 2
5,1 5 2 4
6,1 5 4 2
所以节点2的期望为dp[1]+((1+size(4)+size(5))*2+size(4)+1+size(5)+1+1+1)/6;
根据这个我们先试着猜想dp[v]=dp[u]+1+(size(u)-size(v)-1)/2;
这就是状态转移方程;
我们先把上面到下面一层所必须加上一步先加上,也就是dp[u]+1;
然后我们可以知道下面所有的排列中,此节点的兄弟节点,要么排在这个节点之前要么之后,所一其他节点对于该节点的贡献为size()/2;
也就是排在前面和后面是等概率的;
复杂度O(n)
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<set>
8 #include<stdlib.h>
9 #include<vector>
10 using namespace std;
11 vector<int>vec[100006];
12 long long cnt[100006];
13 long long dfs1(int n);
14 double dp[100006];
15 void dfs(int n);
16 int main(void)
17 {
18 int i,j,k;
19 while(scanf("%d",&k)!=EOF)
20 {
21 int n;memset(dp,0,sizeof(dp));
22 for(i=0; i<100006; i++)
23 {
24 cnt[i]=0;
25 vec[i].clear();
26 }
27 for(i=2; i<=k; i++)
28 {
29 scanf("%d",&n);
30 vec[n].push_back(i);
31 }
32 long long t=dfs1(1);
33 dp[1]=1.0;
34 dfs(1);printf("%.1f",dp[1]);
35 for(i=2; i<=k; i++)
36 {
37 printf(" %.1f",dp[i]);
38 }
39 printf("\n");
40 }
41 return 0;
42 }
43 long long dfs1(int n)
44 {
45 long long sum;
46 int i,j,k;
47 for(i=0; i<vec[n].size(); i++)
48 {
49 cnt[n]+=dfs1(vec[n][i]);
50 }
51 cnt[n]+=1;
52 return cnt[n];
53 }
54 void dfs(int n)
55 {
56 int i,j;
57 for(i=0; i<vec[n].size(); i++)
58 {
59 int x;
60 x=vec[n][i];
61 dp[x]=dp[n]+1.0;
62 dp[x]+=1.0*(cnt[n]-cnt[x]-1)/2.0;
63 dfs(x);
64 }
65 }
D. Puzzles(Codeforces Round #362 (Div. 2))的更多相关文章
- Codeforces Round #362 (Div. 2) D. Puzzles
D. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- 【转载】【树形DP】【数学期望】Codeforces Round #362 (Div. 2) D.Puzzles
期望计算的套路: 1.定义:算出所有测试值的和,除以测试次数. 2.定义:算出所有值出现的概率与其乘积之和. 3.用前一步的期望,加上两者的期望距离,递推出来. 题意: 一个树,dfs遍历子树的顺序是 ...
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)
题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...
- #map+LCA# Codeforces Round #362 (Div. 2)-C. Lorenzo Von Matterhorn
2018-03-16 http://codeforces.com/problemset/problem/697/C C. Lorenzo Von Matterhorn time limit per t ...
- Codeforces Round #362 (Div. 2) A.B.C
A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...
- A. Puzzles CodeForces Round #196 (Div.2)
题目的大意是,给你 m 个数字,让你从中选 n 个,使得选出的数字的极差最小. 好吧,超级大水题.因为要极差最小,所以当然想到要排个序咯,然后去连续的 n 个数字,因为数据不大,所以排完序之后直接暴力 ...
- Codeforces Round #362 (Div. 2)->B. Barnicle
B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #362 (Div. 2)->A. Pineapple Incident
A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #362 (Div. 2) B 模拟
B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
随机推荐
- CentOS6忘记root密码如何重置
CentOS6忘记root密码,如何重置密码 ① 重启服务器,按"e"键进入修改系统开机项界面 ② 选择内核项,按"e"进入其中进行配置 在文件内 ...
- LeetCode数组中重复的数字
LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...
- 学习java 7.3
学习内容:定义类不需要加static 成员方法在多个对象时是可以共用的,而成员变量不可以共用,多个对象指向一个内存时,改变变量的值,对象所在的类中的变量都会改变 成员变量前加private,成员方法前 ...
- day06 视图层
day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...
- C语言大小端判定
要判定大小端?需要弄清以下几个问题: 1.当一个变量占多个字节时,变量的指针指向的是低地址 2.什么是大小端? 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中. 小 ...
- Hive(三)【DDL 数据定义】
目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...
- k8s StatefulSet控制器-独立存储
k8s-StatefulSet控制器-独立存储 1. StatefulSet控制器-独立存储 独享存储:StatefulSet的存储卷使用VolumeClaimTemplate创建,称为卷申请模板,当 ...
- redis入门到精通系列(四):Jedis--使用java操作redis详解
(一)前言 如果不把数据库和后端语言联系起来,就起不到数据库应该要起到的作用.Java语言通过JDBC操作mysql,用Jedis操作redis.当然了,java操作redis的方式不止jedis一种 ...
- redis入门到精通系列(二):redis操作的两个实践案例
在前面一篇博客中我们已经学完了redis的五种数据类型操作,回顾一下,五种操作类型分别为:字符串类型(string).列表类型(list).散列类型(hash).集合类型(set).有序集合类型(so ...
- 关于form表单提交ajaxForm和ajaxSubmit的用法与区别
前几天在学习form表单提交时看到这两种方法,这两种方法都是实现form的ajax提交的方法,看了很多资料还是不太明白其用法和区别,最后直接自己写demo,很快就理解,所以说实操是学习的最快捷直接的途 ...