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

n = int(input())
e = [list(map(int, input().split())) for _ in range(n-1)]
c = [0 for _ in range(n-1)]

cmap = [[0 for _ in range(n)] for _ in range(n)]
for l in e:
    cmap[l[0]-1][l[1]-1] = cmap[l[1]-1][l[0]-1]=-1
    
d = deque()
d.append(1)
while(d):
    ed = d.popleft() -1
    clist = list()
    usedc = set()
    for i in range(n):
        if cmap[ed][i]:
            clist.append([i, cmap[ed][i]])
            usedc.add(cmap[ed][i])
    for color in clist:
        if color[1] == -1:
            d.append(color[0]+1)
            cn = 1
            while(1):
                if cn in usedc:
                    cn+=1
                else:
                    usedc.add(cn)
                    break

            cmap[ed][color[0]] = cmap[color[0]][ed] = cn

for i in range(n-1):
    c[i] = cmap[e[i][0]-1][e[i][1]-1]

print(max(c))
for num in c:
    print(num)




Fixed submission:
from collections import deque
n = int(input())

ls = [list() for _ in range(n)]
ls2 = [list() for _ in range(n)]
edge = []
for i in range(n-1):
    x,y = map(int,input().split())
    ls[x-1].append(i)
    ls2[y-1].append(i)
    edge.append([x-1,y-1])

d = deque()
color = [0]*(n-1)
d.append(0)
while len(d):
    c = d.pop()
    cl = []
    for i in ls2[c]:
        if color[i]:
            cl.append(color[i])
    idx = 1
    for i in ls[c]:
        while(1):
            if not idx in cl:
                break
            else:
                idx += 1
        color[i] = idx
        cl.append(idx)
        d.append(edge[i][1])

print(max(color))
for i in color:
    print(i)

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

n = int(input())
e = [list(map(int, input().split())) for _ in range(n-1)]
c = [0 for _ in range(n-1)]

cmap = [[0 for _ in range(n)] for _ in range(n)]
for l in e:
    cmap[l[0]-1][l[1]-1] = cmap[l[1]-1][l[0]-1]=-1
    
d = deque()
d.append(1)
while(d):
    ed = d.pop() -1
    clist = list()
    usedc = set()
    for i in range(n):
        if cmap[ed][i]:
            clist.append([i, cmap[ed][i]])
            usedc.add(cmap[ed][i])
    for color in clist:
        if color[1] == -1:
            d.append(color[0]+1)
            cn = 1
            while(1):
                if cn in usedc:
                    cn+=1
                else:
                    usedc.add(cn)
                    break

            cmap[ed][color[0]] = cmap[color[0]][ed] = cn

for i in range(n-1):
    c[i] = cmap[e[i][0]-1][e[i][1]-1]

print(max(c))
for num in c:
    print(num)

Fixed submission:
from collections import deque
n = int(input())

ls = [list() for _ in range(n)]
ls2 = [list() for _ in range(n)]
edge = []
for i in range(n-1):
    x,y = map(int,input().split())
    ls[x-1].append(i)
    ls2[y-1].append(i)
    edge.append([x-1,y-1])

d = deque()
color = [0]*(n-1)
d.append(0)
while len(d):
    c = d.pop()
    cl = []
    for i in ls2[c]:
        if color[i]:
            cl.append(color[i])
    idx = 1
    for i in ls[c]:
        while(1):
            if not idx in cl:
                break
            else:
                idx += 1
        color[i] = idx
        cl.append(idx)
        d.append(edge[i][1])

print(max(color))
for i in color:
    print(i)