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

  • 题解:每层子节点的概率等于上一层节点的概率乘\(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. Linux学习笔记 | 配置ssh

    目录: SSH的必要性 将默认镜像源修改为清华镜像源 Linux安装ssh软件 使用putty软件实现ssh连接 Windows下安装winscp SSH的必要性 一般服务器都位于远程而非本地,或者及 ...

  2. 【SpringBoot】Spring Boot 集成SwaggerAPI

    Spring Boot 集成SwaggerAPI 文章目录 Spring Boot 集成SwaggerAPI Swagger 添加依赖 配置类 config 控制类 controller 接口测试 页 ...

  3. 【Linux】cp命令的各种妙用

    CP 功能: 复制文件或目录 说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件 ...

  4. Centos6.9安装ACFS

    安装完oracle 11GR2的RAC后,使用asmca打开图形化界面后,发现Volumes和ASM Cluster File System两个选项卡不能用 原因是因为ACFS不支持CentOS 解决 ...

  5. JavaScript的数据类型和数据类型的检测

    数据类型 JavaScript的基础数据类型有,NaN    string   undefined    Null      Boolen    Symbol   Bigint   这些都是基础数据类 ...

  6. PE节表

  7. https://www.exploit-db.com/docs/english/45906-cors-attacks.pdf

    https://www.exploit-db.com/docs/english/45906-cors-attacks.pdf What is CORS (cross-origin resource s ...

  8. Python基础(列表中变量与内存关系)

    在Python中,copy的是内存地址,引用的是列表的引用地址,列表里存的是各个元素的地址 例如: name = [1,2,3,4,['xfxing','summer',6]] n2 = name.c ...

  9. Solon rpc 1.3.1 发布,推出Cloud接口与配置规范

    Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,3500多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:Rpc. ...

  10. IDEA、Pycharm学生免费使用(无教育邮箱)

    一.打开JetBrains学生产品网站 JetBrains Products for Learning:https://www.jetbrains.com/shop/eform/students 二. ...