Problem:
 Max Score: $800$ Points   Problem Statement There is an undirected connected graph with $N$ vertices and $N-1$ edges. The i -th edge connects u_i and v_i .  E869120 the coder moves in the graph as follows:   He move to adjacent vertex, but he can't a visit vertex two or more times.  He ends move when there is no way to move.  Otherwise, he moves randomly. (equal probability) If he has $p$ way to move this turn, he choose each vertex with $1/p$ probability.     Calculate the expected value of the number of turns, when E869120 starts from vertex i , for all i (1 ≤ i ≤ N) .       Input The input is given from standard input in the following format.    N  u_1  v_1  u_2  v_2 : u_{N-1}  v_{N-1}       Output In i -th (1 ≤ i ≤ N) line, print the expected vaule of the number of turns E869120 moves. The relative error or absolute error of output should be within 10^{-6} .    Constraints   $1 \le N \le 150,000$  The graph is connected.     Subtasks  Subtask 1 [ $190$ points ]   There is no vertex which degree is more than 2.  This means the graph looks like a list.    Subtask 2 [ $220$ points ]   1 ≤ N ≤ 1000 .    Subtask 3 [ $390$ points ]   There are no additional constraints.         Sample Input 1  4
1 2
2 3
2 4      Sample Output 1  2.0
1.0
2.0
2.0     Sample Input 2  4
1 2
2 4
4 3

Solution (Python):
class Tree():
    def __init__(self, n, decrement=1):
        self.n = n
        self.edges = [[] for _ in range(n)]
        self.root = None
        self.size = [1]*n       
        self.decrement = decrement

    def add_edge(self, u, v):
        u, v = u-self.decrement, v-self.decrement
        self.edges[u].append(v)
        self.edges[v].append(u)

    def add_edges(self, edges):
        for u, v in edges:
            u, v = u-self.decrement, v-self.decrement
            self.edges[u].append(v)
            self.edges[v].append(u)

    def set_root(self, root):
        root -= self.decrement
        self.depth = [-1]*self.n
        self.root = root
        self.par = [-1]*self.n
        self.depth[root] = 0
        self.order = [root]
        next_set = [root]
        while next_set:
            p = next_set.pop()
            for q in self.edges[p]:
                if self.depth[q] != -1: continue
                self.par[q] = p
                self.depth[q] = self.depth[p]+1
                self.order.append(q)
                next_set.append(q)
        for p in self.order[::-1]:
            for q in self.edges[p]:
                if self.par[p] == q: continue
                self.size[p] += self.size[q]

    def rerooting(self, op, merge, id):
        
        dp1 = [id] * self.n
        dp2 = [id] * self.n
        for p in self.order[::-1]:
            t = id
            for q in self.edges[p]:
                if self.par[p] == q: continue
                dp2[q] = t
                t = merge(t, op(dp1[q], p, q))
            t = id
            for q in self.edges[p][::-1]:
                if self.par[p] == q: continue
                dp2[q] = merge(t, dp2[q])
                t = merge(t, op(dp1[q], p, q))
            dp1[p] = t
        for q in self.order[1:]:
            pq = self.par[q]
            dp2[q] = op(merge(dp2[q], dp2[pq]), q, pq)
            dp1[q] = merge(dp1[q], dp2[q])
        return dp1


import sys
input = sys.stdin.readline







N = int(input())
T = Tree(N)
for _ in range(N-1):
    x, y = map(int, input().split())
    T.add_edge(x,y)
T.set_root(1)


def op(a, p, q):
    
    
    return a/(len(T.edges[q])-1) + 1 if a != 0 else 1

def merge(a, b):
    
    return a + b


id = 0


dp = T.rerooting(op, merge, id)
res = [""]*N
for i in range(N):
    res[i] = str(dp[i]/len(T.edges[i]))
print("\n".join(res))

Solution (Python):
import sys,os,io

