• 题意:给你一颗树(边是无向的),从根节点向下走,统计走到每个子节点的概率,求所有叶子节点的深度乘上概率的和.

  • 题解:每层子节点的概率等于上一层节点的概率乘\(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 (简单搜索)的更多相关文章

  1. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  2. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  3. 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 ...

  4. 【Codeforces Round #428 (Div. 2) C】Journey

    [Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...

  5. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  6. Codeforces Round #428 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/839 A. Arya and Bran 题意:每天给你一点糖果,如果大于8个,就只能给8个,剩下的可以存起来,小于8个就可以全部 ...

  7. Codeforces Round #428 (Div. 2)

    终于上蓝名了,hahahahaha,虽然这场的 B 题因为脑抽了,少考虑一种情况终判错了,还是很可惜的.. B题本来过来1500个人,终判之后只剩下了200多个,真的有毒!!!! A - Arya a ...

  8. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  9. 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 ...

随机推荐

  1. 断言封装之key检查及kv实战示例

    ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ 测试: 断言处理: demo_04.pyimport jsonjson_obj = {"access_token":&q ...

  2. /usr/bin/ld: cannot find -lc

    yum install glibc-static [root@test chkrootkit-0.50]# make sensecc -static -o strings-static strings ...

  3. 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)

    Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...

  4. 修改机器的hostname

    vi /etc/sysconfig/network hostname=你想设置的主机名 不重启器的情况下使显示名称变成 hostname  主机名

  5. kubernets之向外部应用暴露应用

    一  通过NodePort来暴露服务 前面已经介绍的服务的一些作用,例如将集群内部的应用暴露给集群内部的pod使用,将外部的应用通过服务暴露给内部应用使用,但是服务最大的作用不仅仅是这些 而是将集群内 ...

  6. 汇编学习笔记——DOS及DEBUG介绍

    转自:https://www.shiyanlou.com/courses/running/332 一.课程简介 声明:该课程基于<汇编语言(第2版)>郑晓薇 编著,机械工业出版社.本节实验 ...

  7. Linux下nginx的安装以及环境配置

    参考链接 https://blog.csdn.net/qq_42815754/article/details/82980326 环境: centos7 .nginx-1.9.14 1.下载 并解压  ...

  8. (数据科学学习手札104)Python+Dash快速web应用开发——回调交互篇(上)

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  9. 使用Logback日志

    使用Logback日志 spring boot内部使用Logback作为日志实现的框架. Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手. logba ...

  10. 牛逼!MySQL 8.0 中的索引可以隐藏了…

    MySQL 8.0 虽然发布很久了,但可能大家都停留在 5.7.x,甚至更老,其实 MySQL 8.0 新增了许多重磅新特性,比如栈长今天要介绍的 "隐藏索引" 或者 " ...