p. 339
BFS
import sys
from collections import deque
input=sys.stdin.readline
n,m,k,x=map(int, input().split())
graph=[[] for _ in range(n+1)]
for i in range(m):
a,b=map(int, input().split())
graph[a].append(b)
visited=[-1]*(n+1)
visited[x]=0
def bfs(_x):
queue=deque()
queue.append(_x)
while queue:
now = queue.popleft()
for i in graph[now]:
if visited[i]==-1:
queue.append(i)
visited[i]=visited[now]+1
result=0
for i in range(1, n+1):
if visited[i]==k:
print(i)
result+=1
if result==0:
print('-1')
bfs(x)
input().split()로 하면 시간 초과가 난다.
sys.stdin.readline을 사용하자
댓글