Problem p02564 (python)

Fixed submission:
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8[:, :]), cache=True)
def scc(N, G):
    ind = np.searchsorted(G[:, 0], np.arange(N + 2))
    preorder = np.empty(N, np.int64)
    n = 0
    st = np.empty(N, np.int64)
    p = 0
    nxt = np.full(N + 1, -1, np.int64)

    for root in range(N):
        if nxt[root] != -1:
            continue
        st[p], p = root, p + 1
        while p:
            v = st[p - 1]
            if nxt[v] == -1:
                nxt[v] = ind[v]
            if nxt[v] == ind[v + 1]:
                preorder[n], n = v, n + 1
                p -= 1
                continue
            w = G[nxt[v], 1]
            nxt[v] += 1
            if nxt[w] != -1:
                continue
            st[p], p = w, p + 1
    Grev = G[:, :2][:, ::-1].copy()
    Grev = Grev[np.argsort(Grev[:, 0])]
    ind = np.searchsorted(Grev[:, 0], np.arange(N + 2))
    comp = np.zeros(N, np.int64)
    n_comp = 0
    for root in preorder[::-1]:
        if comp[root]:
            continue
        n_comp += 1
        comp[root] = n_comp
        st[p], p = root, p + 1
        while p:
            v, p = st[p - 1], p - 1
            for i in range(ind[v], ind[v + 1]):
                w = Grev[i, 1]
                if comp[w]:
                    continue
                comp[w] = n_comp
                st[p], p = w, p + 1
    return n_comp, comp

def main(N, G):
    n_comp, comp = scc(N, G)
    ans = np.empty((N, 2), np.int64)
    ans[:, 0] = np.arange(N)
    ans[:, 1] = comp
    ans = ans[np.argsort(ans[:, 1])]
    idx = np.searchsorted(ans[:, 1], np.arange(n_comp + 2))
    print(n_comp)
    for c in range(1, n_comp + 1):
        V = ans[idx[c]:idx[c + 1], 0]
        print(len(V), ' '.join(map(str, V)))

N, M = map(int, readline().split())
AB = np.array(read().split(), np.int64).reshape(-1, 2)

AB = AB[np.argsort(AB[:, 0])]

main(N, AB)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8[:, :], i8[:], i8[:], i8[:], i8[:], i8[:], i8, i8, i8, i8),
      cache=True)
def scc_dfs(N, G, idx, low, ord, ids, visited, now_ord, group_num, vis_i, v):
    low[v] = ord[v] = now_ord
    now_ord += 1
    visited[vis_i], vis_i = v, vis_i + 1
    for e in range(idx[v], idx[v + 1]):
        to = G[e, 1]
        if ord[to] == -1:
            now_ord, group_num, vis_i = \
                scc_dfs(N, G, idx, low, ord, ids,
                        visited, now_ord, group_num,
                        vis_i, to)
            low[v] = min(low[v], low[to])
        else:
            low[v] = min(low[v], ord[to])
    if low[v] == ord[v]:
        while True:
            u, vis_i = visited[vis_i - 1], vis_i - 1
            ord[u] = N
            ids[u] = group_num
            if u == v:
                break
        group_num += 1
    return now_ord, group_num, vis_i


@njit((i8, i8[:, :]), cache=True)
def scc(N, G):
    idx = np.searchsorted(G[:, 0], np.arange(N + 1))
    low = np.zeros(N, np.int64)
    ord = np.zeros(N, np.int64) - 1
    now_ord = 0
    group_num = 0
    visited, vis_i = np.empty(N, np.int64), 0
    ids = np.zeros(N, np.int64)
    for v in range(N):
        if ord[v] == -1:
            now_ord, group_num, vis_i = \
                scc_dfs(N, G, idx, low, ord, ids,
                        visited, now_ord, group_num,
                        vis_i, v)
    return group_num, group_num - ids - 1

