Skip to content

Commit

Permalink
Merge pull request #129 from wooseokCho/jihye
Browse files Browse the repository at this point in the history
Q046
  • Loading branch information
jihye9549 authored Mar 25, 2024
2 parents 5558668 + a0c0ce4 commit 26f5438
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 지혜/DoIt/PYTHON/Q046.py
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)

0 comments on commit 26f5438

Please sign in to comment.