from collections import deque
def bfs(node):
a=deque([node]) # 초기 노드를 큐에 push
state[node] = 1 # 방문 처리
while len(a): # 큐가 빌때까지
node = a.popleft() # 큐에서 처음 들어온 노드 제거
ans_2.append(node) # 제거 후 출력 순서를 보여주기 위해 ans에 방문된 node 추가
for nxt in graph[node]: # 관련된 다른 node
if state[nxt]==0: # 방문하지 않은 노드일 경우
a.append(nxt) # 큐에 추가하고
state[nxt]=1 # 방문 처리
n,m = map(int, input().split()) # n은 노드 개수, m은 간선
graph =[[] for i in range(n+1)] #
ans_2=[]
for i in range(m):
a,b=map(int,input().split())
graph[a].append(b)
graph[b].append(a)
state = [0 for i in range(n+1)]
bfs(1) # 탐색
print(*ans_2) # 결과 출력
댓글남기기