@njit((i8, i8, i8[:]), cache=True)
def main(N, D, XY):
    # X を True として、two sat に帰着する
    X, Y = XY[::2], XY[1::2]
    G, g = np.empty((4 * N * N, 2), np.int64), 0
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if abs(X[i] - X[j]) < D:
                # i True implies j False
                G[g], g = (i, j + N), g + 1
            if abs(X[i] - Y[j]) < D:
                # i True implies j True
                G[g], g = (i, j), g + 1
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if abs(Y[i] - X[j]) < D:
                # i False implies j False
                G[g], g = (i + N, j + N), g + 1
            if abs(Y[i] - Y[j]) < D:
                # i False implies j True
                G[g], g = (i + N, j), g + 1
    G = G[:g]
    n_comp, comp = scc(N + N, G)
    ans = np.empty(N, np.int64)
    for i in range(N):
        if comp[i] < comp[i + N]:
            ans[i] = Y[i]
        elif comp[i] > comp[i + N]:
            ans[i] = X[i]
        else:
            print('No')
            return
    print('Yes')
    for x in ans:
        print(x)

N, D = map(int, readline().split())
XY = np.array(read().split(), np.int64)

main(N, D, XY)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8[:, :], i8[:], i8[:], i8[:], i8[:], i8[:], i8, i8, i8, i8),
      cache=True)
def scc_dfs(N, G, idx, low, ord, ids, visited, now_ord, group_num, vis_i, v):
    low[v] = ord[v] = now_ord
    now_ord += 1
    visited[vis_i], vis_i = v, vis_i + 1
    for e in range(idx[v], idx[v + 1]):
        to = G[e, 1]
        if ord[to] == -1:
            now_ord, group_num, vis_i = \
                scc_dfs(N, G, idx, low, ord, ids,
                        visited, now_ord, group_num,
                        vis_i, to)
            low[v] = min(low[v], low[to])
        else:
            low[v] = min(low[v], ord[to])
    if low[v] == ord[v]:
        while True:
            u, vis_i = visited[vis_i - 1], vis_i - 1
            ord[u] = N
            ids[u] = group_num
            if u == v:
                break
        group_num += 1
    return now_ord, group_num, vis_i


@njit((i8, i8[:, :]), cache=True)
def scc(N, G):
    idx = np.searchsorted(G[:, 0], np.arange(N + 1))
    low = np.zeros(N, np.int64)
    ord = np.zeros(N, np.int64) - 1
    now_ord = 0
    group_num = 0
    visited, vis_i = np.empty(N, np.int64), 0
    ids = np.zeros(N, np.int64)
    for v in range(N):
        if ord[v] == -1:
            now_ord, group_num, vis_i = \
                scc_dfs(N, G, idx, low, ord, ids,
                        visited, now_ord, group_num,
                        vis_i, v)
    return group_num, group_num - ids - 1

@njit((i8, i8, i8[:]), cache=True)
def main(N, D, XY):
    # X を True として、two sat に帰着する
    X, Y = XY[::2], XY[1::2]
    G, g = np.empty((4 * N * N, 2), np.int64), 0
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if abs(X[i] - X[j]) < D:
                # i True implies j False
                G[g], g = (i, j + N), g + 1
            if abs(X[i] - Y[j]) < D:
                # i True implies j True
                G[g], g = (i, j), g + 1
    for i in range(N):
        for j in range(N):
            if abs(Y[i] - X[j]) < D:
                # i False implies j False
                G[g], g = (i + N, j + N), g + 1
            if abs(Y[i] - Y[j]) < D:
                # i False implies j True
                G[g], g = (i + N, j), g + 1
    G = G[:g]
    n_comp, comp = scc(N + N, G)
    ans = np.empty(N, np.int64)
    for i in range(N):
        if comp[i] < comp[i + N]:
            ans[i] = Y[i]
        elif comp[i] > comp[i + N]:
            ans[i] = X[i]
        else:
            print('No')
            return
    print('Yes')
    for x in ans:
        print(x)

N, D = map(int, readline().split())
XY = np.array(read().split(), np.int64)

main(N, D, XY)