Problem p03754 (python)

Fixed submission:
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]
 
    #根を0として木を構築
    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')
 
    # print (dp1)
    # print (dp2)
 
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006
#https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_d?lang=ja

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
4
1 2
2 3
2 4
'''[1:]

input2 ='''
4
1 2
2 4
4 3
'''[1:]



def file_intake():
    try:
        f = open('in32.txt')
    except:
        return None
    nl ='''
    '''
    ret = ''.join( f.readlines()  )
    f.close()
    return ret

input1a = file_intake()


input1bk ='''
4
0 1 2
1 2 1
1 3 3
'''[1:]



#input1 = input2

output1_2='''
2.0
1.0
2.0
2.0

3.0
1.5
3.0
1.5
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except: 
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))



from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])


def reroot(root0,n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):
    
    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v, noe_vwise
        
        ret_return = e_init
        ret_return = None
        stack = [(v, v_par, 0, e_init)]
        while stack:
            (v, v_par, i, ret_onloop ) = stack.pop()
            
            if ( i == len(es_vwise[v]) ):
                ret_return = ret_onloop
                continue
            
            e_local = es_vwise[v][i]
            if ( e_local[0] == v_par):
                print('v_par, not-to-see?')
                vpar_v[v] = i
                stack.append((v, v_par, i+1, ret_onloop))
                continue
            
            if ret_return is None:
                stack.append( (v, v_par, i, ret_onloop) )
                stack.append( (e_local[0], v, 0, e_init) )
                continue
            
            elif ret_return is not None:
                dp_es_vwise[v][i] = lift(ret_return, e_local[1])
                ret_return = None
                ret_onloop = merge(ret_onloop, dp_es_vwise[v][i])
                
                stack.append((v, v_par, i+1, ret_onloop ) )
        
        return ret_return

    def dfs2(v, t):
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init, noe_vwise
        nonlocal pe_vwise
        
        stack = [(v,t,None,None,None,-INF)]
        
        while (stack):
            v,t, dpl, dpr, i, imax = stack.pop()
            
            if (vpar_v[v] is not None):
                dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
                
            if ( i == None):                 
                l = len(es_vwise[v])
                dpl = [e_init for i in range(l+1) ]
                dpr = [e_init for i in range(l+1) ]
                
                if (pe_vwise[v] is not None):
                    v0, v1, w = pe_vwise[v]            
                    dpr[l] = lift(t,w)                    
                
                
                for i in range(l):
                    dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
                for i in range(l-1,-1,-1):
                    dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
                    #l: 0->1, l-1->l
                    #r: l->l-1, 1->0
                stack.append((v, t, dpl, dpr, 0, l-1))
                ans[v] = dpr[0]
            else:
                if (imax < i):
                    continue
                if ( i == vpar_v[v]):
                    stack.append((v, t, dpl, dpr, i+1, imax ))
                    continue
                u = es_vwise[v][i][0]
                stack.append((v, t, dpl, dpr, i+1, imax  ))
                stack.append((u, merge(dpl[i], dpr[i+1]), None, None, None, -INF ))
                
    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)

    
    def merge(a,b):
        # print("merge:",a,b)
        return ((a[0] + b[0], a[1]+b[1]) )

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]
    
    ans = [Edge(0,0) for i in range(nov)]
    

    dfs1(root0,None)   #print(ans)

    
    dfs2(root0,None)
    
    for i in range(n):
        #print(f"i:{i}",ans[i])
        pass
    
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))
        #print((ans[i][0] , ans[i][1]))
    
    return ans


