[백준] 24445 - 너비 우선 탐색 2
2024. 6. 26. 17:05ㆍPS/백준
728x90
문제 링크
https://www.acmicpc.net/problem/24445
import sys
import collections
def bfs(startV):
global visited_order
queue = collections.deque([startV])
visited[startV] = visited_order
while queue:
q = queue.popleft()
for v in graph[q]:
if visited[v] == 0:
queue.append(v)
visited_order += 1
visited[v] = visited_order
N, M, R = map(int, sys.stdin.readline().split())
graph = collections.defaultdict(list)
visited = [0] * (N+1)
visited_order = 1
for _ in range(M):
u, v = map(int, sys.stdin.readline().split())
graph[u].append(v)
graph[v].append(u)
for k in graph.keys():
graph[k] = sorted(graph[k], reverse = True)
bfs(R)
for ind in range(1, N+1):
print(visited[ind])
'PS > 백준' 카테고리의 다른 글
[백준] 24480 - 깊이 우선 탐색 2 (0) | 2024.06.26 |
---|---|
[백준] 24479 - 깊이 우선 탐색 1 (0) | 2024.06.26 |
[백준] 2193 - 이친수 (0) | 2024.06.25 |
[백준] 11726 - 2×n 타일링 (0) | 2024.06.25 |
[백준] 9095 - 1, 2, 3 더하기 (0) | 2024.06.25 |