728x90
문제 링크
https://www.acmicpc.net/problem/18352
코드
# https://www.acmicpc.net/problem/18352
from collections import deque
import sys
f = sys.stdin.readline
n, m, k, x = map(int, f().split())
graph = [[] for _ in range(n+1)]
distance = [0] * (n+1)
visited = [False] * (n+1)
for _ in range(m):
a, b = map(int, f().split())
graph[a].append(b)
def bfs(start):
answer = []
q = deque([start])
visited[start] = True
distance[start] = 0
while q:
now = q.popleft()
for i in graph[now]:
if not visited[i]:
visited[i] = True
q.append(i)
distance[i] = distance[now] + 1
if distance[i] == k:
answer.append(i)
if len(answer) == 0:
print(-1)
else:
answer.sort()
for i in answer:
print(i, end='\n')
bfs(x)
728x90
'코딩테스트' 카테고리의 다른 글
BFS&DFS_심화17) 경쟁적 전염 _ python (0) | 2023.09.25 |
---|---|
BFS&DFS_심화16) 연구소 _ python (0) | 2023.09.25 |
백트래킹(Backtracking) (1) | 2023.09.25 |
3주차 - DFS/BFS (0) | 2023.09.25 |
구현_심화13) 치킨 배달 _ python (0) | 2023.09.25 |