-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from wooseokCho/jihye
Q046
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from collections import deque | ||
|
||
input = sys.stdin.readline | ||
|
||
N,M,K,X = map(int,input().split()) | ||
A = [[] for _ in range(N+1)] #[[],[],[],[],[]] | ||
answer = [] | ||
vistited = [-1] *N+1 #시작노드는 0 이므로 -1로 초기화 | ||
|
||
def BFS(v): | ||
queue = deque() | ||
queue.append(v) # 시작노드 삽입 | ||
vistited[v] += 1 # 현재노드 방문 기록 | ||
while queue: | ||
now_Node = queue.popleft() | ||
|
||
for i in A[now_Node]: | ||
if vistited[i] == -1: | ||
vistited[i] = vistited[now_Node] +1 | ||
queue.append(i) | ||
|
||
|
||
for _ in range(M): | ||
S,E = map(int ,input().split()) | ||
A[S].append(E) | ||
|
||
BFS(X) | ||
|
||
for i in range(N+1): #방문 거리가 K인 노드의 숫자를 정답 리스트에 더하기 | ||
if vistited[i] == K: | ||
answer.append(i) | ||
|
||
if not answer: | ||
print(-1) | ||
else: # 정답 리스트 오른차순 정렬 후 순차 출력 | ||
answer.sort() | ||
for i in answer: | ||
print(i) |