Problem p02850 (python)

Fixed submission:
# 2019-11-24 20:59:47(JST)
import sys

def main():
    n = int(sys.stdin.readline().rstrip())
    m = map(int, sys.stdin.read().split())
    ab = list(zip(m, m))

    graph = [[] for _ in range(n + 1)]
    for a, b in ab:
        graph[a].append(b)
        graph[b].append(a)

    root = 1
    parent = [0] * (n + 1)
    order = []
    stack = [root]

    while stack:
        x = stack.pop()
        order.append(x)
        for y in graph[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            stack.append(y)

    color = [-1] * (n + 1)
    for x in order:
        ng = color[x]
        c = 1
        for y in graph[x]:
            if y == parent[x]:
                continue
            if c == ng:
                c += 1
            color[y] = c
            c += 1

    res = []
    for a, b in ab:
        if parent[a] == b:
            res.append(color[a])
        else:
            res.append(color[b])

    print(max(res))
    print('\n'.join(map(str, res)))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
import numpy as np

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(-1, 2)

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    parent = np.zeros(n + 1, dtype=np.int64)
    color = np.zeros((n + 1, n + 1), dtype=np.int64)
    root = 1
    ng = None
    q = deque()
    q.append((root, ng))

    while q:
        x, ng = q.popleft()
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if c == ng:
                c += 1
            color[x][y] = c
            color[y][x] = c
            q.append((y, c))
            c += 1

    print(np.amax(color))
    for a, b in ab:
        print(color[a][b])
            
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
import numpy as np

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = np.array(sys.stdin.read().split(), dtype=np.int32).reshape(-1, 2)

    G = [[] for _ in range(n + 1)]
    for a, b in ab:
        G[a].append(b)
        G[b].append(a)
    
    parent = np.zeros(n + 1, dtype=np.int32)
    color = np.zeros((n + 1, n + 1), dtype=np.int32)
    root = 1
    ng = None
    q = deque()
    q.append((root, ng))

    while q:
        x, ng = q.popleft()
        c = 1
        for y in G[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            if c == ng:
                c += 1
            color[x][y] = c
            color[y][x] = c
            q.append((y, c))
            c += 1

    print(np.amax(color))
    for a, b in ab:
        print(color[a][b])
            
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# 2019-11-24 20:59:47(JST)
import sys
# import numpy as np
import collections
import copy

def main():
    n = int(sys.stdin.readline().rstrip())
    ab = map(int, sys.stdin.read().split())
    ab = zip(ab, ab)
    A = copy.deepcopy(ab)
    graph = collections.defaultdict(list)

    for a, b in ab:
        graph[a].append(b)
        graph[b].append(a)

    needed_colors = max(len(graph[i]) for i in range(1, n+1))

    res = collections.defaultdict(dict)

    number = 1
    for a, v in graph.items():
        for b in v:
            if res[a]:
                values = res[a].values()
                while number in values:
                    number += 1
                    if number == needed_colors + 1:
                        number = 1
                continue
            res[a][b] = number
            res[b][a] = number
            number += 1
            if number == needed_colors + 1:
                        number = 1

    print(needed_colors)
    
    for a, b in A:
        print(res[a][b])


if __name__ == '__main__':
    main()