CodeForces 696B Puzzles
思维,简单树$dp$。
首先计算出每一个子树包含多少个节点,记为$f[i]$。然后就可以从$root$开始推出所有节点的期望了。
现在已知$fa$节点的答案为$ans[fa]$,假设要计算$fa$的一个儿子$v$的期望,那么$ans[v]=ans[fa]+1.0+(f[fa]-f[v]-1)/2.0$。
我画了一下样例,然后猜测了一下发现是对的......
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() {freopen("D:\\in.txt","r",stdin);freopen("D:\\out.txt","w",stdout); } template <class T> inline void read(T &x) {char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } }const int maxn=100010; double ans[maxn]; vector<int>g[maxn]; int n,f[maxn];void dfs(int x) {f[x]=0;for(int i=0;i<g[x].size();i++){dfs(g[x][i]);f[x]=f[x]+f[g[x][i]];}f[x]++; }void get(int x) {for(int i=0;i<g[x].size();i++){ans[g[x][i]]=ans[x]+1.0+1.0*(f[x]-f[g[x][i]]-1)/2.0;get(g[x][i]);} }int main() {scanf("%d",&n);for(int i=2;i<=n;i++){int fa; scanf("%d",&fa);g[fa].push_back(i);}dfs(1); ans[1]=1.0; get(1); for(int i=1;i<=n;i++) printf("%.6lf ",ans[i]);printf("\n");return 0; }
转载于:https://www.cnblogs.com/zufezzt/p/5798395.html
CodeForces 696B Puzzles相关推荐
- 签到 2016.6.9
1.CodeForces 337A Puzzles 题意: ①在有m个整数的集合中 ②找到有n个整数的子集 ③再比较这些子集中元素的最大值与最小值的差值 ④输出最小的差值 解题思路: ①将集合中的元素 ...
- Codeforces Puzzles
题目链接:http://codeforces.com/problemset/problem/337/A 代码: import java.util.Arrays; import java.util.Sc ...
- Codeforces Round #121 (Div. 1) A. Dynasty Puzzles DP
链接: http://codeforces.com/contest/191/problem/A 题意: 给出n个字符串,两个字符串如果前一个的尾与后一个的首相同,那么可以相连,最后得到的字符串要满足首 ...
- Codeforces Round #196 (Div. 2) A. Puzzles 水题
A. Puzzles Time Limit: 2 Sec Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
- 【CodeForces - 697D】Puzzles(树形dp,期望dp)
题目大意: 给定一棵树,从1开始,按DFS的方式访问这棵树 每次从父亲节点随机访问儿子,问每个节点被访问到的时间的期望 输入:第一行一个数n,代表n个节点.第二行n-1个数p2,p3,p4,p5.. ...
- Dynasty Puzzles CodeForces - 191A+DP二维
题意:一共给你N个字符串,如果一个字符串的尾部字母和一个字符串的首部字母相同,/那么两个字符串就可以拼接起来,问最长的首尾字母相同的字符串的长度为多少. //思路:DP便利一遍就够了 ,dp[a][b ...
- 【37.50%】【codeforces 745B】Hongcow Solves A Puzzle
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces泛做
codeforces div1的题目,一般是做B.C.D 有的A题觉得有价值也会做,死活不会的就跳了-- 364 B: Connecting Universities Problem: 一颗无根树上给 ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
最新文章
- 重学hadoop技术
- C#的static,interface,virtual,abstract,override的区别用法
- 【oracle】日期类型 to_char
- 380万赌石(翡翠原石)切出2亿,现在值多少钱了?
- 解决: Incorrect username or password, or no permission ( Docker 方式运行 Nexus3 登陆密码不为 admin123 、重置登陆密码)
- python whl_python whl是什么文件
- JavaScript——闭包函数及拓展题目
- 环保公益PSD分层海报,小景合成美好世界
- sql无法写入mysql_windows7 sqlserver2012 无法写入受保护的内存 解决办法
- js的异常捕获try和catch语句
- UNIX环境高级编程——创建与打开IPC通道
- python 技能清单_Python清单
- java-多线程安全问题
- C++ 修改 Windows Service【转】
- XJOI 3877 红蓝字符串
- Unity 游戏中近战攻击判定检测——射线检测
- 微信小程序头像怎么改变形状_微信小程序 open-data更改样式 open-data 显示头像 圆形...
- 【盛天体育出品】天津仁爱学院“彩虹”运动场来了
- python爬虫 批量下载高清大图
- iphone文件连接smb服务器,实用!三种iPhone与Windows电脑互传文件操作技巧,建议收藏...