【Codeforces Round 362 (Div 2)D】【树的遍历 概率均分思想】Puzzles 兄弟节点的等概率遍历下 树的遍历每点期望时间戳
2016-07-26 10:33
441 查看
D. Puzzles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Barney lives in country USC (United States of Charzeh). USC has n cities numbered
from 1through 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:
As told before, Barney will start his journey in the root of the tree (equivalent to calldfs(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.
Input
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 numberedpi and i in
USC.
Output
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.
Examples
input
output
input
output
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
vector<int>a
;
double ans
;
int sz
;
void dfs1(int x)
{
sz[x] = 0;
for (int i = a[x].size() - 1; ~i; --i)
{
int y = a[x][i];
dfs1(y);
sz[x] += sz[y];
}
++sz[x];
}
void dfs2(int x)
{
for (int i = a[x].size() - 1; ~i; --i)
{
int y = a[x][i];
double sum = (sz[x] - 1 - sz[y]) / 2.0;
ans[y] = ans[x] + sum + 1;
dfs2(y);
}
}
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)a[i].clear();
for (int i = 2; i <= n; ++i)
{
int x; scanf("%d", &x);
a[x].push_back(i);
}
ans[1] = 1;
dfs1(1);
dfs2(1);
for (int i = 1; i <= n; ++i)printf("%.12f\n", ans[i]);
}
return 0;
}
/*
【题意】
给你一棵树,然后每个节点遍历自己子节点的拓扑序是任意的(其访问子节点的全排列等概率化)。
dfs带有时间戳。问你,每个节点的访问时间戳的期望是多少。
【类型】
树的遍历 概率均分思想
【分析】
这道题乍一看有些不知道如何入手。
难道我们要考虑所有全排列?
不过实际发现,所谓全排列规律均等,
其实也就意味着——
任意两个兄弟节点的访问概率都是均等的。
即:
每个节点有50%的概率在其兄弟节点的子树之后访问。
于是,我们求出每棵子树的大小,
然后每个节点的访问时间戳期望=父节点访问时间戳期望+1+∑兄弟节点子树大小/2
这道题就可以解决啦。
【时间复杂度&&优化】
O(n)
*/
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Barney lives in country USC (United States of Charzeh). USC has n cities numbered
from 1through 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 calldfs(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.
Input
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 numberedpi and i in
USC.
Output
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.
Examples
input
7 1 2 1 1 4 4
output
1.0 4.0 5.0 3.5 4.5 5.0 5.0
input
12 1 1 2 2 4 4 3 3 1 10 8
output
1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
vector<int>a
;
double ans
;
int sz
;
void dfs1(int x)
{
sz[x] = 0;
for (int i = a[x].size() - 1; ~i; --i)
{
int y = a[x][i];
dfs1(y);
sz[x] += sz[y];
}
++sz[x];
}
void dfs2(int x)
{
for (int i = a[x].size() - 1; ~i; --i)
{
int y = a[x][i];
double sum = (sz[x] - 1 - sz[y]) / 2.0;
ans[y] = ans[x] + sum + 1;
dfs2(y);
}
}
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)a[i].clear();
for (int i = 2; i <= n; ++i)
{
int x; scanf("%d", &x);
a[x].push_back(i);
}
ans[1] = 1;
dfs1(1);
dfs2(1);
for (int i = 1; i <= n; ++i)printf("%.12f\n", ans[i]);
}
return 0;
}
/*
【题意】
给你一棵树,然后每个节点遍历自己子节点的拓扑序是任意的(其访问子节点的全排列等概率化)。
dfs带有时间戳。问你,每个节点的访问时间戳的期望是多少。
【类型】
树的遍历 概率均分思想
【分析】
这道题乍一看有些不知道如何入手。
难道我们要考虑所有全排列?
不过实际发现,所谓全排列规律均等,
其实也就意味着——
任意两个兄弟节点的访问概率都是均等的。
即:
每个节点有50%的概率在其兄弟节点的子树之后访问。
于是,我们求出每棵子树的大小,
然后每个节点的访问时间戳期望=父节点访问时间戳期望+1+∑兄弟节点子树大小/2
这道题就可以解决啦。
【时间复杂度&&优化】
O(n)
*/
相关文章推荐
- 【FOJ2207 11月月赛C】【DFS栈性质应用 离线处理】以撒的结合 从x到y路径上的第k个点 询问众多
- 【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂
- 【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
- 【Educational Codeforces Round 1C】【计算几何-极角排序 atan2 long double】Nearest vectors 平面图上原点引出角度最小的两个
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本
- 【HDU5544 2015CCPC 南阳国赛E】【树上dfs找本质不同环 高斯消元 时间戳优化】Ba Gua Zhen 连通图上最大异或环
- 【Codeforces Round 299 (Div 2)D】【KMP 本质是最前与最后匹配】Tavas and Malekas 长度为n的匹配串被模板串多位点覆盖的匹配串个数
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小
- 【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列
- 【Codeforces Round 333 (Div 2)C】【最短路】The Two Routes 完全图两种双向边的最小最大距离
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- 【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数
- 【Codeforces Testing Round 12B】【贪心】Restaurant 选取数量最多的不覆盖区间数
- 【Codeforces Testing Round 12C】【DP 树状数组优化】Subsequences n个不同数,长度为m的LIS数
- 【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大
- 【Educational Codeforces Round 2C】【贪心】最少修改数下得到字典序尽可能小回文串
- 【Educational Codeforces Round 2D】【计算几何 圆面积交 模板】Area of Two Circles' Intersection
- 【[Offer收割]编程练习赛23 A】【爆搜】H国的身份证号码I