Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from heapq import heapify,heappop,heappush
n = int(input())
data = [[] for _ in range(n)]
color = [[-1]*n for _ in range(n)]
sides = []
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
kinds = -1
m = -1
for i in range(n):
    if kinds < len(data[i]):
        kinds = len(data[i])
        m = i
unused = [[i for i in range(1,kinds+1)] for _ in range(n)]
against = [[] for _ in range(n)]
for i in range(n):
    heapify(unused[i])
    heapify(against[i])
print(kinds)
q = deque([m])
visited = [0]*n
while q:
    p = q.popleft()
    visited[p] = 1
    for i in data[p]:
        if visited[i]:
            continue
        while against[p] and (unused[p][0] == against[p][0]):
            heappop(against[p])
            heappop(unused[p])
        color[p][i] = unused[p][0]
        color[i][p] = unused[p][0]
        q.append(i)
        heappush(against[i],heappop(unused[p]))
for i,j in sides:
    print(color[i][j])

Fixed submission:
from collections import deque,defaultdict
n = int(input())
data = [[] for _ in range(n)]
sides = []
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
I = defaultdict(lambda:0)
cs = [0]*n
used = [0]*n
q = deque([0])
used[0]=1
k = 0
while q:
    u = q.popleft()
    if k < len(data[u]):
        k = len(data[u])
    cur = 1
    for v in data[u]:
        if used[v]:continue
        if cur==cs[u]:cur+=1
        cs[v] = cur
        I[(u,v)] = cur
        I[(v,u)] = cur
        cur += 1
        used[v] = 1
        q.append(v)
print(k)
for i,j in sides:
    print(I[(i,j)])

Buggy python submission (verdict: Runtime Error):
from collections import deque
from heapq import heapify,heappop,heappush
n = int(input())
data = [[] for _ in range(n)]
color = [[-1]*n for _ in range(n)]
sides = []
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
kinds = -1
m = -1
for i in range(n):
    if kinds < len(data[i]):
        kinds = len(data[i])
        m = i
unused = [[i for i in range(1,kinds+1)] for _ in range(n)]
against = [[] for _ in range(n)]
for i in range(n):
    heapify(unused[i])
    heapify(against[i])
print(kinds)
q = deque([m])
visited = [0]*n
while q:
    p = q.popleft()
    visited[p] = 1
    for i in data[p]:
        if visited[i]:
            continue
        while against[p] and (unused[p][0] == against[p][0]):
            heappop(against[p])
            heappop(unused[p])
        color[p][i] = unused[p][0]
        color[i][p] = unused[p][0]
        q.append(i)
        heappush(against[i],heappop(unused[p]))
for i,j in sides:
    print(color[i][j])

Fixed submission:
from collections import deque,defaultdict
n = int(input())
data = [[] for _ in range(n)]
sides = []
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
I = defaultdict(lambda:0)
cs = [0]*n
used = [0]*n
q = deque([0])
used[0]=1
k = 0
while q:
    u = q.popleft()
    if k < len(data[u]):
        k = len(data[u])
    cur = 1
    for v in data[u]:
        if used[v]:continue
        if cur==cs[u]:cur+=1
        cs[v] = cur
        I[(u,v)] = cur
        I[(v,u)] = cur
        cur += 1
        used[v] = 1
        q.append(v)
print(k)
for i,j in sides:
    print(I[(i,j)])

Buggy python submission (verdict: Runtime Error):
n = int(input())
sides = []
data = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
kinds = -1
for i in range(n):
    kinds = max(kinds,len(data[i]))
print(kinds)
strage = [[i for i in range(1,kinds+1)] for i in range(n)]
for i,j in sides:
    p = strage[i].pop()
    q = strage[j].pop()
    if p == q:
        print(p)
    elif p > q:
        print(q)
        strage[i].remove(q)
        strage.append(p)
    else:
        print(p)
        strage[j].remove(p)
        strage.append(q)

Fixed submission:
from collections import deque,defaultdict
n = int(input())
data = [[] for _ in range(n)]
sides = []
for _ in range(n-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    data[a].append(b)
    data[b].append(a)
    sides.append((a,b))
I = defaultdict(lambda:0)
cs = [0]*n
used = [0]*n
q = deque([0])
used[0]=1
k = 0
while q:
    u = q.popleft()
    if k < len(data[u]):
        k = len(data[u])
    cur = 1
    for v in data[u]:
        if used[v]:continue
        if cur==cs[u]:cur+=1
        cs[v] = cur
        I[(u,v)] = cur
        I[(v,u)] = cur
        cur += 1
        used[v] = 1
        q.append(v)
print(k)
for i,j in sides:
    print(I[(i,j)])