def virtual_undirected(n,st_list): #　undirected, quasi-directed
    #print('arg:',n,st_list)
    edst_vwise0 =  edge_dst_s_vwise0 =  [ [] for i in range(n)]
    edst_vwise1 =  [ [] for i in range(n)]
    seen = [False for _ in range(n)]
    vmin = INF
    for st in st_list:
        s,t = st
        edst_vwise0[s].append(t)
        edst_vwise0[t].append(s)
        vmin = min(vmin,s,t)
    
    seen[vmin] = True
    stack = [vmin]
    
    while(stack):
        vcur = stack.pop()
        for vnext in edst_vwise0[vcur]:
            if (seen[vnext]):
                continue
            
            stack.append(vnext)
            edst_vwise1[vcur].append(vnext)
            seen[vnext] = True
    
    #print('edst_vwise',edst_vwise0,edst_vwise1)
    st_list1 = [ ]
    for s in range(n):
        for t in edst_vwise1[s]:
            #print('s,t',s,t)
            st_list1.append((s,t))
    return vmin,st_list1
    


def main():
    
    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    vmin, st_list1 = virtual_undirected(n,st_list)
    
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)
    reroot(vmin,n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()

Buggy submission (verdict: Runtime Error):
#recursion-version

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006

import sys
sys.setrecursionlimit(1000000)

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
4
1 2
2 3
2 4
'''[1:]

input2 ='''
7
1 2
1 3
2 4
2 5
3 6
3 7
'''[1:]

input1bk ='''
4
1 2
2 3
2 4
'''[1:]



#input1 = input2

output1_2='''
5 7
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except:
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    #noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))

#print(st_list)

from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])
def Edge(x,y):
    return (x,y)

def reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):

    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v
        stack = [(v,v_par)]
        while stack:
            (v,v_par) = stack.pop()
        
        #print('es_vwise[v]',v,es_vwise[v])
        ret = e_init
        #ret = Edge(-INF,-INF)

        
        for i in range(len(es_vwise[v]) ):
            e_local = es_vwise[v][i]
            if (e_local[0] == v_par):
                print('this-part should not be printed')
                vpar_v[v] = i
                continue
            
            dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1]  )
            #dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1] )
            ret = merge(ret, dp_es_vwise[v][i] )
        return ret

    def dfs2(v, t):
        # bundlable_parental_edge = t
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init
        nonlocal pe_vwise
        
        if (vpar_v[v] is not None):
            pass
            #lift(parental_edge, gparental_edge)
            #dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
        
        l = len(es_vwise[v])
        dpl = [e_init for i in range(l+1) ]
        dpr = [e_init for i in range(l+1) ]
     
        if (pe_vwise[v] is not None):
            v0, v1, w = pe_vwise[v]            
            dpr[l] = lift(t,w)    
    
        for i in range(l):
            dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
        for i in range(l-1,-1,-1):
            dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
            #l: 0->1, l-1->l
            #r: l->l-1, 1->0

        ans[v] = dpr[0]

        for i in range(l):
            if (i == vpar_v[v]):
                print('continue, l',0)
                continue
            u = es_vwise[v][i][0]
                
            dfs2(u, merge(dpl[i], dpr[i+1]) )

    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)

    
    def merge(a,b):
        # print("merge:",a,b)
        return (a[0] + b[0], a[1]+b[1])

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]

    for i in range(len(es_vwise)):
        for e in es_vwise[i]:
            vpar_v0[e[0]] = i
    
    ans = [Edge(0,-INF) for i in range(nov)]    
    dfs1(0,None)   
    #0 must be root?
    #print('1->2',dp_es_vwise)
    dfs2(0,None)
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))

    return ans



def virtual_undirected(n,st_list): #　undirected, quasi-directed

    edst_vwise0 =  edge_dst_s_vwise0 =  [ [] for i in range(n)]
    edst_vwise1 =  [ [] for i in range(n)]
    seen = [False for _ in range(n)]
    vmin = INF
    for st in st_list:
        s,t = st
        edst_vwise0[s].append(t)
        edst_vwise0[t].append(s)
        vmin = min(vmin,s,t)
    
    seen[vmin] = True
    stack = [vmin]
    
    while(stack):
        vcur = stack.pop()
        for vnext in edst_vwise0[vcur]:
            if (seen[vnext]):
                continue
            
            stack.append(vnext)
            edst_vwise1[vcur].append(vnext)
            seen[vnext] = True
    
    #print('edst_vwise',edst_vwise0,edst_vwise1)
    st_list1 = [ ]
    for s in range(n):
        for t in edst_vwise1[s]:
            st_list1.append((s,t))
    return st_list1