input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
N = int(input())
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  while stack:
    v = stack[-1]
    marker = 0
    for u in edge[v]:
      if u==parent[v]:
        continue
      if parent[u]==N: 
        marker = 1
        parent[u]=v
        stack.append(u)
      else: 
        pass
        
        ans[v] += ans[u]+1
    if marker==0:
      stack.pop()
      if v==start:
        
        ans[v] /= len(edge[v])
      elif ans[v]>0:
        ans[v] /= len(edge[v])-1
  return

def dfs2(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  p_value = [0]*N
  while stack:
    v = stack.pop()
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        continue
      parent[u] = v
      if len(edge[v])==1:
        p_value[u] = 0
      else:
        p_value[u] = (ans[v]*len(edge[v])-(ans[u]+1))/(len(edge[v])-1)
      ans[u] *= len(edge[u])-1
      ans[u] += p_value[u]+1
      ans[u] /= len(edge[u]) 
      stack.append(u)
  return

ans = [0]*N
dfs(0)
dfs2(0)
print(*ans, sep='\n')

Solution (Python):
import sys,os,io
input = sys.stdin.readline

N = int(input())
edge = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

def dfs(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  while stack:
    v = stack[-1]
    marker = 0
    for u in edge[v]:
      if u==parent[v]:
        continue
      if parent[u]==N: 
        marker = 1
        parent[u]=v
        stack.append(u)
      else: 
        pass
        
        ans[v] += ans[u]+1
    if marker==0:
      stack.pop()
      if v==start:
        
        ans[v] /= len(edge[v])
      elif ans[v]>0:
        ans[v] /= len(edge[v])-1
  return

def dfs2(start):
  stack = [start]
  parent = [N]*N
  parent[start] = -1
  p_value = [0]*N
  while stack:
    v = stack.pop()
    for i,u in enumerate(edge[v]):
      if u==parent[v]:
        continue
      parent[u] = v
      if len(edge[v])==1:
        p_value[u] = 0
      else:
        p_value[u] = (ans[v]*len(edge[v])-(ans[u]+1))/(len(edge[v])-1)
      ans[u] *= len(edge[u])-1
      ans[u] += p_value[u]+1
      ans[u] /= len(edge[u]) 
      stack.append(u)
  return

ans = [0]*N
dfs(0)
dfs2(0)
print(*ans, sep='\n')

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque, Counter

N = int(input())
X = [[] for i in range(N)]
L = [0]*N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    L[x-1] += 1
    L[y-1] += 1

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

C = Counter(P)

unit = 0
merge = lambda a, b: a + b
adj_bu = lambda a, i: (a+1)/C[P[i]]
adj_td = lambda a, i, p: ((XX[p]*L[p]-XX[i]*C[p])/max(1,L[p]-1)+1)/max(1,C[i])
adj_fin = lambda a, i: a*max(1,C[i])/L[i]


ME = [unit] * N
XX = [0] * N
TD = [unit] * N
for i in R[1:][::-1]:
    XX[i] = adj_bu(ME[i], i)
    p = P[i]
    ME[p] = merge(ME[p], XX[i])
XX[R[0]] = adj_fin(ME[R[0]], R[0])

for i in R:
    ac = unit
    for j in reversed(X[i]):
        TD[j] = adj_td(merge(TD[j], ac), j, i)
        ac = merge(ac, XX[j])
        XX[j] = adj_fin(merge(ME[j], TD[j]), j)

print(*XX, sep = "\n")

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque, Counter

N = int(input())
X = [[] for i in range(N)]
L = [0]*N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    L[x-1] += 1
    L[y-1] += 1

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

C = Counter(P)

unit = 0
merge = lambda a, b: a + b
adj_bu = lambda a, i: (a+1)/C[P[i]]
adj_td = lambda a, i, p: ((XX[p]*L[p]-XX[i]*C[p])/max(1,L[p]-1)+1)/max(1,C[i])
adj_fin = lambda a, i: a*max(1,C[i])/L[i]


ME = [unit] * N
XX = [0] * N
TD = [unit] * N
for i in R[1:][::-1]:
    XX[i] = adj_bu(ME[i], i)
    p = P[i]
    ME[p] = merge(ME[p], XX[i])
XX[R[0]] = adj_fin(ME[R[0]], R[0])

for i in R:
    ac = TD[i]
    for j in X[i]:
        TD[j] = ac
        ac = merge(ac, XX[j])

    ac = unit
    for j in reversed(X[i]):
        TD[j] = adj_td(merge(TD[j], ac), j, i)
        ac = merge(ac, XX[j])
        XX[j] = adj_fin(merge(ME[j], TD[j]), j)

print(*XX, sep = "\n")

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque, Counter

N = int(input())
X = [[] for i in range(N)]
L = [0]*N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    L[x-1] += 1
    L[y-1] += 1

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

C = Counter(P)

unit = 0
merge = lambda a, b: a + b
adj_bu = lambda a, i: (a+1)/C[P[i]]
adj_td = lambda a, i, p: ((XX[P[i]]*L[P[i]]-XX[i]*C[P[i]])/max(1,L[P[i]]-1)+1)/max(1,C[i])
adj_fin = lambda a, i: a*max(1,C[i])/L[i]


ME = [unit] * N
XX = [0] * N
TD = [unit] * N
for i in R[1:][::-1]:
    XX[i] = adj_bu(ME[i], i)
    p = P[i]
    ME[p] = merge(ME[p], XX[i])
XX[R[0]] = adj_fin(ME[R[0]], R[0])

for i in R:
    ac = TD[i]
    for j in X[i]:
        TD[j] = ac
        ac = merge(ac, XX[j])

    ac = unit
    for j in reversed(X[i]):
        TD[j] = adj_td(merge(TD[j], ac), j, i)
        ac = merge(ac, XX[j])
        XX[j] = adj_fin(merge(ME[j], TD[j]), j)

print(*XX, sep = "\n")

Solution (Python):

def resolve():
    def dfs1(topo, par):
        for idx in reversed(topo):
            ret = 0
            child = 0
            for to in G[idx]:
                if to == par[idx]:
                    continue
                ret += EV[to] + 1.0
                child += 1
            
            if child >= 1:
                EV[idx] += ret / child


    def dfs2(idx, d_par, par):
        stack = [(idx, d_par, par)]
        while stack:
            idx, d_par, par = stack.pop()
            ret = 0
            for to in G[idx]:
                if to == par:
                    ret += d_par + 1.0
                else:
                    ret += EV[to] + 1.0
            ans[idx] = ret / len(G[idx])
            for to in G[idx]:
                if to == par:
                    continue
                max_l = max(1, len(G[idx]) - 1)
                nx_d_par = (ret - EV[to] - 1.0) / max_l
                stack.append((to, nx_d_par, idx))

    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    topo = []
    P = [-1] * N
    node = [0]
    while node:
        v = node.pop()
        topo.append(v)
        for to in G[v]:
            if to == P[v]:
                continue
            P[to] = v
            node.append(to)

    EV = [0] * N
    ans = [0] * N
    dfs1(topo, P)
    dfs2(0, 0, -1)
    for i in range(N):
        print(ans[i])

if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    def dfs1(idx, par):
        ret = 0
        child = 0
        for to in G[idx]:
            if to == par:
                continue
            dfs1(to, idx)
            ret += EV[to] + 1.0
            child += 1
        EV[idx] = 0
        if child >= 1:
            EV[idx] += ret / child

    def dfs2(idx, d_par, par):
        ret = 0
        for to in G[idx]:
            if to == par:
                ret += d_par + 1.0
            else:
                ret += EV[to] + 1.0
        ans[idx] = ret / len(G[idx])
        for to in G[idx]:
            if to == par:
                continue
            max_l = max(1, len(G[idx]) - 1)
            nx_d_par = (ret - EV[to] - 1.0) / max_l
            dfs2(to, nx_d_par, idx)

    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    EV = [0] * N
    ans = [0] * N
    dfs1(0, -1)
    dfs2(0, 0, -1)
    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    resolve()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
 
    N = int(input())
 
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        G[u].append(v)
        G[v].append(u)
 
    connect = [len(g) for g in G]
 
    
    root = 0
    stack = [root]
    order = []
    lst = [0] * N
    while len(stack) != 0:
        tmp = stack.pop()
        order.append(tmp)
        for to in G[tmp]:
            if to == lst[tmp]:
                continue
            lst[to] = tmp
            stack.append(to)
        
    dp1 = [0] * N
    for v in order[::-1]:
        d = connect[v]
        if v != root and d == 1:
            continue
        x = sum(dp1[c] for c in G[v])
        if v == root:
            dp1[v] = x/connect[v] + 1
        else:
            dp1[v] = x/(connect[v] - 1) + 1
 
    dp2 = [0] * N
    for v in order:
        d = connect[v]
        n_child = d if v == root else d - 1
        S = dp1[v] * n_child
        if v != root:
            S += dp2[v] + 1
        for c in G[v]:
            if c == lst[v]:
                continue
            if d == 1:
                dp2[c] = 0
            else:
                dp2[c] = (S - dp1[c] - 1)/(d - 1)
 
    ans = []
    for v in range(N):
        d = connect[v]
        n_child = d if v == root else d - 1
        S = dp1[v] * n_child
        if v != root:
            S += dp2[v] + 1
        ans.append(S/d)
 
    print (*ans, sep = '\n')
 
    
    
 
if __name__ == '__main__':
    main()

Solution (Python):
class Tree():
    def __init__(self, n, edge):
        self.n = n
        self.tree = [[] for _ in range(n)]
        for e in edge:
            self.tree[e[0] - 1].append(e[1] - 1)
            self.tree[e[1] - 1].append(e[0] - 1)

    def setroot(self, root):
        self.root = root
        self.parent = [None for _ in range(self.n)]
        self.parent[root] = -1
        self.depth = [None for _ in range(self.n)]
        self.depth[root] = 0
        self.order = []
        self.order.append(root)
        stack = [root]
        while stack:
            node = stack.pop()
            for adj in self.tree[node]:
                if self.parent[adj] is None:
                    self.parent[adj] = node
                    self.depth[adj] = self.depth[node] + 1
                    self.order.append(adj)
                    stack.append(adj)

    def rerooting(self, func, merge, ti, ei):
        dp = [ti for _ in range(self.n)]
        lt = [ei for _ in range(self.n)]
        rt = [ei for _ in range(self.n)]
        inv = [ei for _ in range(self.n)]
        self.setroot(0)
        for node in self.order[::-1]:
            if len(self.tree[node]) == 1 and node != 0:
                continue
            tmp = ti
            for adj in self.tree[node]:
                if self.parent[adj] == node:
                    lt[adj] = tmp
                    tmp = func(tmp, dp[adj])
            tmp = ti
            for adj in self.tree[node][::-1]:
                if self.parent[adj] == node:
                    rt[adj] = tmp
                    tmp = func(tmp, dp[adj])
            dp[node] = tmp / (len(self.tree[node]) - 1) if node != 0 else tmp / len(self.tree[node])
        for node in self.order:
            if node == 0:
                continue
            merged = merge(lt[node], rt[node])
            par = self.parent[node]
            inv[node] = func(merged, inv[par]) / (len(self.tree[par]) - 1) if len(self.tree[par]) != 1 else 0
            dp[node] = func(dp[node] * (len(self.tree[node]) - 1), inv[node]) / len(self.tree[node])
        return dp
'''
    def rerooting(self, func, merge, ti, ei):
        dp = [ti for _ in range(self.n)]
        lt = [ei for _ in range(self.n)]
        rt = [ei for _ in range(self.n)]
        inv = [ei for _ in range(self.n)]
        self.setroot(0)
        for node in self.order[::-1]:
            tmp = ti
            for adj in self.tree[node]:
                if self.parent[adj] == node:
                    lt[adj] = tmp
                    tmp = func(tmp, dp[adj])
            tmp = ti
            for adj in self.tree[node][::-1]:
                if self.parent[adj] == node:
                    rt[adj] = tmp
                    tmp = func(tmp, dp[adj])
            dp[node] = tmp
        for node in self.order:
            if node == 0:
                continue
            merged = merge(lt[node], rt[node])
            par = self.parent[node]
            inv[node] = func(merged, inv[par])
            dp[node] = func(dp[node], inv[node])
        return dp
'''

import sys
input = sys.stdin.readline

N = int(input())
E = [tuple(map(int,input().split())) for _ in range(N - 1)]

T = Tree(N, E)

ti = 0.0
ei = -1.0
func = lambda x, y: x + y + 1.0
merge = lambda x, y: x + y

D = T.rerooting(func, merge, ti, ei)

res = []

for i in range(N):
    res.append(D[i])

print('\n'.join(map(str, res)))

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    v = [[] for i in range(n)]
    for i in range(n-1):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
    q = [0]
    q2 = []
    s = [0]*n
    e = [0]*n
    e[0] = 1
    while q:
        x = q.pop()
        for y in v[x]:
            if not e[y]:
                s[x] += 1
                e[y] = 1
                q.append(y)
                q2.append((y,x))

    while q2:
        y,x = q2.pop()
        e[x] += e[y]/s[x]
    ans = [None]*n
    ans[0] = e[0]
    q.append((0,s[0],e[0]))
    while q:
        x,sx,ex = q.pop()
        nsx = sx-1
        for y in v[x]:
            if ans[y] is None:
                sy,ey = s[y],e[y]
                nex = ((ex-1)*sx-ey)/nsx+1 if nsx else 1
                nsy = sy+1
                ans[y] = ney = ((ey-1)*sy+nex)/nsy+1
                q.append((y,nsy,ney))

    for i in ans:
        print(i-1)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    """ここから変更"""

    
    def op(a, b):
        return a+b+1.

    
    def cum_merge(a, b):
        return a+b

    
    ident_cum_merge = 0.

    
    dp = [0.] * (N+1)
    from_par = [-1.] * (N+1)
    """ここまで変更"""

    
    
    
    def Rerooting(adj):
        N = len(adj) - 1
        st = [1]
        seen = [0] * (N + 1)
        seen[1] = 1
        par = [0] * (N + 1)
        child = [[] for _ in range(N + 1)]
        seq = []
        while st:
            v = st.pop()
            seq.append(v)
            for u in adj[v]:
                if not seen[u]:
                    seen[u] = 1
                    par[u] = v
                    child[v].append(u)
                    st.append(u)
        seq.reverse()
        left = [ident_cum_merge] * (N + 1)
        right = [ident_cum_merge] * (N + 1)
        for v in seq:
            L = len(child[v])
            if L:
                tmp = ident_cum_merge
                for u in child[v]:
                    left[u] = tmp
                    tmp = op(tmp, dp[u])
                tmp = ident_cum_merge
                for u in reversed(child[v]):
                    right[u] = tmp
                    tmp = op(tmp, dp[u])
                dp[v] = tmp / L

        seq.reverse()
        for v in seq:
            if v == 1:
                continue
            if len(adj[par[v]]) > 1:
                from_par[v] = op(cum_merge(left[v], right[v]), from_par[par[v]]) / (len(adj[par[v]]) - 1)
            else:
                from_par[v] = 0.
            dp[v] = op(dp[v] * (len(adj[v]) - 1), from_par[v]) / len(adj[v])

        return dp

    ans = Rerooting(adj)
    for v in range(1, N+1):
        print(ans[v])


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N = int(input())
e = dd(list)
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

s = [1]
order = []
parent = [-1] * (N + 1)
vis = [0] * (N + 1)
while len(s):
  x = s.pop()
  vis[x] = 1
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    s.append(y)
    parent[y] = x

dp = [0] * (N + 1)
esize = [0] * (N + 1)
for x in order[N - 1: 0: -1]: esize[parent[x]] += 1


for x in order[N - 1: 0: -1]:
  y = parent[x]
  dp[y] += (dp[x] + 1) / esize[y]

res = [0] * (N + 1)
res[1] = dp[1]
for x in order[1: ]:
  y = parent[x]
  t = 0
  if y != 1 or (esize[y] > 1): t = (res[y] * (esize[y] + (y != 1)) - (dp[x] + 1)) / (esize[y] - (y == 1))
  
  res[x] = ((t + 1) / (esize[x] + 1)) + (dp[x] * esize[x] / (esize[x] + 1))
for r in res[1: ]: print(r)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
T = []
while Q:
    i = deque.popleft(Q)
    T.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

BU = [0] * N 
TD = [1] * N 
for i in T[::-1]:
    if len(X[i]) == 0:
        BU[i] = 0
        continue
    s = 0
    for j in X[i]:
        s += BU[j]
    BU[i] = s / len(X[i]) + 1

for i in T:
    if i == 0:
        for j in X[i]:
            TD[j] = 2 + ((BU[i]-1) * len(X[i]) - BU[j]) / (len(X[i]) - 1) if len(X[i]) > 1 else 1
    else:
        for j in X[i]:
            TD[j] = BU[i] + (TD[i] - BU[j] - 1) / len(X[i]) + 1

for i in range(N):
    print((len(X[i]) * BU[i] + TD[i]) / (len(X[i]) + 1) if i else BU[i])

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    N = int(input())

    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        G[u].append(v)
        G[v].append(u)

    connect = [len(g) for g in G]

    
    root = 0
    stack = [root]
    order = []
    lst = [0] * N
    while len(stack) != 0:
        tmp = stack.pop()
        order.append(tmp)
        for to in G[tmp]:
            if to == lst[tmp]:
                continue
            lst[to] = tmp
            stack.append(to)
        
    dp1 = [0] * N
    for v in order[::-1]:
        d = connect[v]
        if v != root and d == 1:
            continue
        x = sum(dp1[c] for c in G[v])
        if v == root:
            dp1[v] = x/connect[v] + 1
        else:
            dp1[v] = x/(connect[v] - 1) + 1

    dp2 = [0] * N
    for v in order:
        d = connect[v]
        n_child = d if v == root else d - 1
        S = dp1[v] * n_child
        if v != root:
            S += dp2[v] + 1
        for c in G[v]:
            if c == lst[v]:
                continue
            if d == 1:
                dp2[c] = 0
            else:
                dp2[c] = (S - dp1[c] - 1)/(d - 1)

    ans = []
    for v in range(N):
        d = connect[v]
        n_child = d if v == root else d - 1
        S = dp1[v] * n_child
        if v != root:
            S += dp2[v] + 1
        ans.append(S/d)

    print (*ans, sep = '\n')

    
    

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
UV = zip(m,m)

if N == 1:
    print(0)
    exit()

graph = [[] for _ in range(N+1)]
for u,v in UV:
    graph[u].append(v)
    graph[v].append(u)

deg = [len(x) for x in graph]

root = 1
parent = [0] * (N+1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

parent,order


dp1 = [0.0]*(N+1)
for v in order[::-1]:
    d = deg[v]
    if v != root and d == 1:
        continue
    x = sum(dp1[c] for c in graph[v])
    if v == root:
        dp1[v] = x/deg[v] + 1
    else:
        dp1[v] = x/(deg[v]-1) + 1


dp2 = [0.0] * (N+1)
for v in order:
    d = deg[v]
    n_child = d if v == root else d-1
    S = dp1[v] * n_child 
    if v != root:
        S += dp2[v]+1
    for c in graph[v]:
        if c == parent[v]:
            continue
        if d == 1:
            dp2[c] = 0
        else:
            dp2[c] = (S-dp1[c]-1) / (d-1)

answer = []
for v in range(1,N+1):
    d = deg[v]
    n_child = d if v == root else d-1
    S = dp1[v] * n_child
    if v != root:
        S += dp2[v]+1
    answer.append(S/d)

print('\n'.join(map(str,answer)))