题目大意
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)