nguyentrungkiez9
Rating
-
Bài tập
1
Điểm
60
Rating #
-
Điểm #
28698
Giới thiệu
from collections import defaultdict, deque
n , m, s, t = list(map(int,input().split()))
lane = defaultdict(list)
for _ in range(m):
u,v = map(int,input().split())
lane[u].append(v)
trace = {s:None}
visited = set()
def bfs(start,end,lane):
queue = deque([start])
visited.add(start)
while queue:
cur = queue.popleft()
if cur == end:
return traceback(t,trace)
for neighbor in lane[cur]:
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
trace[neighbor] = cur
def traceback(t,trace):
path = []
current = t
while current is not None:
path.append(current)
current = trace[current]
return path
kq = bfs(s,t,lane)
for i in kq[::-1]:
print(i,end=" ")