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

input = sys.stdin.readline

def main():
    N = int(input())
    visited = [False] * N
    E = [deque() for i in range(N)]
    nums = [set() for i in range(N)] 
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        E[a-1].append((b-1, i))
        E[b-1].append((a-1, i))
        
    ans_ = max([len(e) for e in E])
    G = set(i for i in range(ans_))
    
    q = deque([0])
    
    while q:
        v = q.popleft()
        
        if visited[v]: continue
    
        visited[v] = True
        
        use_nums = deque(list(G - nums[v]))
        for u, idx in E[v]:
            if visited[u]: continue
            
            n = use_nums.popleft()
            ans[idx] = str(n+1)
            nums[u].add(n)
            q.append(u)
            
    print(ans_)
    print('\n'.join(ans))

if __name__ == '__main__':
    main()

Fixed submission:
# -*- coding: utf-8 -*

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    G = [[] for i in range(N)]
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        
        G[a].append((b, i))
        G[b].append((a, i))
        
    q = [(0, -1)]
    
    visited = [False] * N
    cur = 0
    
    while q and cur < N:
        v, c = q.pop(0)
    
        if visited[v]: continue
        visited[v] = True
        cur += 1
        
        k = 0
        for u, idx in G[v]:
            if visited[u]: continue
            if c == k: k += 1
            
            ans[idx] = k
            q.append((u, k))
            k += 1
            
    print(max(ans)+1)
    for a in ans: print(a+1)
     
if __name__ == '__main__':
    main()

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

input = sys.stdin.readline

def main():
    N = int(input())
    visited = [False] * N
    E = [deque() for i in range(N)]
    nums = [set() for i in range(N)] 
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        E[a-1].append((b-1, i))
        E[b-1].append((a-1, i))
        
    ans_ = max([len(e) for e in E])
    G = set(i for i in range(ans_))
    
    q = deque([0])
    
    while q:
        v = q.popleft()
        
        if visited[v]: continue
    
        visited[v] = True
        
        use_nums = list(G - nums[v])
        for u, idx in E[v]:
            if visited[u]: continue
            
            n = use_nums.pop(0)
            ans[idx] = str(n+1)
            nums[u].add(n)
            q.append(u)
            
    print(ans_)
    print('\n'.join(ans))

if __name__ == '__main__':
    main()

Fixed submission:
# -*- coding: utf-8 -*

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    G = [[] for i in range(N)]
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        
        G[a].append((b, i))
        G[b].append((a, i))
        
    q = [(0, -1)]
    
    visited = [False] * N
    cur = 0
    
    while q and cur < N:
        v, c = q.pop(0)
    
        if visited[v]: continue
        visited[v] = True
        cur += 1
        
        k = 0
        for u, idx in G[v]:
            if visited[u]: continue
            if c == k: k += 1
            
            ans[idx] = k
            q.append((u, k))
            k += 1
            
    print(max(ans)+1)
    for a in ans: print(a+1)
     
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    visited = [False] * N
    E = [[] for i in range(N)]
    nums = [set() for i in range(N)] 
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        E[a-1].append((b-1, i))
        E[b-1].append((a-1, i))
        
    ans_ = max([len(e) for e in E])
    G = set(i for i in range(ans_))
    
    q = [0]
    
    while q:
        v = q.pop(0)
        
        if visited[v]: continue
    
        visited[v] = True
        
        use_nums = list(G - nums[v])
        for u, idx in E[v]:
            if visited[u]: continue
            
            n = use_nums.pop(0)
            ans[idx] = str(n+1)
            nums[u].add(n)
            q.append(u)
            
    print(ans_)
    print('\n'.join(ans))
    
if __name__ == '__main__':
    main()

Fixed submission:
# -*- coding: utf-8 -*

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    G = [[] for i in range(N)]
    ans = [None] * (N-1)
    
    for i in range(N-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        
        G[a].append((b, i))
        G[b].append((a, i))
        
    q = [(0, -1)]
    
    visited = [False] * N
    cur = 0
    
    while q and cur < N:
        v, c = q.pop(0)
    
        if visited[v]: continue
        visited[v] = True
        cur += 1
        
        k = 0
        for u, idx in G[v]:
            if visited[u]: continue
            if c == k: k += 1
            
            ans[idx] = k
            q.append((u, k))
            k += 1
            
    print(max(ans)+1)
    for a in ans: print(a+1)
     
if __name__ == '__main__':
    main()