博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 839C Journey【DFS】
阅读量:6703 次
发布时间:2019-06-25

本文共 2395 字,大约阅读时间需要 7 分钟。

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 .

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.

题目链接:

分析:有空补上,DFS做法

下面给出AC代码:

1 #include
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

 

转载地址:http://lzzlo.baihongyu.com/

你可能感兴趣的文章
POJ 3278 Catch That Cow(BFS,板子题)
查看>>
在Winform开发中使用FastReport创建报表
查看>>
vuejs监听苹果iphone手机键盘事件
查看>>
Spring中基于AOP的@AspectJ
查看>>
excel vba 实现跨表单(sheet) 搜索 - 显示搜索行记录搜索历史
查看>>
Dos命令下目录操作
查看>>
Unity长连接
查看>>
cocos2d-x-3.1 数据结构之Vector (coco2d-x 学习笔记六)
查看>>
将树莓派Raspberry Pi设置为无线路由器(WiFi热点AP,RTL8188CUS芯片)
查看>>
OA系统权限管理设计方案
查看>>
TI C66x DSP 系统events及其应用 - 5.11(中断控制寄存器)
查看>>
《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录
查看>>
『Scrapy』全流程爬虫demo
查看>>
Android跳转到系统Wifi界面的方式
查看>>
细说业务逻辑 -- 丢失的业务逻辑层
查看>>
阿里云自动快照有什么用,如何设置?
查看>>
xshell 与 putty
查看>>
Oracle用户、权限、角色管理
查看>>
2. Ext中关于Ext.QuickTips.init()的使用
查看>>
SIGTERM等信号含义【转】
查看>>