문제
코드
from collections import deque
import sys
input = sys.stdin.readline
def bfs(graph: list, visited: list, start: int):
global visited_order
queue = deque()
queue.append(start)
visited[start] = visited_order
while queue:
now = queue.popleft()
for v in graph[now]:
if not visited[v]:
visited_order += 1
visited[v] = visited_order
queue.append(v)
if __name__ == '__main__':
n, m, r = map(int, input().split())
graph = [[] for _ in range(n+1)]
visited = [0] * (n+1)
visited_order = 1
for _ in range(m):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
for x in range(n+1):
graph[x].sort(reverse=True)
bfs(graph, visited, r)
for x in visited[1:]:
print(x)
결과
출처 & 깃허브
boj 24445
github