Problem:
Given is a tree G with N vertices. The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.  Consider painting the edges in G with some number of colors. We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.  Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.

Incorrect submission (verdict: Wrong Answer):
N = int(input())
sides = []  
side_colors = [0] * (N - 1)  に使う色
point_sides = [0] * N  
point_colors = [set() for i in range(N)]

for i in range(N - 1):
    s, e = map(int, input().split())
    sides.append([s - 1, e - 1])
    point_sides[s - 1] += 1
    point_sides[e - 1] += 1

max_color_num = max(point_sides)
point_of_max_side_num = point_sides.index(max_color_num)
color_id = 1
for i in range(N - 1):
    s = sides[i][0]
    e = sides[i][1]
    if s == point_of_max_side_num or e == point_of_max_side_num:
        point_colors[s].add(color_id)
        point_colors[e].add(color_id)
        side_colors[i] = color_id
        color_id += 1

for i in range(N - 1):
    s = sides[i][0]
    e = sides[i][1]
    if s != point_of_max_side_num and e != point_of_max_side_num:
        for j in range(1, max_color_num + 1):
            if (j not in point_colors[s]) and (j not in point_colors[e]):
                point_colors[s].add(j)
                point_colors[e].add(j)
                side_colors[i] = j
                break

print(max_color_num)
for side_color in side_colors:
    print(side_color)


Fixed, accepted submission:
from collections import deque

N = int(input())
vertex_neighbor_vertices = {}  
vertex_colors = {}  
sides = []  
side_color = {}  


def add_neighbors(start, end):
    neighbors_of_s = set()
    if start in vertex_neighbor_vertices:
        neighbors_of_s = vertex_neighbor_vertices[start]
    else:
        vertex_neighbor_vertices[start] = neighbors_of_s
    neighbors_of_s.add(end)


def init_colors(vertex):
    colors = set()
    if vertex not in vertex_colors:
        vertex_colors[vertex] = colors


def get_unused_color(colors_of_sv, colors_of_ev, color_start):
    for i in range(color_start, N + 1):
        if (i not in colors_of_sv) and (i not in colors_of_ev):
            return i


for i in range(N - 1):
    s, e = map(int, input().split())
    sides.append((s, e))
    add_neighbors(s, e)
    add_neighbors(e, s)
    init_colors(s)
    init_colors(e)

max_color_num = 0
max_neighbor_vertex = 0
for k, v in vertex_neighbor_vertices.items():
    if len(v) > max_color_num:
        max_color_num = len(v)
        max_neighbor_vertex = k

print(max_color_num)


visited = set()
q = deque([max_neighbor_vertex])

while q:
    vertex = q.popleft()
    visited.add(vertex)
    
    color_start = 1
    for neighbor in vertex_neighbor_vertices[vertex]:
        if neighbor in visited:
            continue
        q.append(neighbor)
        color = get_unused_color(vertex_colors[vertex], vertex_colors[neighbor], color_start)
        color_start = color + 1
        vertex_colors[vertex].add(color)
        vertex_colors[neighbor].add(color)
        side_color[(vertex, neighbor)] = color

for s, e in sides:
    color = 0
    if (s, e) in side_color:
        color = side_color[(s, e)]
    else:
        color = side_color[(e, s)]
    print(color)