Buggy python submission (verdict: Runtime Error):
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

def dfs(v = 0, p = 0, p_col = -1):
  # 色は１から順に塗っていく
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == p_col:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      dfs(u, v, col)
      col += 1 

dfs()

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

Fixed submission:
from collections import deque
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

q = deque()
q.append((0, 0, -1))
# 色は１から順に塗っていく
while q:
  v, p, pcol = q.popleft()
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == pcol:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      q.append((u, v, col))
      col += 1 

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for a, b in AB:
  e[a].append(b)
  e[b].append(a)
  
color = {}
for a, b in AB:
  color[(a, b)] = -1
  color[(b, a)] = -1
cidx = max([len(i) for i in e])

for itr, i in enumerate(e):
  cls = set(range(1, cidx + 1))
  cls -= set([color[(itr, k)] for k in i])
  for j in i:
    if color[(itr,j)] == -1 or color[(j,itr)] == -1:
      cl = cls.pop()
      color[(itr, j)] = cl 
      color[(j, itr)] = cl
      
print(cidx)
for i in AB:
  print(color[i])

Fixed submission:
from collections import deque
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

q = deque()
q.append((0, 0, -1))
# 色は１から順に塗っていく
while q:
  v, p, pcol = q.popleft()
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == pcol:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      q.append((u, v, col))
      col += 1 

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for a, b in AB:
  e[a].append(b)
  e[b].append(a)
  
color = {}
for a, b in AB:
  color[(a, b)] = -1
  color[(b, a)] = -1
cidx = max([len(i) for i in e])

for itr, i in enumerate(e):
  cls = set(range(1, cidx + 1))
  cls -= set([color[(itr, k)] for k in i])
  for j in i:
    if color[(itr,j)] == -1 or color[(j,itr)] == -1:
      cl = cls.pop()
      color[(itr, j)] = cl 
      color[(j, itr)] = cl
      
print(cidx)
for i in AB:
  print(color[i])

Fixed submission:
from collections import deque
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

q = deque()
q.append((0, 0, -1))
# 色は１から順に塗っていく
while q:
  v, p, pcol = q.popleft()
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == pcol:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      q.append((u, v, col))
      col += 1 

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