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

  • 题解:每层子节点的概率等于上一层节点的概率乘\(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. nginx启动失败(bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket...permissions)

    nginx启动失败 nginx启动失败(bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a ...

  2. 设计一款兼容ST207和GD207的开发板

    在MCU的学习中,大部分人都是学习别人的开发板,例如正点原子.野火等,优点是有可靠的教程和代码,缺点是容易让人有种自己全部都学会的了错觉,听了课程编写了代码,运行正常. 这个时候,可以尝试自已做一块属 ...

  3. 【九阳神功】Nessus 8_VM不限IP及AWVS破解版合体部署

    Nessus 8下载地址: https://moehu-my.sharepoint.com/personal/ximcx_moebi_org/_layouts/15/download.aspx?Sou ...

  4. oralce move和shrink释放高水位

    转自:https://blog.51cto.com/fengfeng688/1955137 move和shrink的共同点: 收缩段,消除部分行迁移,消除空间碎片,使数据更紧密 shrink用法: 语 ...

  5. 用 UniRx 实现 Timeline 式的异步操作

      没接触 UniRx 之前,我在 Unity 中通常用 Coroutine 或 Callback 来实现异步操作.根据我的任务,一般都是去实现游戏组件的演出,比如:敌方角色图形显示后,我方角色 UI ...

  6. SAP中数据库表长度的界定

    SAP中,如何查看表和关键字的长度?通过SE11菜单栏Extras->table width 可以看到.然而SAP在系统也会将表分类,特别是在可扩展的表维护视图中,分为如下几类      ult ...

  7. Spring入门及IoC的概念

    Spring入门 Spring是一个轻量级的Java开发框架,最早由Robd Johnson创建,目的为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,它是一个分层的JavaSE/EE轻量级开源 ...

  8. 3A的限流芯片PW1503

    PW1503是超低RDS(ON)开关,具有可编程的电流限制,以保护电源于过电流和短路情况.它具有超温保护以及反向闭锁功能. PW1503采用薄型(1毫米)5针薄型SOT封装,提供可调版本. 特征    ...

  9. 前端知识(一)04 Vue.js入门-谷粒学院

    目录 一.介绍 1.Vue.js 是什么 2.初识Vue.js 二.基本语法 1.基本数据渲染和指令 2.双向数据绑定 3.事件 4.修饰符 5.条件渲染 6.列表渲染 7.实例生命周期 一.介绍 1 ...

  10. saltstack 服务器批量管理

    学习saltstack 服务器批量管理 1.saltstack 简介 SaltStack是一个开源的.新的基础平台管理工具,使用Python语言开发,同时提供Rest API方便二次开发以及和其他运维 ...