Puzzles
Puzzles
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
分析:不是太懂,貌似不在当前节点的子树中,却在他父亲的子树中的节点,先比他拜访的概率是0.5;
官方题解:http://codeforces.com/blog/entry/46031
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,child[maxn],fa[maxn];
double vis[maxn];
vi p[maxn];
int dfs(int now)
{
int cnt=;
for(int x:p[now])cnt+=dfs(x);
return child[now]=cnt+;
}
void work(int now)
{
if(now!=)
vis[now]=vis[fa[now]]++(child[fa[now]]-child[now]-)/2.0;
for(int x:p[now])work(x);
}
int main()
{
int i,j,k,t;
scanf("%d",&n);
rep(i,,n)scanf("%d",&k),p[k].pb(i),fa[i]=k;
vis[]=1.0;
dfs();
work();
rep(i,,n)printf("%g ",vis[i]);
//system ("pause");
return ;
}
Puzzles的更多相关文章
- 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>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. ...
- [CF697D]Puzzles 树形dp/期望dp
Problem Puzzles 题目大意 给一棵树,dfs时随机等概率选择走子树,求期望时间戳. Solution 一个非常简单的树形dp?期望dp.推导出来转移式就非常简单了. 在经过分析以后,我们 ...
- [cf contest697] D - Puzzles
[cf contest697] D - Puzzles time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- RecyclerView onTouch & onClick 触摸事件与点击事件
遇到一种情况需要处理,RecyclerView所在Activity需要处理滑动事件用于返回(onBackPressed). 手势检测: @Override public boolean onFling ...
- iOS自定制tabbar与系统的tabbar冲突,造成第一次点击各个item图片更换选中,第二次选中部分item图片不改变
可以选择是使用自定制的还是系统的,如果使用自定制的,就使用以下方法即可隐藏系统的uitabbarButton,从而使item恢复正确 //隐藏UITabBarButton -(void)viewWil ...
- 《剑指Offer》算法题——二维数组查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. class Solutio ...
- 手把手教你ranorex_android自动化测试第一个示例
要说android的自动化,那真是折腾死我了,从早期的monkeyrunner,到后来的robotium,再到最新的uiautomator,各有各的问题,总之性价比都不够高,不太适合我的使用场景.于是 ...
- 升级3.2.3后 could not find driver
求解,之前一切正常,升级3.2.3后提示找不到数据库驱动我用的是mysql,也没有设置DB_DSN错误位置FILE: C:\Users\Administrator\workspace\test\Thi ...
- Storm官方帮助手册翻译(下)
使用其他语言编写Bolt Bolt可以使用任意语言编写.用另外一种语言编写Bolt来作为子进程运行.Storm会在标准输入输出的基础上使用Json来与子进程通信.通信协议之需要一个100行的适配器库, ...
- 设置标题小图标ico
在head里添加 <link rel="shortcut icon" href="<%=request.getContextPath()%>/FlatU ...
- win8上cmder文字重叠问题
1.用过ubuntu上的bash,zsh后发现win自带的cmd弱爆了,在网上搜索后找到了代替品cmder,下载安装后好发现中文错位的问题, 状况如下: 修复方法:把设置里面的Monospace选项勾 ...
- cin详解(cin.get()、cin.getline()、cin.clear()、cin.sync())
在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" iostr ...
- Count on the path
Count on the path Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...