Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    n = int(input())
    ab = []
    colors = [[-1 for _ in range(n)] for _ in range(n)]
    visit = [-1 for _ in range(n)]
    tree = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a-1, b-1])
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    visit[0]=0
    q = deque([0])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 + (1+d==visit[s])
                visit[i] = d
                q.append(i)
    print(max(visit))
    for a, b in ab:
        print(visit[b])

if __name__=="__main__":
    main()

Fixed submission:
from collections import deque
def main():
    n = int(input())
    ab = []
    visit = [-1 for _ in range(n+1)]
    tree = [[] for _ in range(n+1)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a, b])
        tree[a].append(b)
        tree[b].append(a)
    visit[1]=0
    q = deque([1])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 + (1+d==visit[s])
                visit[i] = d
                q.append(i)
    print(max(visit))
    for a, b in ab:
        print(visit[b])

if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    n = int(input())
    ab = []
    colors = [[-1 for _ in range(n)] for _ in range(n)]
    visit = [-1 for _ in range(n)]
    tree = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a, b])
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    visit[0] = 0
    q = deque([0])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 +((d+1)==visit[s])
                colors[s][i] = visit[i] = d
                q.append(i)
    print(max([len(i) for i in tree]))
    for a, b in ab:
        print(colors[a-1][b-1])

if __name__=="__main__":
    main()

Fixed submission:
from collections import deque
def main():
    n = int(input())
    ab = []
    visit = [-1 for _ in range(n+1)]
    tree = [[] for _ in range(n+1)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a, b])
        tree[a].append(b)
        tree[b].append(a)
    visit[1]=0
    q = deque([1])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 + (1+d==visit[s])
                visit[i] = d
                q.append(i)
    print(max(visit))
    for a, b in ab:
        print(visit[b])

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)]
colors = [[-1 for _ in range(n)] for _ in range(n)]
visit = [-1 for _ in range(n)]
tree = [[] for _ in range(n)]
for i in range(n-1):
    a, b = ab[i]
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
visit[0] = 0
q = deque([0])
while q:
    s = q.popleft()
    d = 0
    for i in tree[s]:
        if visit[i]<0:
            d += 1 +((d+1)==visit[s])
            colors[s][i] = visit[i] = d
            q.append(i)
print(max([len(i) for i in tree]))
result = []
for a, b in ab:
    print(colors[a-1][b-1])

Fixed submission:
from collections import deque
def main():
    n = int(input())
    ab = []
    visit = [-1 for _ in range(n+1)]
    tree = [[] for _ in range(n+1)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a, b])
        tree[a].append(b)
        tree[b].append(a)
    visit[1]=0
    q = deque([1])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 + (1+d==visit[s])
                visit[i] = d
                q.append(i)
    print(max(visit))
    for a, b in ab:
        print(visit[b])

if __name__=="__main__":
    main()

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

def dfs(T,colors,visit,q):
    if len(q)==0:
        return colors
    s = q.popleft()
    d = 0
    for i in T[s]:
        if visit[i]<0:
            d += 1
            d += (d==visit[s])
            colors[s][i] = d
            visit[i] = d
            q.append(i)
    return dfs(T,colors,visit,q)

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

colors = [[-1 for _ in range(n)] for _ in range(n)]
visit = [-1 for _ in range(n)]
tree = [[] for _ in range(n)]

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

visit[0] = 0
colors = dfs(tree,colors,visit,deque([0]))
result = []
for a, b in ab:
    result.append(colors[a-1][b-1])
print(max(result))
for i in range(n-1):
    print(result[i])

Fixed submission:
from collections import deque
def main():
    n = int(input())
    ab = []
    visit = [-1 for _ in range(n+1)]
    tree = [[] for _ in range(n+1)]
    for i in range(n-1):
        a, b = map(int,input().split())
        ab.append([a, b])
        tree[a].append(b)
        tree[b].append(a)
    visit[1]=0
    q = deque([1])
    while q:
        s = q.pop()
        d = 0
        for i in tree[s]:
            if visit[i]<0:
                d += 1 + (1+d==visit[s])
                visit[i] = d
                q.append(i)
    print(max(visit))
    for a, b in ab:
        print(visit[b])

if __name__=="__main__":
    main()