Skip to content

Commit 241f2a3

Browse files
committed
[BOJ] 특정 거리의 도시 찾기 / 실버2 / 30분
https://www.acmicpc.net/problem/18352
1 parent 406675a commit 241f2a3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from collections import deque
2+
import sys
3+
4+
f = sys.stdin.readline
5+
6+
def bfs(visited, graph,k,x):
7+
visited[x] = 0
8+
queue = deque([x])
9+
answer = []
10+
11+
while queue:
12+
current_city = queue.popleft()
13+
for next_city in graph[current_city]:
14+
if visited[next_city] == -1:
15+
queue.append(next_city)
16+
visited[next_city] = visited[current_city] + 1
17+
if visited[next_city] == k:
18+
answer.append(next_city)
19+
20+
if not answer:
21+
print(-1)
22+
else:
23+
answer.sort()
24+
for ans in answer:
25+
print(ans)
26+
27+
28+
n,m,k,x = map(int, f().split())
29+
30+
graph = [[] for _ in range(n+1)]
31+
32+
33+
for i in range(m):
34+
a,b = map(int, f().split())
35+
graph[a].append(b)
36+
37+
visited = [-1] * (n+1)
38+
39+
bfs(visited, graph,k,x)

0 commit comments

Comments
 (0)