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 ...
随机推荐
- Docker学习笔记之创建Ubuntu基础镜像
在创建基础镜像之前需要安装Bootstrap工具debootstrap,所以执行命令: sudo apt install debootstrap 软件安装完成后就可以使用debootstrap工具下载 ...
- Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because of access permission
Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because ...
- 【Oracle】regexp_substr()函数详解
环境:Oracle10.2.0.5 在SQL中尝试使用正则 可以试下regexp_substr()来进行分割 首先创建一个实验视图: SQL> create or replace view te ...
- ctfhub技能树—文件上传—00截断
什么是00截断 相关教程:http://www.admintony.com/%E5%85%B3%E4%BA%8E%E4%B8%8A%E4%BC%A0%E4%B8%AD%E7%9A%8400%E6%88 ...
- Redis 实战 —— 02. Redis 简单实践 - 文章投票
需求 功能: P15 发布文章 获取文章 文章分组 投支持票 数值及限制条件 P15 如果一篇文章获得了至少 200 张支持票,那么这篇文章就是一篇有趣的文章 如果这个网站每天有 50 篇有趣的文章, ...
- IDEA安装codota插件和使用,开发人员的知心伙伴
打开IDEA 点击左上角的File之后,如下图 成功后如图所示
- 【1w字+干货】第一篇,基础:让你的 Redis 不再只是安装吃灰到卸载(Linux环境)
Redis 基础以及进阶的两篇已经全部更新好了,为了字数限制以及阅读方便,分成两篇发布. 本篇主要内容为:NoSQL 引入 Redis ,以及在 Linux7 环境下的安装,配置,以及总结了非常详细的 ...
- Python爬虫学习笔记(一)
概念: 使用代码模拟用户,批量发送网络请求,批量获取数据. 分类: 通用爬虫: 通用爬虫是搜索引擎(Baidu.Google.Yahoo等)"抓取系统"的重要组成部分. 主要目的是 ...
- 忒修斯的Mac
我有一台Mac笔记本,用了快6年了,当初买它的时候还借了几千块. 三年前,它的屏幕坏了,修理的方式就是直接换屏,而换屏其实就是上半部分连壳带屏幕整个换掉,简单的说:另一台电脑的上半身嫁接过来. 今年, ...
- vue3.0 composition API
一.Setup函数 1.创建时间:组件创建之前被调用,优先与created被调用,this指向的实例为window,created所指向的实例为proxy 2.this指向:不会指向组件实例 3.参数 ...