Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import heapq
# import time

n, m = list(map(int, input().split()))
a = [0] * m
b = [0] * m
path = [[] for i in range(n+1)]

for i in range(m):
    # a[i], b[i] = list(map(int, input().split()))
    a, b = list(map(int, input().split()))
    path[a].append(b)
    path[b].append(a)
# start = time.time()
# 部屋1から出発して、
ans = [0]*(n+1)
visited = [1]
que = deque([[1, 1]])  # 今の場所、前の場所
que_items = [1]
now = 1
prev = 1
# BFS
while len(que) > 0:
    now, prev = que.popleft()
    # print('pop', now)
    for can_go in path[now]:
        if(not (can_go in visited or can_go in que_items)):
            # print('push', can_go)
            que.append([can_go, now])
            que_items.append(can_go)
    if (now in visited):
        continue
    ans[now] = prev
    visited.append(now)

if (len(visited) != n):
    print('No')
else:
    print('Yes')
    for _ans in ans[2:]:
        print(_ans)

# print(time.time() - start)


Fixed submission:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import heapq
# import time

n, m = list(map(int, input().split()))
a = [0] * m
b = [0] * m
path = [[] for i in range(n+1)]

for i in range(m):
    # a[i], b[i] = list(map(int, input().split()))
    a, b = list(map(int, input().split()))
    path[a].append(b)
    path[b].append(a)
# start = time.time()
# 部屋1から出発して、
ans = [0]*(n+1)
visited = []
que = deque([[1, 1]])  # 今の場所、前の場所
que_items = set([1])

# BFS
while len(que) > 0:
    now, prev = que.popleft()
    # print('pop', now)
    for can_go in path[now]:
        if(not (can_go in que_items)):  # ここが遅い
            # print('push', can_go)
            que.append([can_go, now])
            que_items.add(can_go)
    ans[now] = prev
    visited.append(now)
    # print(visited)

if (len(visited) != n):
    print('No')
else:
    print('Yes')
    for _ans in ans[2:]:
        print(_ans)

# print(time.time() - start)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import heapq

n, m = list(map(int, input().split()))
a = [0] * m
b = [0] * m
path = [[] for i in range(n+1)]

for i in range(m):
    # a[i], b[i] = list(map(int, input().split()))
    a, b = list(map(int, input().split()))
    path[a].append(b)
    path[b].append(a)

# 部屋1から出発して、
ans = [0]*(n+1)
visited = [1]
que = deque([[1, 1]])  # 今の場所、前の場所
# que_items = [1]
now = 1
prev = 1
# BFS
while (len(visited) != n) and len(que) > 0:
    now, prev = que.popleft()
    # print('pop', now)
    for can_go in path[now]:
        if(not (can_go in visited)):
            # print('push', can_go)
            que.append([can_go, now])
    if (now in visited):
        continue
    ans[now] = prev
    visited.append(now)

if 0 in ans[2:]:
    print('No')
else:
    print('Yes')
    for _ans in ans[2:]:
        print(_ans)


Fixed submission:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import heapq
# import time

n, m = list(map(int, input().split()))
a = [0] * m
b = [0] * m
path = [[] for i in range(n+1)]

for i in range(m):
    # a[i], b[i] = list(map(int, input().split()))
    a, b = list(map(int, input().split()))
    path[a].append(b)
    path[b].append(a)
# start = time.time()
# 部屋1から出発して、
ans = [0]*(n+1)
visited = []
que = deque([[1, 1]])  # 今の場所、前の場所
que_items = set([1])

# BFS
while len(que) > 0:
    now, prev = que.popleft()
    # print('pop', now)
    for can_go in path[now]:
        if(not (can_go in que_items)):  # ここが遅い
            # print('push', can_go)
            que.append([can_go, now])
            que_items.add(can_go)
    ans[now] = prev
    visited.append(now)
    # print(visited)

if (len(visited) != n):
    print('No')
else:
    print('Yes')
    for _ans in ans[2:]:
        print(_ans)

# print(time.time() - start)