Problem p02991 (python)

Fixed submission:
N, M = map(int, input().split())
nodes = [[] for i in range(N)]

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    nodes[u].append(v)

s, t = map(int, input().split())
s -= 1
t -= 1

num = s
step = 0
q = [[num, step]]
check = [[False for i in range(N)] for j in range(3)]
ok = False
while len(q) > 0:
    num, step = q.pop(0)
    tmp = step % 3
    if tmp == 0 and num == t:
        print(step//3)
        ok = True
        break
    for next in nodes[num]:
        if not check[tmp][next]:
            check[tmp][next] = True
            q.append([next, step+1])
if not ok:
    print(-1)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, i):
        self.num = i
        self.ken = set()
        self.kenken = set()
        self.kenkenpa = set()

N, M = map(int, input().split())
nodes = [Node(i+1) for i in range(N)]

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    nodes[v].ken.add(nodes[u])

s, t = map(int, input().split())
s -= 1
t -= 1

for i in range(N):
    for pre in nodes[i].ken:
        nodes[i].kenken = nodes[i].kenken | pre.ken

for i in range(N):
    for prepre in nodes[i].kenken:
        nodes[i].kenkenpa = nodes[i].kenkenpa | prepre.ken

node = nodes[t]
step = 0
check_list = [[node, step]]
check = [False] * N
ok = False
while len(check_list) > 0:
    node, step = check_list.pop(0)
    for pre in node.kenkenpa:
        if pre == nodes[s]:
            print(step+1)
            ok = True
            break
        if not check[pre.num]:
            check[pre.num] = True
            check_list.append([pre, step+1])
    if ok:
        break
if not ok:
    print(-1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N, M = map(int, input().split())

next = np.zeros((N, N))

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    next[u][v] = 1

s, t = map(int, input().split())
s -= 1
t -= 1

next = np.dot(next, np.dot(next, next))
for i in range(N):
    next[i][i] = 0

now = t
step = 0
check_list = [[now, step]]
ok = False
check = [False] * N
check[now] = True
while len(check_list) > 0:
    now, step = check_list.pop(0)
    for i in range(N):
        if next[i][now] > 0:
            if not check[i]:
                if i == s:
                    print(step+1)
                    # print(check)
                    ok = True
                    break
                check_list.append([i, step+1])
                check[i] = True
    if ok:
        break
if not ok:
    print(-1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N, M = map(int, input().split())

next = np.zeros((N, N))

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    next[u][v] = 1

s, t = map(int, input().split())
s -= 1
t -= 1

next = np.dot(next, np.dot(next, next))
for i in range(N):
    next[i][i] = 0

now = t
step = 0
check_list = [[now, step]]
ok = False
check = [False] * N
while len(check_list) > 0:
    now, step = check_list.pop(0)
    if now == s:
        print(step)
        ok = True
        break
    for i in range(N):
        if next[i][now] > 0:
            if not check[i]:
                check_list.append([i, step+1])
                check[i] = True
if not ok:
    print(-1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N, M = map(int, input().split())

next = np.zeros((N, N))

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    next[u][v] = 1

s, t = map(int, input().split())
s -= 1
t -= 1

next = np.dot(next, np.dot(next, next))
for i in range(N):
    next[i][i] = 0

now = t
step = 0
hist = [t]
check_list = [[now, step, hist]]
ok = False
while len(check_list) > 0:
    now, step, tmp = check_list.pop(0)
    hist = tmp
    if now == s:
        print(step)
        ok = True
        break
    for i in range(N):
        if next[i][now] > 0:
            check_list.append([i, step+1, hist+[i]])
if not ok:
    print(-1)

Buggy submission (verdict: Runtime Error):
import copy

class Node:
    def __init__(self, i):
        self.num = i
        self.goto = []
        self.kenken = set()

N, M = map(int, input().split())
nodes = [Node(i+1) for i in range(N)]

for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    nodes[v].goto.append(nodes[u])

s, t = map(int, input().split())
s -= 1
t -= 1

for i in range(N):
    for next in nodes[i].goto:
        for nextnext in next.goto:
            nodes[i].kenken = nodes[i].kenken | set(nextnext.goto)
    if nodes[i] in nodes[i].kenken:
        nodes[i].kenken.remove(nodes[i])

node = nodes[t]
step = 0
prev = []
check_list = [[node, step, prev]]
ok = False
while len(check_list) > 0:
    node, step, tmp = check_list.pop(0)
    prev = copy.deepcopy(tmp)
    if node == nodes[s]:
        print(step)
        ok = True
        break
    for next in node.kenken:
        if next not in prev:
            check_list.append([next, step+1, prev+[next]])
if not ok:
    print(-1)