Problem p02850 (python)

Fixed submission:
import sys
from collections import deque

# txt = "100000"
# for i in range(99999):
#     txt += "\n" + str(i) +" " +  str(i+1)
# def i():
#     for l in txt.split("\n"):
#         yield l
# input = i().__next__
def main():
    N = int(input())
    mawari = [[] for _ in range(N)]
    e_num = [0] * N 
    for l in range(N-1):
        i, j = [int(x)-1 for x in input().split()]
        e_num[i] += 1
        e_num[j] += 1
        mawari[i].append((j, l))
        mawari[j].append((i, l))
    look = -1
    for ind, l in enumerate(e_num):
        if l == 1:
            look = ind
            break
    color = [-1]*(N-1)
    c_prev = deque([-1])
    to_visit = deque([look])
    maxc = 0
    while len(to_visit)>=1:
        look = to_visit.pop()
        c = 0
        prevc = c_prev.pop()
        for i,j in mawari[look]:
            if color[j] != -1:
                continue
            if c == prevc:
                c += 1
            color[j] = c
            c_prev.append(c)
            to_visit.append(i)
            maxc = max(maxc, c)
            c += 1
        
    # mc, c = a(N, 0, look, color, mawari, -1)
    print(maxc+1)
    for i in color:
        print(i+1)


# def a(N, maxc, look, color, mawari, prev):
#     nexts = mawari[look]
#     c = 0
#     for i,j in nexts:
#         if c == prev:
#             c += 1
#         color[j] = c
#         maxc = max(maxc, a(N, maxc, i, color, mawari, c)[0])
#         c += 1
#     return maxc, color
    

main()

Buggy submission (verdict: Runtime Error):
def main():
    N = int(input())
    mawari = [[] for _ in range(N)]
    e_num = [0] * N 
    for l in range(N-1):
        i, j = [int(x)-1 for x in input().split()]
        e_num[i] += 1
        e_num[j] += 1
        mawari[i].append((j, l))
        mawari[j].append((i, l))
    look = -1
    for ind, l in enumerate(e_num):
        if l == 1:
            look = ind
            break
    color = [-1]*(N-1)
    mc, c = a(N, 0, look, color, mawari)
    print(mc+1)
    for i in c:
        print(i+1)


def a(N, maxc, look, color, mawari):
    nexts = []
    used = []
    for i,j in mawari[look]:
        if color[j] != -1:
            used.append(color[j])
        else:
            nexts.append((i,j))
    c = 0
    for i,j in nexts:
        while c in used:
            c += 1
        color[j] = c
        maxc = max(c, maxc)
        c += 1
    for i,j in nexts:
        maxc = max(maxc, a(N, maxc, i, color, mawari)[0])
    return maxc, color
    

main()

Buggy submission (verdict: Runtime Error):
def main():
    N = int(input())
    edges = [[-1]*N for _ in range(N)]
    e_num = [0] * N 
    for l in range(N-1):
        i, j = [int(x)-1 for x in input().split()]
        edges[i][j] = edges[j][i] = l
        e_num[i] += 1
        e_num[j] += 1
    look = -1
    for ind, l in enumerate(e_num):
        if l == 1:
            look = ind
            break
    color = [-1]*(N-1)
    mc, c = a(N, 0, look, color, edges)
    print(mc+1)
    for i in c:
        print(i+1)


def a(N, maxc, look, color, edges):
    nexts = []
    used = []
    for i in range(N):
        if edges[look][i] != -1:
            if color[edges[look][i]] != -1:
                used.append(color[edges[look][i]])
            else:
                nexts.append(i)
    c = 0
    for n in nexts:
        while c in used:
            c += 1
        color[edges[look][n]] = c
        maxc = max(c, maxc)
        c += 1
    for n in nexts:
        maxc = max(maxc, a(N, maxc, n, color, edges)[0])
    return maxc, color
    

main()