Problem p02991 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, M: int, G, S: int, T: int):
    S -= 1
    T -= 1
    now = deque([S])
    dist = [0 for _ in range(N * 3)]
    while now:
        p = now.popleft()
        for n in G[p]:
            if dist[n] != 0:
                continue
            dist[n] = dist[p] + 1
            if n == T:
                print(dist[n] // 3)
                exit()
            now.append(n)
    print(-1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    G = tuple(set() for _ in range(N * 3))
    for _ in range(M):
        u, v = int(next(tokens)) - 1, int(next(tokens))-1
        G[u].add(v + N)
        G[u + N].add(v + 2 * N)
        G[u + 2 * N].add(v)

    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, G, S, T)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import numpy as np
from collections import deque

def solve(N: int, M: int, edges, S: int, T: int):
    S -= 1
    T -= 1
    now = [S]
    seen = [False for _ in range(N)]
    c = 0
    while now:
        c += 1
        next = []
        for n in now:
            seen[n] = True
            for nz in edges[n].nonzero()[0]:
                if nz == T:
                    print(c)
                    exit()
                if not seen[nz]:
                    next.append(nz)
        now = next
    print(-1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Mat = np.zeros((N, N))
    for _ in range(M):
        u, v = int(next(tokens)) - 1, int(next(tokens))-1
        Mat[u][v] = 1
        #Mat[v][u] = 1


    Three = np.copy(Mat)
    for _ in range(2):
        Three = Three.dot(Mat)

    #print(Three)

    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, Three, S, T)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, M: int, edges, S: int, T: int):
    S -= 1
    T -= 1
    now = np.zeros(10**5)
    now[S] = 1
    seen = now > 0
    c = 0
    s = 0
    while s < np.count_nonzero(seen):
        c += 1
        s = np.count_nonzero(seen)
        now = now.dot(edges)
        if now[T]:
            print(c)
            sys.exit(0)
        seen |= (now > 0)
    print(-1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Mat = np.zeros((10**5, 10**5))
    for _ in range(M):
        u, v = int(next(tokens)) - 1, int(next(tokens))-1
        Mat[u][v] = 1
        #Mat[v][u] = 1


    Three = np.copy(Mat)
    for _ in range(2):
        Three = Three.dot(Mat)

    #print(Three)

    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, Three, S, T)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, M: int, edges, S: int, T: int):
    S -= 1
    T -= 1
    now = np.zeros(N)
    now[0] = 1
    seen = now > 0
    c = 0
    s = 0
    while s < np.count_nonzero(seen):
        c += 1
        s = np.count_nonzero(seen)
        now = now.dot(edges)
        if now[T]:
            print(c)
            exit()
        seen |= (now > 0)
    print(-1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Mat = np.zeros((N, N))
    for _ in range(M):
        u, v = int(next(tokens)) - 1, int(next(tokens))-1
        Mat[u][v] = 1
        #Mat[v][u] = 1


    Three = np.copy(Mat)
    for _ in range(2):
        Three = Three.dot(Mat)

    #print(Three)

    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, Three, S, T)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, M: int, edges, S: int, T: int):
    S -= 1
    T -= 1
    now = np.zeros(N)
    now[0] = 1
    seen = now > 0
    c = 0
    s = 0
    while s < np.count_nonzero(seen):
        c += 1
        s = np.count_nonzero(seen)
        now = now.dot(edges)
        if now[T]:
            print(c)
            exit()
        seen |= now > 0
    print(-1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Mat = np.zeros((N, N))
    for _ in range(M):
        u, v = int(next(tokens)) - 1, int(next(tokens))-1
        Mat[u][v] = 1
        #Mat[v][u] = 1


    Three = np.copy(Mat)
    for _ in range(2):
        Three = Three.dot(Mat)

    #print(Three)

    S = int(next(tokens))  # type: int
    T = int(next(tokens))  # type: int
    solve(N, M, Three, S, T)

if __name__ == '__main__':
    main()