Buggy python submission (verdict: Runtime Error):
N = int(input())
graph = [[] for _ in range(N+1)]
AB = []

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

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)

color = [-1] * (N+1)
K = -1

for x in order:
    temp = color[x]
    c = 1
    for y in graph[x]:
        if y == parent[x]:
            continue
        if c == temp:
            c += 1
        color[y] = c
        K = max(K, c)
        c += 1

ans = []

for a, b in AB:
    if parent[a] == b:
        ans.append(color[a])
    else:
        ans.append(color[b])

print(K)
for i in ans:
  print(ans)

Fixed submission:
N = int(input())
graph = [[] for _ in range(N+1)]
AB = []

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

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)

color = [-1] * (N+1)
K = -1
for x in order:
    ng = color[x]
    c = 1
    for y in graph[x]:
        if y == parent[x]:
            continue
        if c == ng:
            c += 1
        K = max(c, K)
        color[y] = c
        c += 1

ans = []

for a, b in AB:
    if parent[a] == b:
        ans.append(color[a])
    else:
        ans.append(color[b])

print(K)
for i in ans:
  print(i)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N = int(input())
P = [[] for _ in range(N)]
C = [set([]) for _ in range(N)]
T = set([])
D = defaultdict(int)

for _ in range(N-1):
  a, b = map(lambda x: int(x)-1, input().split())
  P[a].append(b)
  #P[b].append(a)
  D[(a, b)]

def bfs(s):
    d = [-1] * N
    d[s] = 0
    p_old = [s]
    M = 0
    while p_old:
      p_new = []
      for i in p_old:
        for j in P[i]:
          temp = T - C[i]
          if temp:
            v = temp.pop()
            D[(i, j)] = v
            C[i].add(v)
            C[j].add(v)
          else:
            M += 1
            T.add(M)
            D[(i, j)] = M
            C[i].add(M)
            C[j].add(M)
          d[j] = 0
          p_new.append(j)
      p_old = p_new
    return M

print(bfs(0))

for x in D.values():
  print(x)

Fixed submission:
N = int(input())
graph = [[] for _ in range(N+1)]
AB = []

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

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)

color = [-1] * (N+1)
K = -1
for x in order:
    ng = color[x]
    c = 1
    for y in graph[x]:
        if y == parent[x]:
            continue
        if c == ng:
            c += 1
        K = max(c, K)
        color[y] = c
        c += 1

ans = []

for a, b in AB:
    if parent[a] == b:
        ans.append(color[a])
    else:
        ans.append(color[b])

print(K)
for i in ans:
  print(i)