Fastest Python Solution


#1

class Solution:
def solve(self, A, B):
visited=[False]*(A+1)
g=[[] for i in range(A+1)]
for i in B:
u=i[0]
v=i[1]
g[u].append(v)
q = []
q.append(1)
visited[1]=True
while len(q):
temp=q.pop(0)
if temp==A:
return 1
for adj in g[temp]:
if not visited[adj]:
q.append(adj)
visited[adj]=True
return 0