def main():

    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    
    st_list1 = virtual_undirected(n,st_list)
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)

    reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()

Buggy submission (verdict: Runtime Error):
#recursion-version

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006

import sys
sys.setrecursionlimit(1000000)

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
12
1 2
2 3
2 4
4 5
5 6
5 7
6 8
8 9
2 10
10 11
11 12
'''[1:]

input2 ='''
7
1 2
1 3
2 4
2 5
3 6
3 7
'''[1:]

input1bk ='''
4
1 2
2 3
2 4
'''[1:]



#input1 = input2

output1_2='''
5 7
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except:
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    #noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))

#print(st_list)

from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])
def Edge(x,y):
    return (x,y)

def reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):

    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v
        stack = [(v,v_par)]
        while stack:
            (v,v_par) = stack.pop()
        
        #print('es_vwise[v]',v,es_vwise[v])
        ret = e
        #ret = Edge(-INF,-INF)
        ret = (0, 0)
        
        for i in range(len(es_vwise[v]) ):
            e_local = es_vwise[v][i]
            if (e_local[0] == v_par):
                print('this-part should not be printed')
                vpar_v[v] = i
                continue
            
            dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1]  )
            #dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1] )
            ret = merge(ret, dp_es_vwise[v][i] )
        return ret

    def dfs2(v, t):
        # bundlable_parental_edge = t
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init
        nonlocal pe_vwise
        
        if (vpar_v[v] is not None):
            pass
            #lift(parental_edge, gparental_edge)
            #dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
        
        l = len(es_vwise[v])
        dpl = [e_init for i in range(l+1) ]
        dpr = [e_init for i in range(l+1) ]
     
        if (pe_vwise[v] is not None):
            v0, v1, w = pe_vwise[v]            
            dpr[l] = lift(t,w)    
    
        for i in range(l):
            dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
        for i in range(l-1,-1,-1):
            dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
            #l: 0->1, l-1->l
            #r: l->l-1, 1->0

        ans[v] = dpr[0]

        for i in range(l):
            if (i == vpar_v[v]):
                print('continue, l',0)
                continue
            u = es_vwise[v][i][0]
                
            dfs2(u, merge(dpl[i], dpr[i+1]) )

    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)

    
    def merge(a,b):
        # print("merge:",a,b)
        return (a[0] + b[0], a[1]+b[1])

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]

    for i in range(len(es_vwise)):
        for e in es_vwise[i]:
            vpar_v0[e[0]] = i
    
    ans = [Edge(0,-INF) for i in range(nov)]    
    dfs1(0,None)   
    #0 must be root?
    dfs2(0,None)
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))

    return ans

class reroot_init:
    def __init__(self,n,noe_vwise):
        self.n = n
        self.es_vwise = [ [] for i in range(n)]
        self.dp_es_vwise = [ [] for i in range(n)]
        self.pe_vwise = [ None for i in range(n)]
        parental_edge_vwise = self.pe_vwise
    
    def movemain(self):
        reroot(n,self.es_vwise, self.dp_es_vwise, (-INF,-INF), noe_vwise, {})

def main():
    rri = reroot_init(n, noe_vwise)
    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)

    reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()

Buggy submission (verdict: Runtime Error):
#recursion-version

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006

import sys
sys.setrecursionlimit(10000)

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
4
1 2
2 3
2 4
'''[1:]

input2 ='''
7
1 2
1 3
2 4
2 5
3 6
3 7
'''[1:]

input1bk ='''
4
0 1 2
1 2 1
1 3 3
'''[1:]



#input1 = input2

output1_2='''
5 7
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except:
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    #noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))

