题目大意
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);
ch = getchar();
}
return x * op;
}
struct edge{
int to, nxt;
}e[N << 1];
inline void add(int u, int v){
e[++cnt] = {v, head[u]};
head[u] = cnt;
}
void dfs(int u, int fa){
dep[u] = dep[fa] + 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
dfs(v, u);
}
}
int main() {
n = read();
for (int i = 1, x, y ; i < n; ++i) {
x = read(), y = read();
add(x, y), add(y, x);
++rud[x];
++rud[y];
}
dfs(1, -1);
for (int i = 2; i <= n; ++i) {
if (rud[i] == 1) {
sum += dep[i] - 1;
++tot;
}
}
double ans = 1.0 * sum / tot;
printf("%f\n",ans);
return 0;
}
`
AC代码
先dfs一边求出每个节点直接子节点的个数,到每个节点的概率是相等的,所以可以计算出每一条路径的概率
#include <iostream>
#include <cstdio>
#include <vector>
#include <cctype>
using namespace std;
const int N = 1e5 + 10;
int son[N];
int n;
double ans;
vector<int> G[N];
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);
ch = getchar();
}
return x * op;
}
void dfs1(int u, int fa){
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (v == fa) continue;
++son[u];
dfs1(v, u);
}
};
void dfs2(int u, int fa, int dis, double chance){
//这里的条件不能用G[u].size() == 0判断
//因为是双向边
if (son[u] == 0){
ans += 1.0 * dis * chance;
return;
}
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (v == fa) continue;
dfs2(v, u, dis + 1, chance / son[u]);
}
}
int main() {
n = read();
for (int i = 1, x, y ; i < n; ++i) {
x = read(), y = read();
G[x].push_back(y);
G[y].push_back(x);
}
dfs1(1, -1);
dfs2(1, -1, 0, 1.0);
printf("%.15f\n",ans);
return 0;
}

这篇博客讨论了在七王国中,Theon和Yara Greyjoy在第一座城市开始旅行的问题。由于雾天,他们无法确定马将带他们去哪里,马只会选择他们未到过的城市。问题求解的是在每条路长为1的情况下,他们旅程的预期长度。博客分析了问题的树形结构,并指出错误的计算方法,提供了正确的AC代码来计算每个节点到叶节点的路径概率。
&spm=1001.2101.3001.5002&articleId=110873081&d=1&t=3&u=40f17c79fac646afac1fe20139c38ed4)
649

被折叠的 条评论
为什么被折叠?



