Problem p02991 (python)

Fixed submission:
from collections import deque
INF = 10**9

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

G = [[] for _ in range(3*N)]
for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

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

dist = [INF] * (3*N)
dist[s] = 0

Q = deque([s])
while len(Q) > 0:
    u = Q.popleft()
    for v in G[u]:
        if dist[v] == INF:
            dist[v] = dist[u] + 1
            Q.append(v)

if dist[t] == INF:
    print(-1)
else: 
    print(dist[t] // 3)

Buggy submission (verdict: Runtime Error):
def dfs(G,u,dist):
    for v in G[u]:
        if dist[v] == 10**9:
            dist[v] = dist[u] + 1
            dfs(G,v,dist)

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

G = [[] for _ in range(3*N)]
for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

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

dist = [10**9] * (3*N)
dist[s] = 0

dfs(G,s,dist)

if dist[t] == 10**9:
    print(-1)
else: 
    print(dist[t] // 3)

Buggy submission (verdict: Runtime Error):
def dfs(G,u,dist):
    for v in G[u]:
        if dist[v] == 10**9:
            dist[v] = dist[u] + 1
            dfs(G,v,dist)

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

G = [[] for _ in range(3*N)]
for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

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

dist = [10**9] * (3*N)
dist[s] = 0

dfs(G,s,dist)

if dist[t] == 10**9:
    print(-1)
else: 
    print(dist[t] // 3)

Buggy submission (verdict: Runtime Error):
def dfs(u):
    for v in G[u]:
        if dist[v] > dist[u] + 1:
            dist[v] = dist[u] + 1
            dfs(v)

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

G = [[] for _ in range(3*N)]
for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

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

dist = [10**9] * (3*N)
dist[s] = 0

dfs(s)

if dist[t] == 10**9:
    print(-1)
else: 
    print(dist[t] // 3)

Buggy submission (verdict: Runtime Error):
def dfs(G, s, dist):
    for v in G[s]:
        if dist[v] > dist[s] + 1:
            dist[v] = dist[s] + 1
            dfs(G, v, dist)

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

G = [[] for _ in range(3*N)]

for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

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

dist = [10**9] * (3*N)
dist[s] = 0

dfs(G, s, dist)

if dist[t] == 10**9:
    print(-1)
else: 
    print(dist[t] // 3)

Buggy submission (verdict: Runtime Error):
def dfs(G2, s, dist):
    for v in G2[s]:
        if dist[v] > dist[s] + 1:
            dist[v] = dist[s] + 1
            dfs(G2, v, dist)

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

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

G2 = [[] for _ in range(N)]

for u in range(N):
    for v1 in G[u]:
        for v2 in G[v1]:
            for v3 in G[v2]:
                if u != v3 and v3 not in G2[u]:
                    G2[u].append(v3)

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

dist = [10**9] * len(G2)
dist[s-1] = 0

dfs(G2, s-1, dist)

if dist[t-1] == 10**9:
    print(-1)
else: 
    print(dist[t-1])