Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n=int(input())
col=[{}for i in range(n)]
edges=[set() for i in range(n)]
ret=[0]*(n-1)
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].add((s-1,i));edges[s-1].add((a-1,i))
m=0
ind=-1
for i in range(n):
    if m<len(edges[i]):
        ind=i
        m=len(edges[i])
print(m)
from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i,e in zip(range(1,m+1),edges[ind]):
    col[e[0]][ind]=i
    dq.append(e[0])
    ret[e[1]]=i
x=set(range(1,m+1))
while dq:
    a=dq.popleft()
    c=x-set(col[a].values())
    ng=set(col[a].keys())
    for to,num in edges[a]:
        if to not in ng:
            cc=c.pop()
            col[to][a]=cc
            dq.append(to)
            ret[num]=cc
        
print(*ret,sep="\n")

Fixed submission:
n=int(input())
edges=[[] for i in range(1+n)]
e={}
for i in range(n-1):
    """#weighted->erase_,__,___=map(int,input().split())
    edges[_].append((__,___))
    edges[__].append((_,___))
    """
    _,__=map(int,input().split())
    edges[_].append(__)
    edges[__].append(_)
    e[(_,__)]=i
    e[(__,_)]=i
    """
"""#weighted->erase
f=max(len(j)for j in edges)
print(f)
ret=[0]*(n-1)
from collections import deque
dq=deque([(1,1)])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
while dq:
    a,c=dq.popleft()
    for to in edges[a]:
        if ret[e[(a,to)]]:continue
        ret[e[(a,to)]]=c
        c=c%f+1
        dq.append((to,c))
        
print(*ret,sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n=int(input())
col=[{}for i in range(n)]
edges=[set() for i in range(n)]
ed=[]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].add(s-1);edges[s-1].add(a-1)
    ed.append((a-1,s-1))
m=0
ind=-1
for i in range(n):
    if m<len(edges[i]):
        ind=i
        m=len(edges[i])
print(m)
from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i,e in zip(range(1,m+1),edges[ind]):
    col[e][ind]=i
    dq.append(e)
x=set(range(1,m+1))
while dq:
    a=dq.popleft()
    c=x-set(col[a].values())
    for to in edges[a]-set(col[a].keys()):
        col[to][a]=c.pop()
        dq.append(to)
for i,j in ed:
    if j in col[i]:
        print(col[i][j])
    else:
        print(col[j][i])


Fixed submission:
n=int(input())
edges=[[] for i in range(1+n)]
e={}
for i in range(n-1):
    """#weighted->erase_,__,___=map(int,input().split())
    edges[_].append((__,___))
    edges[__].append((_,___))
    """
    _,__=map(int,input().split())
    edges[_].append(__)
    edges[__].append(_)
    e[(_,__)]=i
    e[(__,_)]=i
    """
"""#weighted->erase
f=max(len(j)for j in edges)
print(f)
ret=[0]*(n-1)
from collections import deque
dq=deque([(1,1)])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
while dq:
    a,c=dq.popleft()
    for to in edges[a]:
        if ret[e[(a,to)]]:continue
        ret[e[(a,to)]]=c
        c=c%f+1
        dq.append((to,c))
        
print(*ret,sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n=int(input())
col=[{}for i in range(n)]
edges=[set() for i in range(n)]
ed=[]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].add(s-1);edges[s-1].add(a-1)
    ed.append((a-1,s-1))
m=0
ind=-1
for i in range(n):
    if m<len(edges[i]):
        ind=i
        m=len(edges[i])
print(m)
from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i,e in zip(range(1,m+1),edges[ind]):
    col[e][ind]=i
    dq.append(e)
x=set(range(1,m+1))
while dq:
    a=dq.popleft()
    c=x-set(col[a].values())
    for to in edges[a]-set(col[a].keys()):
        col[to][a]=c.pop()
        dq.append(to)
for i,j in ed:
    if j in col[i]:
        print(col[i][j])
    else:
        print(col[j][i])


Fixed submission:
n=int(input())
edges=[[] for i in range(1+n)]
e={}
for i in range(n-1):
    """#weighted->erase_,__,___=map(int,input().split())
    edges[_].append((__,___))
    edges[__].append((_,___))
    """
    _,__=map(int,input().split())
    edges[_].append(__)
    edges[__].append(_)
    e[(_,__)]=i
    e[(__,_)]=i
    """
"""#weighted->erase
f=max(len(j)for j in edges)
print(f)
ret=[0]*(n-1)
from collections import deque
dq=deque([(1,1)])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
while dq:
    a,c=dq.popleft()
    for to in edges[a]:
        if ret[e[(a,to)]]:continue
        ret[e[(a,to)]]=c
        c=c%f+1
        dq.append((to,c))
        
print(*ret,sep="\n")