-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18352번.py
More file actions
37 lines (31 loc) · 717 Bytes
/
18352번.py
File metadata and controls
37 lines (31 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#특정 거리의 도시 찾기
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 = []
visited = [-1] * (N+1)
def BFS(v):
queue = deque()
queue.append(v)
visited[v] += 1
while queue:
nowNode = queue.popleft()
for i in A[nowNode]:
if visited[i] == -1:
visited[i] = visited[nowNode] + 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):
if visited[i] == K:
answer.append(i)
if not answer:
print(-1)
else:
answer.sort()
for i in answer:
print(i)