Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
l=[list(map(int,input().split()))+[i] for i in range(n-1)]
l.sort()
#print(l)
aa=[[] for _ in range(n)]
#aa[l[0][0]-1].append(1)
def bfs(v):
    d=[-1]*n
    d[v]=0
    q=[v]
    c=1
    #print(d,q)
    while q:
        q1=[]
        for i in q:
            for j in pt[i]:
                if d[j]==-1:
                    d[j]=c
                    q1.append(j)
                    for k in range(1,n):
                        if k not in set(aa[i]) and k not in set(aa[j]):
                            aa[j].append(k)
                            aa[i].append(k)
                            break
        q=q1
        c+=1
        #print(aa,d,q)
    return d


pt=[[] for _ in range(n)]
for i in range(n-1):
    pt[l[i][0]-1].append(l[i][1]-1)
    pt[l[i][1]-1].append(l[i][0]-1)
#print(pt)
ans=bfs(l[0][0]-1)
#print(aa)
A=0
for i in range(n):
    A=max(A,max(aa[i]))
print(A)
for i in range(n-1):
    #print(aa)
    #print(aa[l[i][0]-1][0])
    l[i].append(aa[l[i][0]-1][0])
    aa[l[i][0]-1]=aa[l[i][0]-1][1:]
    aa[l[i][1]-1]=aa[l[i][1]-1][1:]
l=sorted(l, key=lambda x: x[2])
for i in range(n-1):
    print(l[i][3])


Fixed submission:
n = int(input())
l=[list(map(int,input().split()))+[i+1] for i in range(n-1)]
l.sort()
flg = [0 for _ in range(n+1)]
ans = [0 for _ in range(n)]
#print(l)
t=0
count=0
for i in range(n-1):
    a,b,c=l[i]
    if t!=a:
        t=a
        count=1
    if flg[a]==count:
        count+=1
    flg[b]=count
    ans[c]=count
    count+=1
    #print(flg,ans)
print(max(flg))
for i in range(1,n):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
l=[list(map(int,input().split()))+[i] for i in range(n-1)]
l.sort()
#print(l)
aa=[[] for _ in range(n)]
#aa[l[0][0]-1].append(1)
def bfs(v):
    d=[-1]*n
    d[v]=0
    q=[v]
    c=1
    #print(d,q)
    while q:
        q1=[]
        for i in q:
            for j in pt[i]:
                if d[j]==-1:
                    d[j]=c
                    q1.append(j)
                    for k in range(1,n):
                        if k not in aa[i] and k not in aa[j]:
                            aa[j].append(k)
                            aa[i].append(k)
                            break
        q=q1
        c+=1
        #print(aa,d,q)
    return d


pt=[[] for _ in range(n)]
for i in range(n-1):
    pt[l[i][0]-1].append(l[i][1]-1)
    pt[l[i][1]-1].append(l[i][0]-1)
#print(pt)
ans=bfs(l[0][0]-1)
#print(aa)
A=0
for i in range(n):
    A=max(A,max(aa[i]))
print(A)
for i in range(n-1):
    #print(aa)
    #print(aa[l[i][0]-1][0])
    l[i].append(aa[l[i][0]-1][0])
    aa[l[i][0]-1]=aa[l[i][0]-1][1:]
    aa[l[i][1]-1]=aa[l[i][1]-1][1:]
l=sorted(l, key=lambda x: x[2])
for i in range(n-1):
    print(l[i][3])


Fixed submission:
n = int(input())
l=[list(map(int,input().split()))+[i+1] for i in range(n-1)]
l.sort()
flg = [0 for _ in range(n+1)]
ans = [0 for _ in range(n)]
#print(l)
t=0
count=0
for i in range(n-1):
    a,b,c=l[i]
    if t!=a:
        t=a
        count=1
    if flg[a]==count:
        count+=1
    flg[b]=count
    ans[c]=count
    count+=1
    #print(flg,ans)
print(max(flg))
for i in range(1,n):
    print(ans[i])

Buggy python submission (verdict: Runtime Error):
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
l.sort()
#print(l)
r=[0]*n
for i in range(n-1):
    r[l[i][0]-1]+=1
    r[l[i][1]-1]+=1
aa=0
c=0

for i in range(n):
    if aa<=r[i]:
        aa=r[i]
        c=i+1

A=1;print(max(r))
p=[0]*(n-1)
w=[[] for _ in range(n)]
for i in range(n-1):
    if l[i][0]==c or l[i][1]==c:
        p[i]=A
        w[l[i][0]-1].append(A)
        w[l[i][1]-1].append(A)
        A+=1

box=set([i for i in range(1,max(r)+1)])
for i in range(n-1):
    if l[i][0]!=c and l[i][1]!=c:
        B=box-set(w[l[i][0]-1]+w[l[i][1]-1])
        #print(B)
        w[l[i][0]-1].append(list(B)[0])
        w[l[i][1]-1].append(list(B)[0])
        p[i]=list(B)[0]

#print(r,c,w)
#print(p)

for i in range(n-1):
    print(p[i])

Fixed submission:
n = int(input())
l=[list(map(int,input().split()))+[i+1] for i in range(n-1)]
l.sort()
flg = [0 for _ in range(n+1)]
ans = [0 for _ in range(n)]
#print(l)
t=0
count=0
for i in range(n-1):
    a,b,c=l[i]
    if t!=a:
        t=a
        count=1
    if flg[a]==count:
        count+=1
    flg[b]=count
    ans[c]=count
    count+=1
    #print(flg,ans)
print(max(flg))
for i in range(1,n):
    print(ans[i])