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

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

c = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    c[a].append(b)
    c[b].append(a)

s = 0
for i in range(n):
    if len(c[i]) > s:
        s = i

e = [0] * n
l = [[0] * n for _ in range(n)]
q = deque()
q.append((s, 0))
while q:
    f, v = q.pop()
    e[f] = 1
    j = 1
    for i in c[f]:
        if e[i] == 0:
            if j == v:
                j += 1
            q.appendleft((i, j))
            l[min(f, i)][max(f, i)] = j
            j += 1

ans = []
for a, b in ab:
    a, b = a-1, b-1
    ans.append(l[a][b])

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

Fixed submission:
def main():
    import sys
    readline = sys.stdin.buffer.readline

    from collections import deque

    n = int(readline())
    ab = [list(map(int, readline().split())) for _ in range(n-1)]

    l = [[] for _ in range(n+1)]
    for a, b in ab:
        l[a].append(b)
        l[b].append(a)

    parents = [-1] * (n+1)
    order = []
    q = deque()
    q.append(1)
    while q:
        c = q.pop()
        order.append(c)
        for i in l[c]:
            if i == parents[c]:
                continue
            parents[i] = c
            q.append(i)
    
    color = [-1] * (n+1)
    for i in order:
        ng = color[i]
        c = 1
        for j in l[i]:
            if j == parents[i]:
                continue
            if c == ng:
                c += 1
            color[j] = c
            c += 1
    
    ans = []
    for a, b in ab:
        if a == parents[b]:
            ans.append(color[b])
        else:
            ans.append(color[a])

    print(max(ans))
    print('\n'.join(list(map(str, ans))))

if __name__ == '__main__':
    main()

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

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

c = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    c[a].append(b)
    c[b].append(a)

s = 0
for i in range(n):
    if len(c[i]) > s:
        s = i

e = [0] * n
l = [[0] * n for _ in range(n)]
q = deque()
q.append((s, 0))
while q:
    f, v = q.pop()
    e[f] = 1
    j = 1
    for i in c[f]:
        if e[i] == 0:
            if j == v:
                j += 1
            q.appendleft((i, j))
            l[min(f, i)][max(f, i)] = j
            j += 1

ans = []
for a, b in ab:
    a, b = a-1, b-1
    ans.append(l[a][b])

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

Fixed submission:
def main():
    import sys
    readline = sys.stdin.buffer.readline

    from collections import deque

    n = int(readline())
    ab = [list(map(int, readline().split())) for _ in range(n-1)]

    l = [[] for _ in range(n+1)]
    for a, b in ab:
        l[a].append(b)
        l[b].append(a)

    parents = [-1] * (n+1)
    order = []
    q = deque()
    q.append(1)
    while q:
        c = q.pop()
        order.append(c)
        for i in l[c]:
            if i == parents[c]:
                continue
            parents[i] = c
            q.append(i)
    
    color = [-1] * (n+1)
    for i in order:
        ng = color[i]
        c = 1
        for j in l[i]:
            if j == parents[i]:
                continue
            if c == ng:
                c += 1
            color[j] = c
            c += 1
    
    ans = []
    for a, b in ab:
        if a == parents[b]:
            ans.append(color[b])
        else:
            ans.append(color[a])

    print(max(ans))
    print('\n'.join(list(map(str, ans))))

if __name__ == '__main__':
    main()

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

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

c = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    c[a].append(b)
    c[b].append(a)

s = 0
for i in range(n):
    if len(c[i]) > s:
        s = i

e = [0] * n
l = [0] * (n-1)
q = deque()
q.append((s, 0))
while q:
    f, v = q.pop()
    e[f] = 1
    j = 1
    for i in c[f]:
        if e[i] == 0:
            if j == v:
                j += 1
            q.appendleft((i, j))
            l[ab.index([min(f, i)+1, max(f, i)+1])] = j
            j += 1

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

Fixed submission:
def main():
    import sys
    readline = sys.stdin.buffer.readline

    from collections import deque

    n = int(readline())
    ab = [list(map(int, readline().split())) for _ in range(n-1)]

    l = [[] for _ in range(n+1)]
    for a, b in ab:
        l[a].append(b)
        l[b].append(a)

    parents = [-1] * (n+1)
    order = []
    q = deque()
    q.append(1)
    while q:
        c = q.pop()
        order.append(c)
        for i in l[c]:
            if i == parents[c]:
                continue
            parents[i] = c
            q.append(i)
    
    color = [-1] * (n+1)
    for i in order:
        ng = color[i]
        c = 1
        for j in l[i]:
            if j == parents[i]:
                continue
            if c == ng:
                c += 1
            color[j] = c
            c += 1
    
    ans = []
    for a, b in ab:
        if a == parents[b]:
            ans.append(color[b])
        else:
            ans.append(color[a])

    print(max(ans))
    print('\n'.join(list(map(str, ans))))

if __name__ == '__main__':
    main()