Codeforces 839C Journey【DFS】
C. Journey
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
题目链接:http://codeforces.com/contest/839/problem/C
分析:有空补上,DFS做法
下面给出AC代码:
1 #include<cstdio>
2 const int N=100010;
3 int n,i,x,y,g[N],v[N<<1],nxt[N<<1],ed;double f[N];
4 void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
5 void dfs(int x,int y){
6 int deg=0;
7 for(int i=g[x];i;i=nxt[i])if(v[i]!=y){
8 deg++;
9 dfs(v[i],x);
10 f[x]+=f[v[i]];
11 }
12 if(deg)f[x]=1.0*f[x]/deg+1;
13 }
14 int main(){
15 scanf("%d",&n);
16 for(i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
17 dfs(1,0);
18 printf("%.10f",f[1]);
19 }
- R语言学习 - 非参数法生存分析
- 初学File类
- R语言学习 - 图形设置中英字体
- 递归思想解决输出目录下的全部文件
- Linux学习 - 又双叒叕一个软件安装方法
- 在Python中实现你自己的推荐系统
- 在Java中如何实现“Pless presss any key to continue.”
- 震惊小伙伴的Python单行代码
- 如何打造一款可靠的WAF(Web应用防火墙)
- 为啥我的Python这么慢 (一)
- 矩阵的基本知识构造重复矩阵的方法——repmat(xxx,xxx,xxx)构造器的构造方法单位数组的构造方法指定公差的等差数列指定项数的等差数列指定项数的lg等差数列sub2ind()从矩阵索引==》
- 简单的Writer和ReaderWriter的主要方法Writer工作原理Reader的主要方法readLine():String
- 为啥我的Python这么慢 - 项查找 (二)
- 初识字节流+实现缓冲字节流OutputStream的主要方法构造方法读关流实现BufferedInputStream实现BufferedOutputStream为什么read()返回的是Int型而不是
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 排障集锦:九九八十一难之第二十难!-----使用jenkins镜像出现的问题Can not write to /var/jenkins_home/copy_reference_file.log.
- 人生苦短,我用k8s--------------secret和configmap的配置管理
- Go Colly抓取豆瓣电影Top250
- knative client 使用knative client 使用
- Java_web 学习1 Maven安装和使用2 mybatis的配置文件的详解mybatis缓冲mybatis注解延迟加载${} 与 #{} 区别【笔试题】
- Python3 的面向对象
- 【JVM】Int类型在栈中是否会被缓存?
- Python 【os.path()模块解析】
- Pythonic【15个代码示例】
- Django REST framwork的权限验证实例
- Python任务调度利器之APScheduler详解
- Django Admin后台添加数据库视图过程解析
- Python 线性回归分析以及评价指标详解
- Python实现从N个数中找到最大的K个数
- .NET开源工具类库-Masuit.Tools