1. 程式人生 > >codeforces 428div2 C journey dfs+期望

codeforces 428div2 C journey dfs+期望

連結:http://codeforces.com/contest/839/problem/C
C. Journey
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ n, ui ≠ 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
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.
百度翻譯題意:

··
有N個城市和N - 1路七國,每路連線了兩個城市,我們可以到達任何城市任何其他的道路。
席恩和Yara Greyjoy是在第一城的馬,他們開始穿越公路。但是天氣多霧,所以他們看不見馬把他們帶到哪兒了。當馬到達一個城市(包括第一個城市),它會轉到一個與當前城市相連的城市。但是它是一匹奇怪的馬,它只去那些以前沒有的城市。在每一個這樣的城市裡,馬都有相同的概率,如果沒有這樣的城市,它就會停下來。
讓每一條路的長度是1。1在城市開始旅程。他們旅程的預期長度(期望值)是多少?您可以通過連結讀取關於期望值(平均值)的資訊。
··

題目大意:
給一棵樹,結點1為根,每條邊長為1,從根出發,每到一個結點,可等概率的往其子樹走,到葉子則終止,求走過的路徑長度的期望。
分析:
這個題手算和用程式寫出來是不一樣的,首先n個點n-1條邊且任意兩點聯通,所以這個圖是一個無向樹,這樣要保證走過的路不再走只要保證走的下一條邊不是他的父親節點即可,那麼對於每個節點如何計算呢?

這裡寫圖片描述

    對於單個節點我們可以看出,除非它沒有子節點,否則無論有幾個位元組點,它下一步的期望一定是1。

這裡寫圖片描述

對於上圖的兩個樹來說,我們手動計算他們的期望發現,都是2, 

這裡寫圖片描述

   對於其中的2節點來說,除了他自身帶的概率,自身到下一步的概率依然是1,所以我們計算單個節點概率的公式就是
 if(該節點非葉子結點) p  =1.0+sum (子節點的概率之和)/ k(子節點個數)

code:

#include <iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n;
vector<int>o[101000];
double dfs(int x,int father)
{
    double sum  =0.0,k=0.0;
    int flag = 0;//判斷是不是葉子結點
    for(int i=0;i<o[x].size();i++)
    {
        int v =o[x][i];
        if(v==father)continue;//判斷是否走回父節點
        k+=1.0;
        flag = 1;
        sum+=dfs(v,x);
    }
    if(flag) return 1.0+sum/k;
    else return 0.0;

}
int main(){

    cin>>n;
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        o[a].push_back(b);
        o[b].push_back(a);
    }
    printf("%.7lf",dfs(1,0));
    return 0;
}