#print(st_list)

from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])
def Edge(x,y):
    return (x,y)

def reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):

    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v
        stack = [(v,v_par)]
        while stack:
            (v,v_par) = stack.pop()
        
        #print('es_vwise[v]',v,es_vwise[v])
        ret = e
        #ret = Edge(-INF,-INF)
        ret = (0, 0)
        
        for i in range(len(es_vwise[v]) ):
            e_local = es_vwise[v][i]
            if (e_local[0] == v_par):
                print('this-part should not be printed')
                vpar_v[v] = i
                continue
            
            dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1]  )
            #dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1] )
            ret = merge(ret, dp_es_vwise[v][i] )
        return ret

    def dfs2(v, t):
        # bundlable_parental_edge = t
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init
        nonlocal pe_vwise
        
        if (vpar_v[v] is not None):
            pass
            #lift(parental_edge, gparental_edge)
            #dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
        
        l = len(es_vwise[v])
        dpl = [e_init for i in range(l+1) ]
        dpr = [e_init for i in range(l+1) ]
     
        if (pe_vwise[v] is not None):
            v0, v1, w = pe_vwise[v]            
            dpr[l] = lift(t,w)    
    
        for i in range(l):
            dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
        for i in range(l-1,-1,-1):
            dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
            #l: 0->1, l-1->l
            #r: l->l-1, 1->0

        ans[v] = dpr[0]

        for i in range(l):
            if (i == vpar_v[v]):
                print('continue, l',0)
                continue
            u = es_vwise[v][i][0]
                
            dfs2(u, merge(dpl[i], dpr[i+1]) )

    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)

    
    def merge(a,b):
        # print("merge:",a,b)
        return (a[0] + b[0], a[1]+b[1])

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]

    for i in range(len(es_vwise)):
        for e in es_vwise[i]:
            vpar_v0[e[0]] = i
    
    ans = [Edge(0,-INF) for i in range(nov)]    
    dfs1(0,None)   
    #0 must be root?
    dfs2(0,None)
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))

    return ans

class reroot_init:
    def __init__(self,n,noe_vwise):
        self.n = n
        self.es_vwise = [ [] for i in range(n)]
        self.dp_es_vwise = [ [] for i in range(n)]
        self.pe_vwise = [ None for i in range(n)]
        parental_edge_vwise = self.pe_vwise
    
    def movemain(self):
        reroot(n,self.es_vwise, self.dp_es_vwise, (-INF,-INF), noe_vwise, {})

def main():
    rri = reroot_init(n, noe_vwise)
    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)

    reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()

Buggy submission (verdict: Runtime Error):
#recursion-version

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006

import sys
sys.setrecursionlimit(10000)

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
4
1 2
2 3
2 4
'''[1:]

input2 ='''
7
1 2
1 3
2 4
2 5
3 6
3 7
'''[1:]

input1bk ='''
4
0 1 2
1 2 1
1 3 3
'''[1:]



#input1 = input2

output1_2='''
5 7
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except:
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    #noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))

#print(st_list)

from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])
def Edge(x,y):
    return (x,y)

def reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):

    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v
        stack = [(v,v_par)]
        while stack:
            (v,v_par) = stack.pop()
        
        #print('es_vwise[v]',v,es_vwise[v])
        ret = e
        #ret = Edge(-INF,-INF)
        ret = (0, 0)
        
        for i in range(len(es_vwise[v]) ):
            e_local = es_vwise[v][i]
            if (e_local[0] == v_par):
                print('this-part should not be printed')
                vpar_v[v] = i
                continue
            
            dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1]  )
            #dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1] )
            ret = merge(ret, dp_es_vwise[v][i] )
        return ret

    def dfs2(v, t):
        # bundlable_parental_edge = t
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init
        nonlocal pe_vwise
        
        if (vpar_v[v] is not None):
            pass
            #lift(parental_edge, gparental_edge)
            #dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
        
        l = len(es_vwise[v])
        dpl = [e_init for i in range(l+1) ]
        dpr = [e_init for i in range(l+1) ]
     
        if (pe_vwise[v] is not None):
            v0, v1, w = pe_vwise[v]            
            dpr[l] = lift(t,w)    
    
        for i in range(l):
            dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
        for i in range(l-1,-1,-1):
            dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
            #l: 0->1, l-1->l
            #r: l->l-1, 1->0

        ans[v] = dpr[0]

        for i in range(l):
            if (i == vpar_v[v]):
                print('continue, l',0)
                continue
            u = es_vwise[v][i][0]
                
            dfs2(u, merge(dpl[i], dpr[i+1]) )

    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)

    
    def merge(a,b):
        # print("merge:",a,b)
        return (a[0] + b[0], a[1]+b[1])

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]

    for i in range(len(es_vwise)):
        for e in es_vwise[i]:
            vpar_v0[e[0]] = i
    
    ans = [Edge(0,-INF) for i in range(nov)]    
    dfs1(0,None)   
    #0 must be root?
    dfs2(0,None)
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))

    return ans

class reroot_init:
    def __init__(self,n,noe_vwise):
        self.n = n
        self.es_vwise = [ [] for i in range(n)]
        self.dp_es_vwise = [ [] for i in range(n)]
        self.pe_vwise = [ None for i in range(n)]
        parental_edge_vwise = self.pe_vwise
    
    def movemain(self):
        reroot(n,self.es_vwise, self.dp_es_vwise, (-INF,-INF), noe_vwise, {})

def main():
    rri = reroot_init(n, noe_vwise)
    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)

    reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()

Buggy submission (verdict: Runtime Error):
#recursion-version

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A&lang=jp
#http://penguinshunya.hatenablog.com/entry/2019/07/09/003006

import sys
sys.setrecursionlimit(10000)

inpPh = 0
debug = True
#debug = False
if not debug:
    inpPh = 0

input1 ='''
4
1 2
2 3
2 4
'''[1:]

input2 ='''
4
1 2
2 4
4 3
'''[1:]

input1bk ='''
4
0 1 2
1 2 1
1 3 3
'''[1:]



#input1 = input2

output1_2='''
5 7
'''

inputXs = [input1, input2,None]

if debug:
    try:
        inputXs.append(inputA0)
    except:
        pass
    else:
        pass

inploc = inpPh - 1 #0,1,2

if inpPh > 0:
    #print('selected_input:\n'+inputXs[inploc])
    #print('')
    pass

inpX_lst = []
if debug and inpPh > 0:
    inpX_lst = inputXs[inploc].splitlines()


if inpPh ==0:
    n = int(input())
else:
    n = int(inpX_lst.pop(0) )

st_list = []
noe_vwise = [0 for _ in range(n)]

for i in range(n-1):
    if inpPh ==0:
        si,ti = tuple(map(lambda x: int(x), input().split() ) )
    else:
        si,ti = tuple(map(lambda x: int(x), inpX_lst.pop(0).split() ) )
    
    noe_vwise[si-1] += 1
    #noe_vwise[ti-1] += 1
    st_list.append((si-1,ti-1))

#print(st_list)

from collections import namedtuple
INF = float('inf')

Edge = namedtuple('Edge', ['to', 'value'])
def Edge(x,y):
    return (x,y)

def reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise):

    def dfs1(v,v_par):
        nonlocal es_vwise, dp_es_vwise, e_init, vpar_v, noe_vwise
        stack = [(v,v_par)]
        while stack:
            (v,v_par) = stack.pop()
        
        #print('es_vwise[v]',v,es_vwise[v])
        ret = e
        #ret = Edge(-INF,-INF)
        ret = (0, 0)
        
        for i in range(len(es_vwise[v]) ):
            e_local = es_vwise[v][i]
            if (e_local[0] == v_par):
                print('this-part should not be printed')
                vpar_v[v] = i
                continue
            
            dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1]  )
            #dp_es_vwise[v][i] = lift(dfs1(e_local[0], v), e_local[1] )
            ret = merge(ret, dp_es_vwise[v][i] )
        return ret

    def dfs2(v, t):
        # bundlable_parental_edge = t
        nonlocal ans, es_vwise, dp_es_vwise, vpar_v, e_init, noe_vwise
        nonlocal pe_vwise
        
        if (vpar_v[v] is not None):
            pass
            #lift(parental_edge, gparental_edge)
            #dp_es_vwise[v][vpar_v[v]] = lift(t,es_vwise[v][vpar_v[v]][1] )
        
        l = len(es_vwise[v])
        dpl = [e_init for i in range(l+1) ]
        dpr = [e_init for i in range(l+1) ]
     
        if (pe_vwise[v] is not None):
            v0, v1, w = pe_vwise[v]            
            dpr[l] = lift(t,w)    
    
        for i in range(l):
            dpl[i+1] = merge(dpl[i], dp_es_vwise[v][i])
        for i in range(l-1,-1,-1):
            dpr[i] = merge(dpr[i+1], dp_es_vwise[v][i])
            #l: 0->1, l-1->l
            #r: l->l-1, 1->0

        ans[v] = dpr[0]

        for i in range(l):
            if (i == vpar_v[v]):
                print('continue, l',0)
                continue
            u = es_vwise[v][i][0]
                
            dfs2(u, merge(dpl[i], dpr[i+1]) )

    def lift(a,w):
        if a[1] == 0:
            return (w,1)
        
        return ( (a[0] / a[1]) + w, 1)
        #w is 1, though...
        #return Edge(max(0,a[0]) +w, -INF)
    
    def merge(a,b):
        # print("merge:",a,b)
        return (a[0] + b[0], a[1]+b[1])

    nov = n
    vpar_v = [None for i in range(nov)]
    vpar_v0 = [None for i in range(nov)]
    
    for i in range(len(es_vwise)):
        for e in es_vwise[i]:
            vpar_v0[e[0]] = i
    
    ans = [Edge(0,-INF) for i in range(nov)]
    
    dfs1(0,None)   
    #0 must be root?
    dfs2(0,None)
    ret = 0
    
    for i in range(len(ans)):
        #print(f'i:{i} ans:{ans[i]}')
        print(float(ans[i][0] / ans[i][1]))

    return ans


class reroot_init:
    def __init__(self,n,noe_vwise):
        self.n = n
        self.es_vwise = [ [] for i in range(n)]
        self.dp_es_vwise = [ [] for i in range(n)]
        self.pe_vwise = [ None for i in range(n)]
        
        parental_edge_vwise = self.pe_vwise
    
    def movemain(self):
        reroot(n,self.es_vwise, self.dp_es_vwise, (-INF,-INF), noe_vwise, {})

def main():
    rri = reroot_init(n, noe_vwise)
    es_vwise = [ [] for i in range(n)]
    dp_es_vwise = [ [] for i in range(n)]
    pe_vwise = [ None for i in range(n)]    
    
    edges_vwise = es_vwise ; parental_edge_vwise = pe_vwise

    def v_add_edge(v,u,w):
        es_vwise[v].append((u,w))
        dp_es_vwise[v].append((0, -INF))
    
    for st in st_list:
        s,t = st
        #print(stw)
        v_add_edge(s,t,1)
        pe_vwise[t] = (s,t,1)
        
    e_init = (0,0)

    reroot(n, es_vwise, dp_es_vwise, e_init, noe_vwise, pe_vwise)

main()