【CF839C】journey(树上dfs求期望)

题目大意

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?

题目链接

传送门

分析

这些城市构成了一棵树,每一条路径是从根节点1倒最末端的叶节点的距离*选这条路的概率。注意,一下错误示范是按照条路的概率相等取算的,所以出错了。

错误示范

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
const int N = 1e5 + 10;
int rud[N], head[N], dep[N];
int tot, cnt, n;
long long sum;
inline int read(){
   
    int x = 0, op = 1;
    char ch = getchar();
    while (!isdigit(ch)){
   
        if (ch == '-') op = -1;
        ch = getchar();
    }
    while (isdigit(ch)){
   
        x = (x << 1) + (x << 3) + (ch ^ 48)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值