Codeforces #428 (Div. 2) C. Journey (DFS
来源:互联网 发布:和孩子一起学编程 编辑:程序博客网 时间:2024/05/21 05:17
C. Journey
Description
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 .
Sample Input
41 21 32 4
51 21 33 42 5
Sample Output
1.500000000000000
2.000000000000000
Hint
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.
题意
给一棵树 问从1开始 两个人分别一条路的终点的期望
题解:
挺有意思的一道DFS
AC代码
#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int INF = 0x3f3f3f3f;const LL INFLL = 0x3f3f3f3f3f3f3f3f;const int N = 2e5+10;vector<int> G[N];double dfs(int x, int y){ double sum = 0.0, k = 0.0; bool flag = false; for(int i = 0; i < G[x].size(); i++) { if(G[x][i] == y) continue; k += 1.0; flag = true; sum += dfs(G[x][i],x); } if(flag) return 1.0+sum/k; return 0.0;}int main(){ int n; scanf("%d",&n); int x, y; for(int i = 1; i < n; i++) { scanf("%d%d",&x,&y); G[x].push_back(y); G[y].push_back(x); } printf("%.10lf\n",dfs(1,0));return 0;}
- Codeforces #428 (Div. 2) C. Journey (DFS
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- Codeforces Round #428 (Div. 2) C. Journey(dfs)
- Codeforces Round #374 (Div. 2) C. Journey(dfs+dp)
- Codeforces Round #428 (Div. 2) C.Journey(dfs求期望)
- C. Journey(dfs求期望+Codeforces Round #428 (Div. 2))
- Codeforces Round #428 (Div. 2) C. Journey(【dfs】求期望值)
- Codeforces Round #428 (Div. 2) Journey(数学期望+dfs)
- codeforces 428div2 C journey dfs+期望
- Codeforces Round #428 (Div. 2)-搜索&期望&细节-C. Journey
- CF——Codeforces Round #428 (Div. 2)C. Journey
- Codeforces Round #374 (Div. 2) C. Journey
- Codeforces Round #374 (Div. 2) C. Journey
- Codeforces Round #374 (Div. 2)C. Journey
- Codeforces Round #374 (Div. 2)-C. Journey
- 【Codeforces 839 C. Journey】& dfs
- Codeforces Round #407 (Div. 2) D Weird journey [dfs]
- Codeforces Round #374 (Div. 2) -- C. Journey (DP)
- http一个完整的请求?
- Java源码阅读之HashMap
- 如何处理二进制文件
- android studio gradle/gradle plugin 配置详解,解决"Gradle sync failed: Unknown host 'services.gradle.org'."
- 杭电acm2017 字符数量统计
- Codeforces #428 (Div. 2) C. Journey (DFS
- Spring-AOP基础知识
- java版poi+excel导入实例1
- 尾递归
- DDNS(花生壳)
- 数据结构-红黑树
- bzoj 1601: [Usaco2008 Oct]灌水(最小生成树)
- Codeforces Round #384 (Div. 2) A. Vladik and flights
- 2017.08.12