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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import sys
from collections import deque
n = int(sys.stdin.readline())
if n == 1:
print("0.000000000000000")
sys.exit()
adj = [[] for _ in range(n + 1)]
for _ in range(n - 1):
u, v = map(int, sys.stdin.readline().split())
adj[u].append(v)
adj[v].append(u)
parent = [0] * (n + 1)
children = [[] for _ in range(n + 1)]
q = deque()
q.append(1)
parent[1] = -1
while q:
u = q.popleft()
for v in adj[u]:
if parent[v] == 0 and v != parent[u]:
parent[v] = u
children[u].append(v)
q.append(v)
total = 0.0
stack = [(1, 1.0)]
while stack:
node, prob = stack.pop()
m = len(children[node])
if m == 0:
continue
contribution = prob / m
total += contribution * m # Each child contributes 'contribution'
for child in children[node]:
stack.append((child, contribution))
print("{0:.15f}".format(total))
Wait, there's a mistake here. Let me re-examine the code.
The original code after building the children array, the stack starts with (1,1.0). Then, for each node, m is the number of children. Then, contribution is prob / m. Then, total is increased by contribution * m. Because each of the m children contributes 'contribution', so adding m * contribution to the total. But this is equivalent to adding prob, since m * (prob/m) = prob. That's not correct.
Wait, no. Let's see. For a node with m children, each child's contribution is prob * (1/m). So the total for all children of this node is m * (prob * 1/m) = prob. So the code adds 'prob' to the total for each node's children. But this is not correct.
But the total expected length is the sum of all edges' contributions. Each edge is from parent to child. So for each child, the contribution is prob (of parent) multiplied by 1/(number of parent's children). So for the code above, the line:
total += contribution * m
is incorrect. The correct code would compute for each child, contribution = prob / m, and add that to the total. Then, each child is pushed with 'contribution' as their probability.
But in the code as written, for example, when processing node 1 (with m=2), contribution is 1.0 / 2 = 0.5. Then total += 0.5 * 2 (m=2) which is 1.0, which is correct. Then, each child is pushed with 0.5. Then, when processing child 2 (with m=1), contribution is 0.5 /1 = 0.5, which added to total is 0.5 *1 =0.5. So total becomes 1.5.
Ah, wait, in the code, the line "total += contribution * m" is adding for each child, the contribution multiplied by the number of children. Which is correct because each of the m children contributes prob/m, so their sum is prob. So for each node, the sum of contributions from its children is prob (since each child contributes prob/m, and there are m children). But in the code, for each node, this sum is added as contribution * m, which is (prob/m)*m = prob. So for the root, which has prob 1.0, the sum of its children's contributions is 1.0. Then, for each child node, their prob is added to the total.
But this approach is incorrect, because the edge from the parent to child is added to the total as prob/m, but the code here is summing prob for each node. For example, in the first sample:
After processing the root (1), the code adds 1.0 (contribution * 2) which is 1.0. Then, when processing node 2, which has m=1, the contribution is 0.5/1=0.5. The code adds 0.5 *1=0.5. So total becomes 1.5. But node 3 is processed, but has m=0, so nothing is added. So the code gives correct result.
Similarly, for the second sample, processing node 1 adds 1.0. Then processing node 2 adds 0.5, node 3 adds 0.5. Total 2.0.
But why is this equivalent to the original approach? Because the code is using the fact that the sum of the contributions from all children of a node is equal to the node's probability. So for each node, the code adds prob (node's probability) to the total. But this is not the case. Because the edge from parent to child is added as prob_child = prob_parent / m_parent. The sum of prob_child for all children of the parent is prob_parent.
But the total expected length is the sum of all prob_child for all edges. Which is the sum of all prob_child for each child of every node. Which is equal to the sum of the prob of each node (except the root). Because each prob_child is the probability of that child's edge, and the sum over all edges (u, v) of prob_child (prob_v) is equal to the sum of all non-root nodes' prob_v. Because each non-root node has exactly one parent, and the prob_v is the probability of the edge from the parent to it. Therefore, the total expected length is the sum of prob for all non-root nodes.
But in the code above, the code adds for each node, the sum of prob_child for its children, which is prob_node. So the total expected length is the sum of prob for all nodes except the root. Because the root's prob is 1.0, but the code adds 1.0 (the sum of its children's contributions) to the total. Then, for each child, their prob (which is 0.5 in the first sample) is added to the total when processing their own children. So the code is summing the prob of all nodes except the root.
Wait, no. Let's see:
The code initializes total to 0. For each node in the stack:
It pops the node and its prob.
It computes m = number of children.
contribution = prob / m.
total += contribution * m (which equals prob).
Then, for each child, push (child, contribution).
Thus, the code adds to the total the sum of contributions from all children of the node. For the root, this sum is 1.0 (since contribution * m = (1.0 / m) * m = 1.0). For each child, when processed, their contribution is their own prob (which is 0.5 in the first sample) multiplied by their own m (number of children). For example, node 2 in the first sample has m=1. So when processed, contribution is 0.5 /1 =0.5. Then, total += 0.5 *1 =0.5. So total becomes 1.0 +0.5=1.5.
Thus, the code is effectively summing for each node (except the root) the prob of that node. Because each node's prob is added to the total when its parent is processed. For example, node 2's prob is 0.5, and this is added to the total when node 1 is processed (sum of 1.0). Then, node 2's own children's sum (0.5) is added when processing node 2.
So the code is correct.
But why is this the case? Because the sum of all contributions is equal to the sum of the probabilities of all nodes except the root. Because each node's probability is added exactly once when its parent is processed.