D. Puzzles(Codeforces Round #362 (Div. 2))
2016-07-19 12:17
477 查看
D.Puzzles
BarneylivesincountryUSC(UnitedStatesofCharzeh).USChasncitiesnumberedfrom1throughnandn - 1roadsbetweenthem.CitiesandroadsofUSCformarootedtree(Barney'snotsurewhyitisrooted).Rootofthetreeisthecitynumber1.Thusifonewillstarthisjourneyfromcity1,hecanvisitanycityhewantsbyfollowingroads.
SomegirlhasstolenBarney'sheart,andBarneywantstofindher.Hestartslookingforintherootofthetreeand(sinceheisBarneyStinsonnotarandomguy),heusesarandomDFStosearchinthecities.Apseudocodeofthisalgorithmisasfollows:
Astoldbefore,Barneywillstarthisjourneyintherootofthetree(equivalenttocalldfs(1)).
NowBarneyneedstopackabackpackandsohewantstoknowmoreabouthisupcomingjourney:foreverycityi,Barneywantstoknowtheexpectedvalueofstarting_time[i].He'safriendofJonSnowandknowsnothing,that'swhyheaskedforyourhelp.
Input
Thefirstlineofinputcontainsasingleintegern(1 ≤ n ≤ 105)—thenumberofcitiesinUSC.
Thesecondlinecontainsn - 1integersp2, p3, ..., pn(1 ≤ pi < i),wherepiisthenumberoftheparentcityofcitynumberiinthetree,meaningthereisaroadbetweencitiesnumberedpiandiinUSC.
Output
Inthefirstandonlylineofoutputprintnnumbers,wherei-thnumberistheexpectedvalueofstarting_time[i].
Youranswerforeachcitywillbeconsideredcorrectifitsabsoluteorrelativeerrordoesnotexceed10 - 6.
Examples
Input
Output
Input
Output
BarneylivesincountryUSC(UnitedStatesofCharzeh).USChasncitiesnumberedfrom1throughnandn - 1roadsbetweenthem.CitiesandroadsofUSCformarootedtree(Barney'snotsurewhyitisrooted).Rootofthetreeisthecitynumber1.Thusifonewillstarthisjourneyfromcity1,hecanvisitanycityhewantsbyfollowingroads.
SomegirlhasstolenBarney'sheart,andBarneywantstofindher.Hestartslookingforintherootofthetreeand(sinceheisBarneyStinsonnotarandomguy),heusesarandomDFStosearchinthecities.Apseudocodeofthisalgorithmisasfollows:
letstarting_timebeanarrayoflengthn current_time=0 dfs(v): current_time=current_time+1 starting_time[v]=current_time shufflechildren[v]randomly(eachpermutationwithequalpossibility) //children[v]isvectorofchildrencitiesofcityv foruinchildren[v]: dfs(u)
Astoldbefore,Barneywillstarthisjourneyintherootofthetree(equivalenttocalldfs(1)).
NowBarneyneedstopackabackpackandsohewantstoknowmoreabouthisupcomingjourney:foreverycityi,Barneywantstoknowtheexpectedvalueofstarting_time[i].He'safriendofJonSnowandknowsnothing,that'swhyheaskedforyourhelp.
Input
Thefirstlineofinputcontainsasingleintegern(1 ≤ n ≤ 105)—thenumberofcitiesinUSC.
Thesecondlinecontainsn - 1integersp2, p3, ..., pn(1 ≤ pi < i),wherepiisthenumberoftheparentcityofcitynumberiinthetree,meaningthereisaroadbetweencitiesnumberedpiandiinUSC.
Output
Inthefirstandonlylineofoutputprintnnumbers,wherei-thnumberistheexpectedvalueofstarting_time[i].
Youranswerforeachcitywillbeconsideredcorrectifitsabsoluteorrelativeerrordoesnotexceed10 - 6.
Examples
Input
7 121144
Output
1.04.05.03.54.55.05.0
Input
12 112244331108
Output
1.05.05.56.57.58.08.07.07.56.57.58.0 题意:给你一棵树,然后用dfs随即给每个点标号,求每个点标号的期望; 思路:dfs+概率dp; 我们可以知道根节点的期望为1; 然后,他的子节点都是等概率的。 dp数组是各个节点的期望。 先给样例一的图:我们写第二层的排列 1,1245 2,1254 3,1425 4,1452 5,1524 6,1542 所以节点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)
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<queue> #include<stack> #include<set> #include<stdlib.h> #include<vector> usingnamespacestd; vector<int>vec[100006]; longlongcnt[100006]; longlongdfs1(intn); doubledp[100006]; voiddfs(intn); intmain(void) { inti,j,k; while(scanf("%d",&k)!=EOF) { intn;memset(dp,0,sizeof(dp)); for(i=0;i<100006;i++) { cnt[i]=0; vec[i].clear(); } for(i=2;i<=k;i++) { scanf("%d",&n); vec .push_back(i); } longlongt=dfs1(1); dp[1]=1.0; dfs(1);printf("%.1f",dp[1]); for(i=2;i<=k;i++) { printf("%.1f",dp[i]); } printf("\n"); } return0; } longlongdfs1(intn) { longlongsum; inti,j,k; for(i=0;i<vec .size();i++) { cnt +=dfs1(vec [i]); } cnt +=1; returncnt ; } voiddfs(intn) { inti,j; for(i=0;i<vec .size();i++) { intx; x=vec [i]; dp[x]=dp +1.0; dp[x]+=1.0*(cnt -cnt[x]-1)/2.0; dfs(x); } }
相关文章推荐
- 利用python脚本实现域名解析和归属地信息查询
- hdoj 1280 前m大的数 【sort】
- 一句reload,解决了千古难题
- Linux for 循环总结
- CAKeyframeAnimation关键帧动画的相关属性和方法
- 2-Application Layer
- hdu 4277 USACO ORZ
- 我的助理辞职了
- 了解Java基础
- Tablayout 的使用
- 直方图相似度算法
- android压缩图片 遇到的问题
- 好的开源工程
- hdoj 2187 老人是真饿了 【结构体+贪心】
- STL概述
- C++中临时对象的学习笔记
- 输出格式 c && c++
- 源码-PL/SQL从入门到精通-第七章-操作数据表
- Servlet学习(四)使用request读取所有参数
- LFS from scratch(从0入门)