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 ≤ nui ≠ vi) — the cities connected by thei-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 .

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 ≤ nui ≠ vi) — the cities connected by thei-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.

题意:给你一棵树,每一条边的权值为1,让你求从1到叶子节点边权和的期望,到达每一个相邻节点的概率是一样的,不能走已走过的边。

题解:

dfs做一遍树形dp。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
double ans;
struct node
{
int next,to;
}edge[];
int head[],size=;
void putin(int from,int to)
{
size++;
edge[size].to=to;
edge[size].next=head[from];
head[from]=size;
}
void dfs(int u,int fa,double s,int depth)
{
int i,cnt=;
for(i=head[u];i!=-;i=edge[i].next)if(edge[i].to!=fa)cnt++;
if(cnt==){ans+=s*(double)depth;return;}
for(i=head[u];i!=-;i=edge[i].next)
if(edge[i].to!=fa)
dfs(edge[i].to,u,s*(double)(/(double)cnt),depth+);
}
int main()
{
int i,j;
memset(head,-,sizeof(head));
scanf("%d",&n);
for(i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
putin(a,b);
putin(b,a);
}
dfs(,,,);
printf("%.15lf",ans);
return ;
}

C. Journey的更多相关文章

  1. CF721C. Journey[DP DAG]

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  2. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  3. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  4. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  5. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  6. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  7. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  8. 【推公式】UVa 10995 - Educational Journey

    1A~,但后来看人家的代码好像又写臭了,T^T... Problem A: Educational journey The University of Calgary team qualified f ...

  9. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  10. HDU 5477 A Sweet Journey 水题

    A Sweet Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. PS 滤镜— —水波效果

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  2. Qt之log数据展示模块简要实现

    Log模块主要用于实时测井数据的显示和测后曲线数据的预览和打印,为更好的展示对Qt中相关知识点的应用,特以Log模块为例对其进行简要实现. 内容导图: 一.功能需求 1.界面效果图 Log模块实现曲线 ...

  3. hdu3518 Boring Counting[后缀排序]

    裸的统计不同的重复出现子串(不重叠)种数的题.多次使用后缀排序要注意小细节.y数组在重复使用时一定要清空,看那个line25 +k就明白了 ,cnt也要清空,为什么就不说了 #include<b ...

  4. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  5. 洛谷P2530 [SHOI2001]化工厂装箱员

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不 ...

  6. boost::function 通过boost::bind调用类成员函数

    1. 首先引用boost::function和boost::bind的头文件和库: #include "boost/bind.hpp" #include "boost/f ...

  7. sql 的基础语句

    USE day15; -- 创建表CREATE TABLE teacher( id INT, NAME VARCHAR(20))-- 查看所有表SHOW TABLES; DESC student; D ...

  8. 杂项随记:gcc/objdump/section等

    gcc -g 如果不打开-g或者-ggdb(GDB专用)调试开关,GCC编译时不会加入调试信息,因为这会增大生成代码的体积.GCC采用了分级调试,通过在-g选项后附加数字1.2或3来指定在代码中加入调 ...

  9. JAVA操作cassandra数据库

    如果是maven项目,在pom.xml里加入依赖.不是的话下载相应的jar包放到lib目录下.这里驱动包的版本要和你cassandra的大版本一致.我这里cassandra的版本是最新的3.9,驱动是 ...

  10. ceph 删除了osd但是osd目录保存完整如何恢复

    1. 这里假设有一个osd被删除了 执行下列步骤删除: ceph osd out osd.0 service ceph stop osd.0 ceph osd crush remove osd.0 c ...