C. Journey

来源:互联网 发布:城市网络超市 编辑:程序博客网 时间:2024/06/07 04:03
There are n cities andn - 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 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.

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 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.

Output

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.

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 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.


刚熟悉点前向星,就用前向星做的,开始runtime error 。。。 觉得数开的不小,忘记无向边乘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);}


原创粉丝点击