• Round #428 C. Journey(Div.2)期望值


    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

    Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

    Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.

    Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road.

    It is guaranteed that one can reach any city from any other by the roads.

    Output

    Print a number — the expected length of their journey. The journey starts in the city 1.

    Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

    Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

    Examples
    Input
    4
    1 2
    1 3
    2 4
    Output
    1.500000000000000
    Input
    5
    1 2
    1 3
    3 4
    2 5
    Output
    2.000000000000000
    Note

    In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.

    In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.

    在概率论和统计学中,一个离散性随机变量的期望值(或数学期望、或均值,亦简称期望)是试验中每次可能结果的概率乘以其结果的总和。
    期望可以理解为加权平均值,权数是函数的密度。对于离散函数,E(x)=∑f(xi)xi
    例如,掷一枚六面骰子的期望值是3.5,计算如下:
    1*1/6+2*1/6+3*1/6+4*1/6+5*1/6+6*1/6=3.5

    题意:总共有n个城市,有n-1条城市间连通的路,问你当无路可走的时候,此时所有结束结果的期望值总和

    简析:比如例题一,走到没有路时,只有3,4城市,当我们站在一号城市时,有2,3城市可以选择,所以概率为1/2,

    假设我们先走3,3后没有子节点,结束,此时期望值C1为:路径长度×概率=1*1/2;

    接下来走2号城市,2后有4号城市,4号后就没有啦,2到4概率为1,在4号结束时,期望值C2:路径长度×概率=2×(1/2×1)

    总的期望值:sum=C1+C2;

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<vector>
     6 #include<algorithm>
     7 
     8 using namespace std;
     9 const int maxn = 100010;
    10 vector<int> G[maxn];
    11 bool used[maxn];
    12 int n;
    13 double res;
    14 void dfs(int x,double y,int dis){
    15     if(G[x].size() == 1 && x != 1){
    16         res += dis*y;
    17         return;
    18     }
    19     int deep = (int)G[x].size();
    20     for(int i=0;i<deep;i++){
    21         int k = G[x][i];
    22         if(!used[k]){
    23             used[k] = true;
    24             if(x == 1)  dfs(k,y*1/deep,dis+1);
    25             else    dfs(k,y*1/(deep-1),dis+1);
    26         }
    27     }
    28 }
    29 int main(void){
    30     scanf("%d",&n);
    31     int x,y;
    32     for(int i=1;i<n;i++){
    33         scanf("%d %d",&x,&y);
    34         G[x].push_back(y);
    35         G[y].push_back(x);
    36     }
    37     memset(used,false,sizeof(used));
    38     used[1] = true;res = 0;
    39     dfs(1,1,0);
    40 
    41     printf("%.15lf",res);
    42     return 0;
    43 }
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstdio>
     4 using namespace std;
     5 const int N=100010;
     6 int n,i,x,y,g[N],v[N<<1],nxt[N<<1],ed;
     7  double f[N];
     8 
     9 void add(int x,int y){
    10         v[++ed]=y;
    11         nxt[ed]=g[x];
    12         g[x]=ed;
    13 }
    14 
    15 void dfs(int x,int y){
    16         int deg=0;
    17         for(int i=g[x];i;i=nxt[i])
    18         if(v[i]!=y){
    19         deg++;
    20         dfs(v[i],x);
    21         f[x]+=f[v[i]];
    22         }
    23         if(deg)
    24         f[x]=1.0*f[x]/deg+1;
    25 }
    26 int main(){
    27         scanf("%d",&n);
    28         for(i=1;i<n;i++)
    29         scanf("%d%d",&x,&y),add(x,y),add(y,x);
    30         dfs(1,0);
    31         printf("%.15f
    ",f[1]);
    32 
    33 }
  • 相关阅读:
    搭建高可用K8S集群
    K8S部署apollo配置中心
    微服务二:微服务的拆分、设计模式、内部结构
    微服务一:微服务概念入门及发展历程
    k8s可视化管理dashboard
    Windows节点加入K8S集群(K8S搭建Linux和Window混合集群)
    K8S搭建单点集群+问题处理
    K8S核心概念
    毕业论文word排版设置
    Anaconda3+PyTorch安装教程
  • 原文地址:https://www.cnblogs.com/z-712/p/7354090.html
Copyright © 2020-2023  润新知