Problem p02850 (python)

Fixed submission:
from collections import deque

def main():
    N = int(input())
    path = [[] for _ in range(N)]
    ab = []
    ans = {}
    visited = [False] * N
    visited[0] = True
    for _ in range(N-1):
        a, b = map(int, input().split())
        path[a-1].append(b-1)
        path[b-1].append(a-1)
        # ab.append(str(a-1)+str(b-1))
        ab.append((a-1, b-1))
    
    que = deque([(0, 0)])
    while que:
        # k = 1
        k = 0
        pre, c = que.popleft()
        for next_node in path[pre]:
            if visited[next_node]:
                continue
            k += 1
            if c == k:
                k += 1
            visited[next_node] = True
            # ans[str(pre)+str(next_node)] = k
            ans[(pre, next_node)] = k
            que.append((next_node, k))
            

    print(max(ans.values()))
    for key in ab:
        print(ans[key])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    N = int(input())
    # path = [[] for _ in range(N)]
    path = [[] for _ in range(N-1)]
    ab = []
    ans = {}
    visited = [False] * N
    visited[0] = True
    for _ in range(N-1):
        a, b = map(int, input().split())
        path[a-1].append(b-1)
        # path[b-1].append(a-1)
        ab.append((a-1, b-1))
    
    que = deque([(0, 0)])
    while que:
        k = 1
        pre, c = que.popleft()
        for next_node in path[pre]:
            if visited[next_node]:
                continue
            
            if c == k:
                k += 1
            visited[next_node] = True
            ans[(pre, next_node)] = k
            que.append((next_node, k))
            k += 1
            

    print(max(ans.values()))
    for key in ab:
        print(ans[key])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    N = int(input())
    edges = [[] for _ in range(N)]
    ab = []
    ans = {}
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a-1].append(b-1)
        edges[b-1].append(a-1)
        ab.append(str(a-1)+str(b-1))
    
    K = max([len(g) for g in edges])
    init = [[0, node, i+1] for i, node in enumerate(edges[0])]  # [parent, current, color]
    que = deque(init)
    for _, _, i in init:
        ans['0'+str(i)] = i
    while que:
        c = 1
        p, node, color = que.popleft()
        next_nodes = edges[node]        
        for next_node in next_nodes:
            if next_node == p:
                continue
            if c == color:
                c += 1
            ans[str(node)+str(next_node)] = c
            que.append([node, next_node, c])
            c += 1

    print(K)
    for key in ab:
        print(ans[key])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    N = int(input())
    edges = [[] for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a-1].append(b-1)
    
    K = max([len(g) for g in edges[1:]])
    K = max(K+1, len(edges[0]))
    init = [[node, i+1] for i, node in enumerate(edges[0])]
    que = deque(init)
    colors = set([i for i in range(1, K+1)])
    ans = [i for n, i in init]
    while que:
        node, color = que.popleft()
        if node >= N-1:
            continue

        next_nodes = edges[node]
        next_colors = list(colors - set([color]))
        
        for i in range(len(next_nodes)):
            ans.append(next_colors[i])
            que.append([next_nodes[i], next_colors[i]])

    print(K)
    print('\n'.join(list(map(str, ans))))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    N = int(input())
    edges = [[] for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a-1].append(b-1)
    
    K = max([len(g) for g in edges[1:]])
    K = max(K+1, len(edges[0]))
    init = [[node, i+1] for i, node in enumerate(sorted(edges[0]))]
    que = deque(init)
    colors = set([i for i in range(1, K+1)])
    ans = [i for n, i in init]
    while que:
        node, color = que.popleft()
        if node >= N-1:
            continue

        next_nodes = edges[node]
        next_nodes.sort()
        next_colors = list(colors - set([color]))
        
        for i in range(len(next_nodes)):
            ans.append(next_colors[i])
            que.append([next_nodes[i], next_colors[i]])

    print(K)
    print('\n'.join(list(map(str, ans))))

if __name__ == '__main__':
    main()