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

  • 题解:每层子节点的概率等于上一层节点的概率乘\(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. Error: Could not request certificate: No route to host - connect(2)

    [root@puppetclient ~]# puppet agent --server 192.168.127.137 --testInfo: Creating a new SSL key for ...

  2. Mybatis 一级缓存和二级缓存的使用

    目录 Mybatis缓存 一级缓存 二级缓存 缓存原理 Mybatis缓存 官方文档:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#cache My ...

  3. BDC应用

    第一步:SHDB或者是SM35进入BDC录制事务.开始录制. 第二部:保存录制的记录. 第三步:在你自己的程序中定义一个内表如:ITAB TYPE TABLE OF BDCDATA. 再定义一个工作空 ...

  4. printf函数输出格式总结

    printf函数格式 函数描述: printf("[格式化字符串]", [参数链表]); 函数声明: int printf(const char *format, ...) ; 输 ...

  5. STGAN: A Unified Selective Transfer Network for Arbitrary Image Attribute Editing 阅读笔记和pytorch代码解读

    一.论文采用的新方法 1.AttGan中skip connect的局限性 由于encoder中对特征的下采样实际上可能损失部分特征,我们在decoder中进行上采样和转置卷积也无法恢复所有特征,因此A ...

  6. 1V转3.3V稳压供电的芯片电路图

    1V转3.3V供电是简单的,仅需要一个芯片和三个外围元件即可组成这样的一个1V转3.3V的电路图和升压电路了.可以持续稳定地供电3.3V给模块或者MCU灯电路.让后端工作稳定,同时也能控制电路的功耗. ...

  7. PAT练习num4-D进制的A+B

    输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...

  8. SQL 语法速成手册

    本文针对关系型数据库的一般语法.限于篇幅,本文侧重说明用法,不会展开讲解特性.原理. 一.基本概念 数据库术语 数据库(database) - 保存有组织的数据的容器(通常是一个文件或一组文件). 数 ...

  9. Django Signals

    信号 Django中提供了"信号调度",用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. Django内置的信号 Model si ...

  10. SQL性能优化汇总

    SQL效率低下也是导致性能差的一个非常重要的原因,可以通过查看执行计划看SQL慢在哪里,一般情况,SQL效率低下原因主要有:   类别 子类 表达式或描述 原因 索引 未建索引 无 产生全表扫描 未利 ...