Problem p02991 (python)

Fixed submission:
n, m = map(int, input().split())
d = {i+1 : [] for i  in range(n)}
for _ in range(m):
    u, v = map(int, input().split())
    d[u].append(v)

visited1 = [False for i in range(n+1)]
visited2 = [False for i in range(n+1)]
visited3 = [False for i in range(n+1)]

s, t = map(int, input().split())
q = set([s])
c = 1
def solve():
    global visited1, visited2,visited3,  c, q
    while True:
        q2 = list(q)
        q = set([])
        for v in q2:
            for e1 in d[v]:
                if visited1[e1]:
                    continue
                visited1[e1] = True
                for e2 in d[e1]:
                    if visited2[e2]:
                        continue
                    visited2[e2] = True
                    for e3 in d[e2]:
                        if visited3[e3]:
                            continue
                        visited3[e3] = True
                        if e3 == t:
                            return c
                        else:
                            q.add(e3)
        if not len(q):
            return -1
        c += 1
print(solve())

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
d = {i+1 : [] for i  in range(n)}
for _ in range(m):
    u, v = map(int, input().split())
    d[u].append(v)
s, t = map(int, input().split())
q = [s]
visited = set([s])
c = 1
def solve():
    while True:
        global visited, c
        flag = True
        for i, v in enumerate(q[:]):
            q.pop(i)
            es = [e3 for e1 in d[v] for e2 in d[e1] for e3 in d[e2] if not e3 in visited]
            if len(es):
                flag = False
            if t in es:
                return c
            else:
                visited |= set(es)
                q.extend(es)
        if flag:
            return -1
        c += 1
print(solve())

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
d = {i+1 : [] for i  in range(n)}
for _ in range(m):
    u, v = map(int, input().split())
    d[u].append(v)
s, t = map(int, input().split())
q = [s]
visited = set([s])
c = 1
def solve():
    while True:
        global visited, c
        flag = True
        for i, v in enumerate(q[:]):
            q.pop(i)
            es = [e3 for e1 in d[v] for e2 in d[e1] for e3 in d[e2] if not e3 in visited]
            if len(es):
                flag = False
            if t in es:
                return c
            else:
                visited |= set(es)
                q.extend(es)
        if flag:
            return -1
        c += 1
print(solve())

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
d = {i+1 : [] for i  in range(n)}
for _ in range(m):
    u, v = map(int, input().split())
    d[u].append(v)
s, t = map(int, input().split())
q = [s]
visited = set([s])
def calc(c):
    global visited
    flag = True
    for i, v in enumerate(q[:]):
        q.pop(i)
        es = [e3 for e1 in d[v] for e2 in d[e1] for e3 in d[e2] if not e3 in visited]
        if len(es):
            flag = False
        if t in es:
            return c
        else:
            visited |= set(es)
            q.extend(es)
    if flag:
        return -1
    return calc(c + 1)
print(calc(1))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
d = {i+1 : [] for i  in range(n)}
for _ in range(m):
    u, v = map(int, input().split())
    d[u].append(v)
s, t = map(int, input().split())
q = [s]
visited = set([s])
def calc(c):
    global visited
    if len(q) == 0:
        return -1
    for i, v in enumerate(q[:]):
        q.pop(i)
        es = [e3 for e1 in d[v] for e2 in d[e1] for e3 in d[e2] if not e3 in visited]
        if t in es:
            return c
        else:
            visited |= set(es)
            q.extend(es)
    return calc(c + 1)
print(calc(1))