18352 : 특정 거리의 도시 찾기

서희찬·2022년 2월 18일
0

백준

목록 보기
103/105
post-thumbnail

문제

코드

from collections import deque
import sys 
input = sys.stdin.readline
n,m,k,x = map(int,input().split())

result = [-1]*(n+1)
result[x] = 0 #시작점 
path = [[] for _ in range(n+1)]

for _ in range(m):
    a,b = map(int,input().split())
    path[a].append(b) #간선 연결 

queue = deque([x])
while queue:
    now = queue.popleft()
    for nextcity in path[now]:
        if result[nextcity] == False :
            result[nextcity] = result[now]+1
            queue.append(nextcity)

for i in range(n+1):
    if result[i]==k:
        print(i)
#없으면        
if k not in result:
    print(-1)

해설

-1로 초기화하고 갈때마다 1씩 증가시켜준다

profile
부족한 실력을 엉덩이 힘으로 채워나가는 개발자 서희찬입니다 :)

0개의 댓글