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 ...
随机推荐
- SpringBoot配置文件(2)
六.配置文件加载 SpringBoot 启动会扫描以下位置的application.properties或者application.yml文件作为SpringBoot的默认配置文件 file:./co ...
- 爬虫-urllib3模块的使用
urllib3是一个功能强大,对SAP健全的 HTTP客户端,许多Python生态系统已经使用了urllib3. 一.安装 sudo pips install urllib3 二.创建PoolMana ...
- 如何实现CentOS服务器的扩容??
Linux的硬盘识别: 一般使用"fdisk -l"命令可以列出系统中当前连接的硬盘 设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息. 1.关闭服务器加上新硬盘 2.启动 ...
- random模块常用函数
random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...
- oracle 释放表空间到OS(resize)
1.查看表空间里面的对象 SELECT OWNER AS OWNER, SEGMENT_NAME AS SEGMENT_NAME, SEGMENT_TYPE AS SEGMENT_TYPE, SUM ...
- 企业项目迁移go-zero全攻略(一)
作者:Mikael 最近发现 golang 社区里出了一个新兴的微服务框架.看了一下官方提供的工具真的很好用,只需要定义好 .api 文件模版代码都可以一键生成,只需要关心业务:同时 core 中的工 ...
- 干电池升压IC
1, 干电池升压IC 升压输出3V,3,3V,5V等3V-5V可调 2, 单节锂电池升压IC 升压输出4.2 ...
- MYSQL(将数据加载到表中)
1. 创建和选择数据库 mysql> CREATE DATABASE menagerie; mysql> USE menagerie Database changed 2. 创建表 mys ...
- Spring之 IOC&依赖注入
0x01.Spring 1什么是Spring Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的(解耦). 框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组 ...
- 离线安装docker-ce
1.用一台可以连外网的虚拟机把docker-ce安装包下载下来,vim /tmp/docker-download.sh #!/bin/bash set -e mkdir -p /apps/docker ...