from collections import deque
import sys
input = sys.stdin.readline
n,m,k,x = map(int, input().split())
graph = [[] for i in range(n+1)]
state = [-1 for j in range(n+1)]
for i in range(m):
a,b = map(int, input().split()) # 단방향 그래프 구현을 위해 한 노드에서만 갈 수 있도록
graph[a].append(b)
for j in range(len(graph)):
graph[j] = sorted(graph[j]) # 정렬
def bfs(node):
ans = []
a=deque([node])
state[node] = 1 # 스타트를 1로, count로 올리면 순서가 되어버리므로, 현재 노드의 거리를 다음 노드의 +1씩 더해주며 depth 계산
while len(a):
node = a.popleft()
for nxt in graph[node]:
if state[nxt]==-1:
a.append(nxt)
state[nxt]=state[node]+1
if state[nxt]-1==k: # start를 1로 잡았으므로, -1한 값이 최단거리와 동일한 경우
ans.append(nxt) # 정답 리스트에 추가하기
else:
continue
return ans
ans = bfs(x)
ans = sorted(ans) # 오름차순
if len(ans)==0: # return 값이 없다면 -1
print(-1)
else:
for i in ans: # 있다면 출력
print(i)
댓글남기기