C. Journey
来源:互联网 发布:城市网络超市 编辑:程序博客网 时间:2024/06/07 04:03
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 city1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the linkhttps://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 andvi (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 city1.
Your answer will be considered correct if its absolute or relative error does not exceed10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury isb. The checker program will consider your answer correct, if.
41 21 32 4
1.500000000000000
51 21 33 42 5
2.000000000000000
Note
In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city3 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.
#include<bits/stdc++.h>#define maxn 200000+10using namespace std;struct node{ int to,next;}edge[maxn];struct node1{ int id; double gai,num;};int cnt=0,head[maxn],vis[maxn];double ans;void add(int u,int v){ edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt++;}void bfs(int cur){ queue<node1> q; node1 a,tt; a.id=cur; a.gai=1; a.num=0; q.push(a); double j=0; while(!q.empty()) { a=q.front(); q.pop(); int count1=0; for(int k=head[a.id];k!=-1;k=edge[k].next) { if(!vis[edge[k].to]) count1++; } if(count1==0) { ans+=a.gai*a.num; continue; } for(int k=head[a.id];k!=-1;k=edge[k].next) { if(!vis[edge[k].to]) { vis[edge[k].to]=1; tt.gai=a.gai*1./count1; tt.id=edge[k].to; tt.num=a.num+1; q.push(tt); } } }}int main(){ int n; scanf("%d",&n); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); for(int i=0;i<n-1;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } vis[1]=1; bfs(1); printf("%.15lf\n",ans);}
- C. Journey
- C. Journey
- Codeforces 721C. Journey
- codeforces 839 C Journey
- Codeforces 839C Journey
- C. Journey【dfs + 期望】
- Codeforces 839C-Journey
- BUPT Summer Journey #test1 C
- BUPT Summer Journey #test2 C
- BUPT Summer Journey #test3 C
- BUPT Summer Journey #test4 C
- BUPT Summer Journey #test5 C
- BUPT Summer Journey #test6 C
- BUPT Summer Journey #test7 C
- BUPT Summer Journey #test8 C
- BUPT Summer Journey #test9 C
- BUPT Summer Journey #test11 C
- 【10.58%】【codeforces 721C】Journey
- New Bus Route (Codeforces-792A)
- hdu3530 Subsequence 单调队列
- 浅谈如何利用ip数据来辅助风控和安全系统
- OpenJudge 全排列
- hdu6118 最小费用最大流
- C. Journey
- Linux下MySQL数据库操作的常用命令行
- 解决Linux下编译.sh文件报错 “[: XXXX: unexpected operator”
- jquery easyUI 验证
- H5---面试题七
- 自动装箱与自动拆箱(jdk1.5后)(以int和Integer为例)
- cluster(集群)
- opencv安装和配置
- 使用opencv打开笔记本摄像头