Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
input = sys.stdin.readline
INF = 10**9


n = int(input())
edges = [0] * (n-1)
for i in range(n-1):
    edges[i] = tuple(map(int, input().split()))
    

adjacency_list = [[] for i in range(n+1)]
for edge in edges:
    adjacency_list[edge[0]].append(edge[1])
    adjacency_list[edge[1]].append(edge[0])


max_order = max(len(i) for i in adjacency_list)
root = 0
for i in range(0, n+1):
    if len(adjacency_list[i]) == max_order:
        root = i
             

node_history = set()
edge_dict = dict()
node_deque = deque()


node_deque.append(root)
while len(node_deque):
    current_node = node_deque.popleft()
    undef_edges = []
    kinds = {i for i in range(1, max_order + 1)}
    
    for a in adjacency_list[current_node]:
        if a not in node_history:
            node_history.add(a)
            node_deque.append(a)
            
        if current_node < a:
            temp_edge = (current_node, a)
        else:
            temp_edge = (a, current_node)    
        
        if temp_edge in edge_dict.keys():
            kinds.remove(edge_dict[temp_edge])
        else:
            undef_edges.append(temp_edge)
            
    for undef_edge in undef_edges:
        kind = min(kinds)
        edge_dict[undef_edge] = kind
        kinds.remove(kind)


print(max_order)
for e in edges:
    print(edge_dict[e])

Fixed submission:
from collections import deque

def main():
	N, *AB = map(int, open(0).read().split())
	B = AB[1::2]

	E = [[] for _ in range(N + 1)]
	for a, b in zip(*[iter(AB)] * 2):
		E[a].append(b)

	Q = deque([1])
	C = [0] * (N + 1)
	while Q:
		v = Q.popleft()
		c = 0
		for u in E[v]:
			c += 1 + (c + 1 == C[v])
			C[u] = c
			Q.append(u)

	print(max(C))
	for b in B:
		print(C[b])

main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from collections import deque

def main():

    input_buffer = """
    
    """
    #8
    #1 2
    #2 3
    #2 4
    #2 5
    #4 7
    #5 6
    #6 8
    
    INPUT = list(reversed(input_buffer.strip().splitlines()))
    if INPUT:
        n = int(INPUT.pop())
        edges = [0] * (n-1)
        for i in range(n-1):
            edges[i] = tuple(map(int, INPUT.pop().split()))
    else:
        n = int(input())
        edges = [0] * (n-1)
        for i in range(n-1):
            edges[i] = tuple(map(int, input().split()))
        
    
    adjacency_list = [[] for i in range(n+1)]
    for edge in edges:
        adjacency_list[edge[0]].append(edge[1])
        adjacency_list[edge[1]].append(edge[0])
    
    
    max_order = max(len(i) for i in adjacency_list)
    root = 0
    for i in range(0, n+1):
        if len(adjacency_list[i]) == max_order:
            root = i
                 
    
    node_history = set()
    edge_dict = dict()
    node_deque = deque()
    
    
    node_deque.append(root)
    while len(node_deque):
        current_node = node_deque.popleft()
        undef_edges = []
        kinds = {i for i in range(1, max_order + 1)}
        
        for a in adjacency_list[current_node]:
            if a not in node_history:
                node_history.add(a)
                node_deque.append(a)
                
            if current_node < a:
                temp_edge = (current_node, a)
            else:
                temp_edge = (a, current_node)    
            
            if temp_edge in edge_dict.keys():
                kinds.remove(edge_dict[temp_edge])
            else:
                undef_edges.append(temp_edge)
                
        for undef_edge in undef_edges:
            kind = min(kinds)
            edge_dict[undef_edge] = kind
            kinds.remove(kind)
    
    
    print(max_order)
    for e in edges:
        print(edge_dict[e])
        
main()

Fixed submission:
from collections import deque

def main():
	N, *AB = map(int, open(0).read().split())
	B = AB[1::2]

	E = [[] for _ in range(N + 1)]
	for a, b in zip(*[iter(AB)] * 2):
		E[a].append(b)

	Q = deque([1])
	C = [0] * (N + 1)
	while Q:
		v = Q.popleft()
		c = 0
		for u in E[v]:
			c += 1 + (c + 1 == C[v])
			C[u] = c
			Q.append(u)

	print(max(C))
	for b in B:
		print(C[b])

main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from collections import deque

def main():
    input_buffer = """
    """
    #8
    #1 2
    #2 3
    #2 4
    #2 5
    #4 7
    #5 6
    #6 8
    
    INPUT = list(reversed(input_buffer.strip().splitlines()))
    if INPUT:
        n = int(INPUT.pop())
        edges = [0] * (n-1)
        for i in range(n-1):
            edges[i] = tuple(map(int, INPUT.pop().split()))
    else:
        n = int(input())
        edges = [0] * (n-1)
        for i in range(n-1):
            edges[i] = tuple(map(int, input().split()))
        
    
    adjacency_list = [[] for i in range(n+1)]
    for edge in edges:
        adjacency_list[edge[0]].append(edge[1])
        adjacency_list[edge[1]].append(edge[0])
    
    
    max_order = max(len(i) for i in adjacency_list)
    root = 0
    for i in range(0, n+1):
        if len(adjacency_list[i]) == max_order:
            root = i
                 
    
    node_history = set()
    edge_dict = dict()
    node_deque = deque()
    
    
    node_deque.append(root)
    while len(node_deque):
        current_node = node_deque.popleft()
        undef_edges = []
        kinds = {i for i in range(1, max_order + 1)}
        
        for a in adjacency_list[current_node]:
            if a not in node_history:
                node_history.add(a)
                node_deque.append(a)
                
            if current_node < a:
                temp_edge = (current_node, a)
            else:
                temp_edge = (a, current_node)    
            
            if temp_edge in edge_dict.keys():
                kinds.remove(edge_dict[temp_edge])
            else:
                undef_edges.append(temp_edge)
                
        for undef_edge in undef_edges:
            kind = min(kinds)
            edge_dict[undef_edge] = kind
            kinds.remove(kind)
    
    
    print(max_order)
    for e in edges:
        print(edge_dict[e])
    
