문제
코드
from collections import deque
import sys
input = sys.stdin.readline
def bfs(graph: list, visited: list, now: int):
global visited_order
queue = deque()
queue.append(now)
visited[now] = visited_order
while queue:
data = queue.popleft()
for v in graph[data]:
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()
bfs(graph, visited, r)
for x in visited[1:]:
print(x)
결과
출처 & 깃허브
boj 24444
github