Codeforces Round #428 (Div. 2) C. Journey (简单搜索)

题意:给你一颗树(边是无向的),从根节点向下走,统计走到每个子节点的概率,求所有叶子节点的深度乘上概率的和.
题解:每层子节点的概率等于上一层节点的概率乘\(1\)除以这层的子节点数,所以我们用\(dfs\)或者\(bfs\)都可以写,其实就是个搜索裸题,注意给的边是无向的就好了.
代码:
1.dfs:
int n;
int a,b;
vector<int> v[N];
double ans; void dfs(int u,int fa,double p,int len){
if((int)v[u].size()==1 && u!=1){
ans+=p*len;len;
return;
}
for(auto w:v[u]){
double p1;
if(w==fa) continue;
if(u==1) p1=p*(1.0/(double)(v[u].size()));
else p1=p*(1.0/(double)(v[u].size()-1));
dfs(w,u,p1,len+1);
}
} int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
scanf("%d",&n);
for(int i=1;i<n;++i){
scanf("%d %d",&a,&b);
v[a].pb(b);
v[b].pb(a);
} dfs(1,-1,1.0,0); printf("%.6f\n",ans); return 0;
}
2.bfs
struct misaka{
int node;
int fa;
double p;
double len;
}e; int n;
int a,b;
vector<int> v[N];
double ans; void bfs(){
queue<misaka> q;
e.node=1;e.p=1.0;e.len=0.0;e.fa=-1;
q.push(e); while(!q.empty()){
auto tmp=q.front();
q.pop(); int node=tmp.node;
double p=tmp.p;
double len=tmp.len;
int fa=tmp.fa; if((int)v[node].size()==1 && node!=1){
ans+=p*len;
} for(auto w:v[node]){
if(w==fa) continue;
e.node=w;
if(node==1)
e.p=p*((double)1.0/(double)v[node].size());
else e.p=p*(1.0/(double)(v[node].size()-1));
e.len=len+1.0;
e.fa=node;
q.push(e);
}
}
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
e.len=1.0;
e.p=1.0;
for(int i=1;i<n;++i){
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
} bfs(); printf("%.6f\n",ans); return 0;
}
Codeforces Round #428 (Div. 2) C. Journey (简单搜索)的更多相关文章
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)
血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...
- 【Codeforces Round #428 (Div. 2) C】Journey
[Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- Codeforces Round #428 (Div. 2) 题解
题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...
- Codeforces Round #428 (Div. 2)
终于上蓝名了,hahahahaha,虽然这场的 B 题因为脑抽了,少考虑一种情况终判错了,还是很可惜的.. B题本来过来1500个人,终判之后只剩下了200多个,真的有毒!!!! A - Arya a ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
随机推荐
- iTerm2 实现 ssh 自动登录,并使用 Zmodem 实现快速传输文件
原文链接:https://fuckcloudnative.io/posts/iterm2-auto-login/ 对于 YAML 工程师来说,我们经常需要 ssh 登录不同的服务器,每次登录时都要经历 ...
- Apache目录详解
Apache的主要目录和配置文件理解 参考链接:http://httpd.apache.org/docs/2.4/misc/security_tips.html 一.Apache主要配置文件注释(演示 ...
- 三种梯度下降算法的区别(BGD, SGD, MBGD)
前言 我们在训练网络的时候经常会设置 batch_size,这个 batch_size 究竟是做什么用的,一万张图的数据集,应该设置为多大呢,设置为 1.10.100 或者是 10000 究竟有什么区 ...
- ping 命令示例
将下面的代码粘贴到记事本中,然后保存为扩展名为BAT的文件,运行就可以将网段内ping不通的IP地址写入到文本文件IP.txt中. @echo offsetlocal ENABLEDELAYEDEXP ...
- 与图论的邂逅06:dfs找环
当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...
- 二十七:XSS跨站之代码及httponly绕过
httponly:如果给某个 cookie 设置了 httpOnly 属性,则无法通过 JS 脚本 读取到该 cookie 的信息,但还Application 中手动修改 cookie,所以只是在一定 ...
- Python安装教程之anaconda篇
[导读]我们知道,Python的功能非常强大.那么对于迫切想学习Python的新手同学来说,第一件事情可能需要了解python是什么?能用来做什么?语法结构是怎样的?这些我们几句话很难介绍清楚,后续会 ...
- 通过js给某个标签添加内容或者删除标签
添加内容 //先保存div中原来的html var tag = document.getElementById("tag").innerHTML; //构造新的内容 var cou ...
- 指针锁定 Pointer Lock API 用法
指针锁定 Pointer Lock API 通过它可以访问原始的鼠标运动(基于指针的相对位移 movementX / movementY),把鼠标事件的目标锁定到一个特定的元素,同时隐藏视图中的指针光 ...
- Django-发上云服务器遇到的问题
1.服务器启动后外网访问显示A server error occurred. Please contact the administrator. 解决方法:原文:https://www.cnblogs ...