本文主要是介绍【Codeforces Round 362 (Div 2)D】【树的遍历 概率均分思想】Puzzles 兄弟节点的等概率遍历下 树的遍历每点期望时间戳,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 + 1starting_time[v] = current_timeshuffle children[v] randomly (each permutation with equal possibility)// children[v] is vector of children cities of city vfor 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.
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.
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
#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[N];
double ans[N];
int sz[N];
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)*/
这篇关于【Codeforces Round 362 (Div 2)D】【树的遍历 概率均分思想】Puzzles 兄弟节点的等概率遍历下 树的遍历每点期望时间戳的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!