Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
edge = []
color = [set() for _ in range(n)]
ans = dict()
for i in range(n-1):
    a,b = map(int,input().split())
    edge.append([a,b])
    tree[a-1].append(b)
    tree[b-1].append(a)

work_q = deque([])
visited = set()
work_q.append(1)
visited.add(1)
while work_q:
    here = work_q.popleft()
    for i in tree[here-1]:
        col = 1
        if i not in visited:
            work_q.append(i)
            visited.add(i)
            while col in color[here-1]:
                col += 1
            color[here-1].add(col)
            color[i-1].add(col)
            ans[(here-1,i-1)] = col
            col += 1

print(max(ans.values()))
for i in range(n-1):
    print(ans[edge[i][0]-1,edge[i][1]-1])


Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n = int(input())
edge = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int,input().split())
    edge[a].append((b, i))
    edge[b].append((a, i))
colors = 0
for i, line in enumerate(edge):
    colors = max(colors, len(line))
print(colors)
work_q = deque()
work_q.append((1, -1, -1))
color = [-1] * (n - 1)

while work_q:
    here, prev, col = work_q.popleft()
    cnt = 1
    for to, ind in edge[here]:
        if to == prev:
            continue
        if cnt == col:
            cnt += 1
        color[ind] = cnt
        work_q.append((to, here, cnt))
        cnt += 1
print(*color, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
edge = []
max_c = [0]*n
color = [set() for _ in range(n)]
ans = dict()
for i in range(n-1):
    a,b = map(int,input().split())
    edge.append([a,b])
    tree[a-1].append(b)
    tree[b-1].append(a)
    max_c[a-1] += 1
    max_c[b-1] += 1
print(max(max_c))

work_q = deque([])
visited = set()
work_q.append(1)
visited.add(1)
while work_q:
    here = work_q.popleft()
    for i in tree[here-1]:
        col = 1
        if i not in visited:
            work_q.append(i)
            visited.add(i)
            while col in color[here-1]:
                col += 1
            color[here-1].add(col)
            color[i-1].add(col)
            ans[(here-1,i-1)] = col
            col += 1

for i in range(n-1):
    print(ans[edge[i][0]-1,edge[i][1]-1])


Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n = int(input())
edge = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int,input().split())
    edge[a].append((b, i))
    edge[b].append((a, i))
colors = 0
for i, line in enumerate(edge):
    colors = max(colors, len(line))
print(colors)
work_q = deque()
work_q.append((1, -1, -1))
color = [-1] * (n - 1)

while work_q:
    here, prev, col = work_q.popleft()
    cnt = 1
    for to, ind in edge[here]:
        if to == prev:
            continue
        if cnt == col:
            cnt += 1
        color[ind] = cnt
        work_q.append((to, here, cnt))
        cnt += 1
print(*color, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
edge = []
max_c = [0]*n
color = [set() for _ in range(n)]
ans = dict()
for i in range(n-1):
    a,b = map(int,input().split())
    edge.append([a,b])
    tree[a-1].append(b)
    tree[b-1].append(a)
    max_c[a-1] += 1
    max_c[b-1] += 1
print(max(max_c))


def breadth_first_search(start,W):
    work_q = deque([])
    visited = set()
    work_q.append(start)
    visited.add(start)
    while work_q:
        here = work_q.popleft()
        for i in W[here-1]:
            if i not in visited:
                work_q.append(i)
                visited.add(i)
                col = 1
                while col in color[here-1]:
                    col += 1
                color[here-1].add(col)
                color[i-1].add(col)
                ans[(here-1,i-1)] = col
    return visited

breadth_first_search(1,tree)
for i in range(n-1):
    start = edge[i][0]
    goal = edge[i][1]
    print(ans[start-1,goal-1])


Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n = int(input())
edge = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int,input().split())
    edge[a].append((b, i))
    edge[b].append((a, i))
colors = 0
for i, line in enumerate(edge):
    colors = max(colors, len(line))
print(colors)
work_q = deque()
work_q.append((1, -1, -1))
color = [-1] * (n - 1)

while work_q:
    here, prev, col = work_q.popleft()
    cnt = 1
    for to, ind in edge[here]:
        if to == prev:
            continue
        if cnt == col:
            cnt += 1
        color[ind] = cnt
        work_q.append((to, here, cnt))
        cnt += 1
print(*color, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
edge = []
max_c = [0]*n
color = [set() for _ in range(n)]
ans = dict()
for i in range(n-1):
    a,b = map(int,input().split())
    edge.append([a,b])
    tree[a-1].append(b)
    tree[b-1].append(a)
    max_c[a-1] += 1
    max_c[b-1] += 1
print(max(max_c))


def breadth_first_search(start,W):
    work_q = deque([])
    visited = set()
    work_q.append(start)
    visited.add(start)
    while work_q:
        here = work_q.popleft()
        for i in W[here-1]:
            if i not in visited:
                work_q.append(i)
                visited.add(i)
                col = 1
                while col in color[here-1]:
                    col += 1
                color[here-1].add(col)
                color[i-1].add(col)
                ans[(here-1,i-1)] = col
    return visited

breadth_first_search(1,tree)
for i in range(n-1):
    start = edge[i][0]
    goal = edge[i][1]
    print(ans[start-1,goal-1])


Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

n = int(input())
edge = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int,input().split())
    edge[a].append((b, i))
    edge[b].append((a, i))
colors = 0
for i, line in enumerate(edge):
    colors = max(colors, len(line))
print(colors)
work_q = deque()
work_q.append((1, -1, -1))
color = [-1] * (n - 1)

while work_q:
    here, prev, col = work_q.popleft()
    cnt = 1
    for to, ind in edge[here]:
        if to == prev:
            continue
        if cnt == col:
            cnt += 1
        color[ind] = cnt
        work_q.append((to, here, cnt))
        cnt += 1
print(*color, sep="\n")