Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0
keys = []

def get_key(n1, n2):
  if n1 > n2:
    return str(n2) + '-' + str(n1)
  return str(n1) + '-' + str(n2)

for _ in range(n - 1):
  a, b = map(int, input().split())
  keys.append(get_key(a - 1, b - 1))
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [root]
checked = set()
result = {}

min_colors = [set(range(color_num)) for i in range(n)]

while len(queue) > 0:
  node = queue.pop(0)
  # print(node + 1)
  # ks = list(range(color_num))
  # print(node)
  # print(ks)
  # for node2 in G[node]:
  #   key = get_key(node, node2)
  #   if key in result:
  #     ks.pop(result[key])
  for node2 in G[node]:
    if node2 not in checked:
      key = get_key(node, node2)
      # result[key] = ks.pop(0)
      result[key] = min_colors[node].pop()
      min_colors[node2].discard(result[key])
      queue.append(node2)
  checked.add(node)

for k in keys:
  print(result[k] + 1)


Fixed submission:
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0

for i in range(n - 1):
  a, b = map(int, input().split())
  G[a - 1].append((b - 1, i))
  G[b - 1].append((a - 1, i))
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [(root, -1)]
checked = set()
answer = [-1] * (n - 1)

while len(queue) > 0:
  node, c = queue.pop(0)
  color = 1
  for node2, index in G[node]:
    if node2 not in checked:
      if color == c:
        color = c + 1
      answer[index] = color
      color += 1
      queue.append((node2, answer[index]))
  checked.add(node)
print('\n'.join(map(str, answer)))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0
keys = []

def get_key(n1, n2):
  if n1 > n2:
    return str(n2) + '-' + str(n1)
  return str(n1) + '-' + str(n2)

for _ in range(n - 1):
  a, b = map(int, input().split())
  keys.append(get_key(a - 1, b - 1))
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [root]
checked = set()
result = {}

while len(queue) > 0:
  node = queue.pop(0)
  # print(node + 1)
  ks = list(range(color_num))
  for node2 in G[node]:
    key = get_key(node, node2)
    if key in result:
      ks.pop(result[key])
  for node2 in G[node]:
    if node2 not in checked:
      key = get_key(node, node2)
      result[key] = ks.pop(0)
      queue.append(node2)
  checked.add(node)

for k in keys:
  print(result[k] + 1)


Fixed submission:
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0

for i in range(n - 1):
  a, b = map(int, input().split())
  G[a - 1].append((b - 1, i))
  G[b - 1].append((a - 1, i))
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [(root, -1)]
checked = set()
answer = [-1] * (n - 1)

while len(queue) > 0:
  node, c = queue.pop(0)
  color = 1
  for node2, index in G[node]:
    if node2 not in checked:
      if color == c:
        color = c + 1
      answer[index] = color
      color += 1
      queue.append((node2, answer[index]))
  checked.add(node)
print('\n'.join(map(str, answer)))

Buggy python submission (verdict: Runtime Error):
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0
keys = []

def get_key(n1, n2):
  if n1 > n2:
    return str(n2) + '-' + str(n1)
  return str(n1) + '-' + str(n2)

for _ in range(n - 1):
  a, b = map(int, input().split())
  keys.append(get_key(a - 1, b - 1))
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  if len(G[a - 1]) > color_num:
    root = a - 1
    color_num = len(G[a - 1])
  elif len(G[b - 1]) > color_num:
    root = b - 1
    color_num = len(G[b - 1])

# print(root) # 最も多い色を持つノードの番号
print(color_num) # 必要な最小色数
queue = [root]
checked = set()
result = {}

while len(queue) > 0:
  node = queue.pop(0)
  # print(node + 1)
  ks = list(range(color_num))
  for node2 in G[node]:
    key = get_key(node, node2)
    if key in result:
      ks.pop(result[key])
    if node2 not in checked:
      result[key] = ks.pop(0)
      queue.append(node2)
  checked.add(node)

for k in keys:
  print(result[k] + 1)


Fixed submission:
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0

for i in range(n - 1):
  a, b = map(int, input().split())
  G[a - 1].append((b - 1, i))
  G[b - 1].append((a - 1, i))
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [(root, -1)]
checked = set()
answer = [-1] * (n - 1)

while len(queue) > 0:
  node, c = queue.pop(0)
  color = 1
  for node2, index in G[node]:
    if node2 not in checked:
      if color == c:
        color = c + 1
      answer[index] = color
      color += 1
      queue.append((node2, answer[index]))
  checked.add(node)
print('\n'.join(map(str, answer)))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
G = [[] for i in range(n)]
nodes = {}
root = 0
color_num = 0
keys = []

def get_key(n1, n2):
  if n1 > n2:
    return str(n2) + '-' + str(n1)
  return str(n1) + '-' + str(n2)

for _ in range(n - 1):
  a, b = map(int, input().split())
  keys.append(get_key(a - 1, b - 1))
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
  if a in nodes:
    nodes[a] += 1
  else:
    nodes[a] = 1
  if b in nodes:
    nodes[b] += 1
  else:
    nodes[b] = 1
# ex. {1: 1, 2: 4, 3: 1, 4: 2, 5: 2, 6: 2, 7: 1, 8: 1}
# print(nodes)
# print(G)
for i, g in enumerate(G):
  if color_num < len(g):
    root = i
    color_num = len(g)

# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num) # 必要な最小色数
queue = [root]
checked = set()
result = {}

while len(queue) > 0:
  node = queue.pop(0)
  # print(node + 1)
  ks = list(range(color_num))
  for node2 in G[node]:
    key = get_key(node, node2)
    if key in result:
      ks.pop(result[key])
  for node2 in G[node]:
    if node2 not in checked:
      key = get_key(node, node2)
      result[key] = ks.pop(0)
      queue.append(node2)
  checked.add(node)

for k in keys:
  print(result[k] + 1)


Fixed submission:
n = int(input())
G = [[] for i in range(n)]
root = 0
color_num = 0

for i in range(n - 1):
  a, b = map(int, input().split())
  G[a - 1].append((b - 1, i))
  G[b - 1].append((a - 1, i))
  if color_num < len(G[a - 1]):
    root = a - 1
    color_num = len(G[a - 1])
  elif color_num < len(G[b - 1]):
    root = b - 1
    color_num = len(G[b - 1])

# print(G)
# [[1], [0, 2, 3, 4], [1], [1, 6], [1, 5], [4, 7], [3], [5]]
# print(root) # 最も多い色を持つノードの番号
print(color_num)  # 必要な最小色数
queue = [(root, -1)]
checked = set()
answer = [-1] * (n - 1)

while len(queue) > 0:
  node, c = queue.pop(0)
  color = 1
  for node2, index in G[node]:
    if node2 not in checked:
      if color == c:
        color = c + 1
      answer[index] = color
      color += 1
      queue.append((node2, answer[index]))
  checked.add(node)
print('\n'.join(map(str, answer)))