Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

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

    k = 0
    for i in adj:
        k = max(k, len(i))
    print(k)


    c = [[0]*n for _ in range(n)]
    d = deque([[0, 0]])
    visited = [0]*n
    visited[0] = 1
    c_l = [i for i in range(1, k+1)]
    while d:
        node, color = d.popleft()
        if color == 0:
            color_list = c_l
        else:
            color_list = c_l[:color-1] + c_l[color:]
        cnt = 0
        for x in adj[node]:
            if visited[x]:
                continue
            visited[x] = 1
            c[node][x] = color_list[cnt]
            d.append([x, color_list[cnt]])
            cnt += 1

    for a,b in ab:
        print(c[a-1][b-1])

    # c = [[0]*n for _ in range(n)]
    # visited = [0]*n
    # def dfs(node):
    #     visited[node] = 1
    #     color = 1
    #     for nextnode in adj[node]:
    #         if visited[nextnode]:
    #             continue
    #         c[node][nextnode] = color
    #         dfs(nextnode)
    #         color += 1


    # c = [[] for _ in range(n)]
    # for a,b in ab:
    #     a -= 1
    #     b -= 1
    #     for i in range(1, k+1):
    #         if not i in c[a] and not i in c[b]:
    #             c[a].append(i)
    #             c[b].append(i)
    #             print(i)
    #             break


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

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

    

    c = [0]*n
    d = deque([0])
    while d:
        v = d.popleft()
        cnt = 1
        for nv in adj[v]:
            if c[v] == cnt:
                cnt += 1
            c[nv] = cnt
            cnt += 1
            d.append(nv)

    print(max(c))

    for i in order:
        print(c[i])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

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

    k = 0
    for i in adj:
        k = max(k, len(i))
    print(k)


    c = [[0]*n for _ in range(n)]
    d = deque([[0, 0]])
    visited = [0]*n
    visited[0] = 1
    c_l = [i for i in range(1, k+1)]
    while d:
        node, color = d.popleft()
        if color == 0:
            color_list = c_l
        else:
            color_list = c_l[:color-1] + c_l[color:]
        cnt = 0
        for x in adj[node]:
            if visited[x]:
                continue
            visited[x] = 1
            c[node][x] = color_list[cnt]
            d.append([x, color_list[cnt]])
            cnt += 1

    for a,b in ab:
        print(c[a-1][b-1])



if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

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

    

    c = [0]*n
    d = deque([0])
    while d:
        v = d.popleft()
        cnt = 1
        for nv in adj[v]:
            if c[v] == cnt:
                cnt += 1
            c[nv] = cnt
            cnt += 1
            d.append(nv)

    print(max(c))

    for i in order:
        print(c[i])

if __name__ == '__main__':
    main()