Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
data = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append([i,b])  # i本目でbに
    data[b].append([i,a])

k = max(len(data[i]) for i in range(n))
print(k)
ans = [-1 for i in range(n)]


stack = [[0,n,k]]
while stack:
    now,pre,pre_path = stack.pop()
    color = [True for i in range(k + 1)]
    color[ans[pre_path]] = False

    for nex_path, nex in data[now]:
        if nex == pre: continue
        for num, c in enumerate(color):
            if c:
                ans[nex_path] = num
                color[num] = False
                stack.append([nex, now, nex_path])
                break


for x  in ans[:-1]:
    print(x+1)

Fixed submission:
n = int(input())
G = [[] for _ in range(n)]
d = {}
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    d[(a,b)] = d[(b,a)] = i

k = 0
ans = [None]*(n-1)

stack = [(0,-1)] #node, par

while stack:
    now,pre = stack.pop()
    color = 1
    for nex in G[now]:
        if nex == pre:continue
        stack.append((nex,now))
        if pre != -1:
            if color == ans[d[(now,pre)]]:
                color += 1
        ans[d[(now,nex)]] = color
        k = max(k,color)
        color += 1

print(k)
print(*ans, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
data = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append([i,b])  # i本目でbに
    data[b].append([i,a])

k = max(len(data[i]) for i in range(n))
print(k)
ans = [-1 for i in range(n)]


stack = [[0,n,k]]
while stack:
    now,pre,pre_path = stack.pop()
    color = [True for i in range(k + 1)]
    color[ans[pre_path]] = False

    for nex_path, nex in data[now]:
        if nex == pre: continue
        for num, c in enumerate(color):
            if c:
                ans[nex_path] = num
                color[num] = False
                stack.append([nex, now, nex_path])
                break


for x  in ans[:-1]:
    print(x+1)

Fixed submission:
n = int(input())
G = [[] for _ in range(n)]
d = {}
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    d[(a,b)] = d[(b,a)] = i

k = 0
ans = [None]*(n-1)

stack = [(0,-1)] #node, par

while stack:
    now,pre = stack.pop()
    color = 1
    for nex in G[now]:
        if nex == pre:continue
        stack.append((nex,now))
        if pre != -1:
            if color == ans[d[(now,pre)]]:
                color += 1
        ans[d[(now,nex)]] = color
        k = max(k,color)
        color += 1

print(k)
print(*ans, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
data = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append([i,b])  # i本目でbに
    data[b].append([i,a])

k = max(len(data[i]) for i in range(n))
print(k)
ans = [-1 for i in range(n)]

def solve(now,pre,pre_path):
    color = [True for i in range(k+1)]
    color[ans[pre_path]] = False

    for nex_path,nex in data[now]:
        if nex == pre:continue
        for num,c in enumerate(color):
            if c:
                ans[nex_path] = num
                color[num] = False
                solve(nex,now,nex_path)
                break
    return

solve(0,n,k)
for x  in ans[:-1]:
    print(x+1)

Fixed submission:
n = int(input())
G = [[] for _ in range(n)]
d = {}
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    d[(a,b)] = d[(b,a)] = i

k = 0
ans = [None]*(n-1)

stack = [(0,-1)] #node, par

while stack:
    now,pre = stack.pop()
    color = 1
    for nex in G[now]:
        if nex == pre:continue
        stack.append((nex,now))
        if pre != -1:
            if color == ans[d[(now,pre)]]:
                color += 1
        ans[d[(now,nex)]] = color
        k = max(k,color)
        color += 1

print(k)
print(*ans, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
data = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append([i,b])  # i本目でbに
    data[b].append([i,a])

k = max(len(data[i]) for i in range(n))
print(k)
ans = [-1 for i in range(n)]

def solve(now,pre,pre_path):
    color = [True for i in range(k+1)]
    color[ans[pre_path]] = False

    for nex_path,nex in data[now]:
        if nex == pre:continue
        for num,c in enumerate(color):
            if c:
                ans[nex_path] = num
                color[num] = False
                solve(nex,now,nex_path)
                break
    return

solve(0,n,k)
for x  in ans[:-1]:
    print(x+1)

Fixed submission:
n = int(input())
G = [[] for _ in range(n)]
d = {}
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    d[(a,b)] = d[(b,a)] = i

k = 0
ans = [None]*(n-1)

stack = [(0,-1)] #node, par

while stack:
    now,pre = stack.pop()
    color = 1
    for nex in G[now]:
        if nex == pre:continue
        stack.append((nex,now))
        if pre != -1:
            if color == ans[d[(now,pre)]]:
                color += 1
        ans[d[(now,nex)]] = color
        k = max(k,color)
        color += 1

print(k)
print(*ans, sep="\n")