if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def main():
	N, *AB = map(int, open(0).read().split())
	B = AB[1::2]

	E = [[] for _ in range(N + 1)]
	for a, b in zip(*[iter(AB)] * 2):
		E[a].append(b)

	Q = deque([1])
	C = [0] * (N + 1)
	while Q:
		v = Q.popleft()
		c = 0
		for u in E[v]:
			c += 1 + (c + 1 == C[v])
			C[u] = c
			Q.append(u)

	print(max(C))
	for b in B:
		print(C[b])

main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from collections import deque

input_buffer = """

"""
#8
#1 2
#2 3
#2 4
#2 5
#4 7
#5 6
#6 8

INPUT = list(reversed(input_buffer.strip().splitlines()))
if INPUT:
    n = int(INPUT.pop())
    edges = [0] * (n-1)
    for i in range(n-1):
        edges[i] = tuple(map(int, INPUT.pop().split()))
else:
    n = int(input())
    edges = [0] * (n-1)
    for i in range(n-1):
        edges[i] = tuple(map(int, input().split()))
    

adjacency_list = [[] for i in range(n+1)]
for edge in edges:
    adjacency_list[edge[0]].append(edge[1])
    adjacency_list[edge[1]].append(edge[0])


max_order = max(len(i) for i in adjacency_list)
root = 0
for i in range(0, n+1):
    if len(adjacency_list[i]) == max_order:
        root = i
             

node_history = set()
edge_dict = dict()
node_deque = deque()


node_deque.append(root)
while len(node_deque):
    current_node = node_deque.popleft()
    undef_edges = []
    kinds = {i for i in range(1, max_order + 1)}
    
    for a in adjacency_list[current_node]:
        if a not in node_history:
            node_history.add(a)
            node_deque.append(a)
            
        if current_node < a:
            temp_edge = (current_node, a)
        else:
            temp_edge = (a, current_node)    
        
        if temp_edge in edge_dict.keys():
            kinds.remove(edge_dict[temp_edge])
        else:
            undef_edges.append(temp_edge)
            
    for undef_edge in undef_edges:
        kind = min(kinds)
        edge_dict[undef_edge] = kind
        kinds.remove(kind)


print(max_order)
for e in edges:
    print(edge_dict[e])


Fixed submission:
from collections import deque

def main():
	N, *AB = map(int, open(0).read().split())
	B = AB[1::2]

	E = [[] for _ in range(N + 1)]
	for a, b in zip(*[iter(AB)] * 2):
		E[a].append(b)

	Q = deque([1])
	C = [0] * (N + 1)
	while Q:
		v = Q.popleft()
		c = 0
		for u in E[v]:
			c += 1 + (c + 1 == C[v])
			C[u] = c
			Q.append(u)

	print(max(C))
	for b in B:
		print(C[b])

main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

from collections import deque

input_buffer = """

"""
#8
#1 2
#2 3
#2 4
#2 5
#4 7
#5 6
#6 8

INPUT = list(reversed(input_buffer.strip().splitlines()))
if INPUT:
    n = int(INPUT.pop())
    edges = [0] * (n-1)
    for i in range(n-1):
        edges[i] = tuple(map(int, INPUT.pop().split()))
else:
    n = int(input())
    edges = [0] * (n-1)
    for i in range(n-1):
        edges[i] = tuple(map(int, input().split()))
    

adjacency_list = [[] for i in range(n+1)]
for edge in edges:
    adjacency_list[edge[0]].append(edge[1])
    adjacency_list[edge[1]].append(edge[0])


max_order = max(len(i) for i in adjacency_list)
root = 0
for i in range(0, n+1):
    if len(adjacency_list[i]) == max_order:
        root = i
             

node_history = set()
edge_dict = dict()
node_deque = deque()


node_deque.append(root)
while len(node_deque):
    current_node = node_deque.popleft()
    undef_edges = []
    kinds = {i for i in range(1, max_order + 1)}
    
    for a in adjacency_list[current_node]:
        if a not in node_history:
            node_history.add(a)
            node_deque.append(a)
            
        if current_node < a:
            temp_edge = (current_node, a)
        else:
            temp_edge = (a, current_node)    
        
        if temp_edge in edge_dict.keys():
            kinds.remove(edge_dict[temp_edge])
        else:
            undef_edges.append(temp_edge)
            
    for undef_edge in undef_edges:
        kind = min(kinds)
        edge_dict[undef_edge] = kind
        kinds.remove(kind)


print(max_order)
for e in edges:
    print(edge_dict[e])


Fixed submission:
from collections import deque

def main():
	N, *AB = map(int, open(0).read().split())
	B = AB[1::2]

	E = [[] for _ in range(N + 1)]
	for a, b in zip(*[iter(AB)] * 2):
		E[a].append(b)

	Q = deque([1])
	C = [0] * (N + 1)
	while Q:
		v = Q.popleft()
		c = 0
		for u in E[v]:
			c += 1 + (c + 1 == C[v])
			C[u] = c
			Q.append(u)

	print(max(C))
	for b in B:
		print(C[b])

main()