Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]

tree = {}
for i in ab:
    if tree.get(i[0],-1) == -1:
        tree[i[0]] = [i[1]]
    else:
        tree[i[0]].append(i[1])
    if tree.get(i[1],-1) == -1:
        tree[i[1]] = [i[0]]
    else:
        tree[i[1]].append(i[0])

def maxdegree(tree,n):
    a = 0
    for i in range(1,n+1):
        a = max(a,len(tree[i]))
    return a

print(maxdegree(tree,n))

abmap = [[-1]*(n+1) for i in range(n+1)]

def coloring(self_num,parent,used_color):
    global tree
    global abmap
    num = self_num
    color = 1
    nodes = tree[num]
    for i in nodes:
        if i == parent:
            continue
        else:
            if color == used_color:
                color += 1
            abmap[num][i] = color
            abmap[i][num] = color
            coloring(i,num,color)
            color += 1

coloring(1,0,0)

for i in ab:
    print(abmap[i[0]][i[1]])


Fixed submission:
from collections import deque

N = int(input())
edges = [[] for _ in range(N)]
edge_index = {}
ab = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(N-1)]
for i, (a, b) in enumerate(ab):
    edges[a].append(b)
    edges[b].append(a)
    edge_index[(a, b)] = edge_index[(b, a)] = i
# print(edges)

max_degree = 0
root = 0
for i in range(N):
    if len(edges[i]) > max_degree:
        max_degree = len(edges[i])
        root = i

color = [-1]*(N-1)
visited = [False]*N
visited[root] = True

stack = deque()  # (vertex, color_index)
for i, to in enumerate(edges[root]):
    color[edge_index[(root, to)]] = i+1
    visited[to] = True
    stack.append((to, i+1))

while stack:
    v, v_col = stack.pop()
    color_condidate = 0
    for to in edges[v]:
        assert color_condidate <= max_degree, "hello"
        if visited[to]:
            continue
        visited[to] = True
        color_condidate += 1
        if color_condidate == v_col:
            color_condidate += 1
        idx = edge_index[(v, to)]
        color[idx] = color_condidate
        stack.append((to, color_condidate))

print(max_degree)
print(*color, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]

tree = {}
for i in ab:
    if tree.get(i[0],-1) == -1:
        tree[i[0]] = [i[1]]
    else:
        tree[i[0]].append(i[1])
    if tree.get(i[1],-1) == -1:
        tree[i[1]] = [i[0]]
    else:
        tree[i[1]].append(i[0])

def maxdegree(tree,n):
    a = 0
    for i in range(1,n+1):
        a = max(a,len(tree[i]))
    return a

print(maxdegree(tree,n))

abmap = [[-1]*(n+1) for i in range(n+1)]

def coloring(self_num,parent,used_color):
    global tree
    global abmap
    num = self_num
    color = 1
    nodes = tree[num]
    for i in nodes:
        if i == parent:
            continue
        else:
            if color == used_color:
                color += 1
            abmap[num][i] = color
            abmap[i][num] = color
            coloring(i,num,color)
            color += 1

coloring(1,0,0)

for i in ab:
    print(abmap[i[0]][i[1]])


Fixed submission:
from collections import deque

N = int(input())
edges = [[] for _ in range(N)]
edge_index = {}
ab = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(N-1)]
for i, (a, b) in enumerate(ab):
    edges[a].append(b)
    edges[b].append(a)
    edge_index[(a, b)] = edge_index[(b, a)] = i
# print(edges)

max_degree = 0
root = 0
for i in range(N):
    if len(edges[i]) > max_degree:
        max_degree = len(edges[i])
        root = i

color = [-1]*(N-1)
visited = [False]*N
visited[root] = True

stack = deque()  # (vertex, color_index)
for i, to in enumerate(edges[root]):
    color[edge_index[(root, to)]] = i+1
    visited[to] = True
    stack.append((to, i+1))

while stack:
    v, v_col = stack.pop()
    color_condidate = 0
    for to in edges[v]:
        assert color_condidate <= max_degree, "hello"
        if visited[to]:
            continue
        visited[to] = True
        color_condidate += 1
        if color_condidate == v_col:
            color_condidate += 1
        idx = edge_index[(v, to)]
        color[idx] = color_condidate
        stack.append((to, color_condidate))

print(max_degree)
print(*color, sep='\n')