Codeforce 839C Journey (dfs+概率)

来源:互联网 发布:网络团队愿景怎么写 编辑:程序博客网 时间:2024/06/08 11:55
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 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
41 21 32 4
output
1.500000000000000
input
51 21 33 42 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去找每一条路径,但问题是怎么处理期望问题,刚开始的想法是求出每一次的路径长度,然后求出总的路径数,最后一除做期望,但问题是这是一个1为根的树,遍历一遍整棵树就完了,根本没法这么计算,然后想用一个数组实现,数组中存到第i个结点的期望,就类似于一个记忆化搜索的题,可怎么也调试不出来,这里有一个知识点死活没想到,也可以说根本没接触过,就是当前的期望等于前一步的期望除以路径总数+1,这样dfs最后输出ans[1]就ok了,高中概率没学好吧,就差了这一步。

还有一种做法就是得到走每一条路径的概率,这样在dfs的一开始搜一遍就可以得到,用当前概率除以边的数量就等于走下一条边的概率,然后当下面没有边时,直接用概率*路径的数量就可以得到期望。dfs很简单,就是概率太难想到。

方法一代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<set>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}struct node{int u,v,next;}p[maxn];int cnt,head[maxn];double ans[maxn];void init(){cnt=0;mset(head,-1);mset(ans,0);}void add(int x,int y){p[cnt].u=x;p[cnt].v=y;p[cnt].next=head[x];head[x]=cnt++;p[cnt].u=y;p[cnt].v=x;p[cnt].next=head[y];head[y]=cnt++;}void dfs(int x,int y){    int sum=0,i;    for(i=head[x];~i;i=p[i].next)    {    if(p[i].v!=y)    {    sum++;        dfs(p[i].v,x);        ans[x]+=ans[p[i].v];    }}    if(sum)    {    ans[x]=1.0*ans[x]/sum+1;}}int main(){int n,i,j,x,y;while(~scanf("%d",&n)){init();for(i=0;i<n-1;i++){scanf("%d%d",&x,&y);add(x,y);}if(n==1){cout<<"0.000000000000000"<<endl;continue;}dfs(1,0);printf("%.8f\n",ans[1]);}return 0;}

方法二代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<set>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}struct node{int u,v,next;}p[maxn];int cnt,head[maxn],vis[maxn];double ans;void init(){cnt=0;mset(head,-1);mset(vis,0);}void add(int x,int y){p[cnt].u=x;p[cnt].v=y;p[cnt].next=head[x];head[x]=cnt++;p[cnt].u=y;p[cnt].v=x;p[cnt].next=head[y];head[y]=cnt++;}void dfs(int x,int f,int step,double pp){   int s=0;   for(int i=head[x];~i;i=p[i].next)   {   if(p[i].v!=f)   s++;}if(s==0){ans+=pp*step;return ;}for(int i=head[x];~i;i=p[i].next){if(!vis[p[i].v]){vis[p[i].v]=1;dfs(p[i].v,x,step+1,pp/s);}}}int main(){int n,i,j,x,y;while(~scanf("%d",&n)){init();for(i=0;i<n-1;i++){scanf("%d%d",&x,&y);add(x,y);}if(n==1){cout<<"0.000000000000000"<<endl;continue;}vis[1]=1;ans=0;dfs(1,-1,0,1);printf("%.8f\n",ans);}return 0;}


原创粉丝点击