C. Journey
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.
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 thei-th road.
It is guaranteed that one can reach any city from any other by the roads.
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 .
2 seconds
256 megabytes
standard input
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.
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 thei-th road.
It is guaranteed that one can reach any city from any other by the roads.
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 .
4
1 2
1 3
2 4
1.500000000000000
5
1 2
1 3
3 4
2 5
2.000000000000000
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的更多相关文章
- CF721C. Journey[DP DAG]
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- 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 ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 【推公式】UVa 10995 - Educational Journey
1A~,但后来看人家的代码好像又写臭了,T^T... Problem A: Educational journey The University of Calgary team qualified f ...
- poj 3544 Journey with Pigs
Journey with Pigs Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3004 Accepted: 922 ...
- HDU 5477 A Sweet Journey 水题
A Sweet Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- CSS实现简单无缝滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 洛谷P3252 [JLOI2012]树
题目描述 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节点1是根节点,根的深度是0,它的儿子节点的深度为1.路径不 ...
- MongoDB中的一些坑(最好不要用)
MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless,大部分情况下性能也很棒.但是薄荷 ...
- python反复执行某个命令
#! /usr/bin/env python #coding=utf-8 # 以需要的时间间隔执行某个命令 import time, os def re_exe(cmd, inc = 60 ...
- 数据库小记:根据指定名称查询数据库表名及根据指定名称查询数据库所有表中的字段名称(支持mysql/postgre)
意:本篇文章仅适用于mysql和postgre这两种数据库 1.查询数据库中所有表名及对应表的详细信息 select * from INFORMATION_SCHEMA.tables 2.根据指定名称 ...
- Java远程调用
一. 概念: RMI全称是Remote Method Invocation-远程方法调用,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网络分布式应用系统的核心解决方案之一.它支持 ...
- Tomcat加载JSP原理
Tomcat加载JSP主要有以下几个类: JspServlet JspServletWrapper JspCompilationContext JspRuntimeContext JsperLoade ...
- [hdu4405]Aeroplane chess(概率dp)
题意:某人掷骰子,数轴上前进相应的步数,会有瞬移的情况,求从0到N所需要的期望投掷次数. 解题关键:期望dp的套路解法,一个状态可以转化为6个状态,则该状态的期望,可以由6个状态转化而来.再加上两个状 ...
- (转)apache2.2.x+tomcat7.0.x集群+…
apache http server下载地址 http://httpd.apache.org/download.cgi#apache22 这里下载的是httpd-2.2.21-win32-x86-op ...
- Linux系统下如何设置IP地址?
Linux系统下如何设置IP地址?我们可以通过命令设定IP的方法,不过此方法的前提条件是用户需root权限.在linux系统的 /etc/sysconfig/network-script/ifcfg- ...