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

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = dict()
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[(a, b)] = i
    v, k = 0, 0
    for i in range(n):
        if len(al[i]) > k:
            v = i
            k = len(al[i])
    print(k)
    sn = deque([v])
    sd = set()
    cv = [set() for _ in range(n)]
    ce = dict()
    while sn:
        v = sn.popleft()
        sd.add(v)
        for vn in al[v]:
            if vn in sd:
                continue
            for ci in range(1, k + 1):
                if ci not in cv[v] and ci not in cv[vn]:
                    cv[v].add(ci)
                    cv[vn].add(ci)
                    if (v, vn) in e:
                        ce[(v, vn)] = ci
                    else:
                        ce[(vn, v)] = ci
                    break
            sn.append(vn)
    for k, _ in sorted(e.items(), key=itemgetter(1)):
        print(ce[k])

if __name__ == '__main__':
    main()

Fixed submission:
import sys
from collections import deque
from operator import itemgetter

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = dict()
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[(a, b)] = i
    v, k = 0, 0
    for i in range(n):
        if len(al[i]) > k:
            v = i
            k = len(al[i])
    print(k)
    sn = deque([(v, 0)])
    sd = set()
    ce = dict()
    while sn:
        v, cv = sn.popleft()
        sd.add(v)
        ci = 1
        for vn in al[v]:
            if vn in sd:
                continue
            if ci == cv:
                ci += 1
            if (v, vn) in e:
                ce[(v, vn)] = ci
            else:
                ce[(vn, v)] = ci
            sn.append((vn, ci))
            ci += 1
    for k, _ in sorted(e.items(), key=itemgetter(1)):
        print(ce[k])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = [(-1, -1) for _ in range(n - 1)]
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[i] = (a, b)
    k = max(map(len, al))
    print(k)
    c = [{i + 1 for i in range(k)} for _ in range(n)]
    for i in range(n - 1):
        a, b = e[i]
        ci = min(c[a] & c[b])
        print(ci)
        c[a].remove(ci)
        c[b].remove(ci)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
from collections import deque
from operator import itemgetter

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = dict()
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[(a, b)] = i
    v, k = 0, 0
    for i in range(n):
        if len(al[i]) > k:
            v = i
            k = len(al[i])
    print(k)
    sn = deque([(v, 0)])
    sd = set()
    ce = dict()
    while sn:
        v, cv = sn.popleft()
        sd.add(v)
        ci = 1
        for vn in al[v]:
            if vn in sd:
                continue
            if ci == cv:
                ci += 1
            if (v, vn) in e:
                ce[(v, vn)] = ci
            else:
                ce[(vn, v)] = ci
            sn.append((vn, ci))
            ci += 1
    for k, _ in sorted(e.items(), key=itemgetter(1)):
        print(ce[k])

if __name__ == '__main__':
    main()