Buggy python submission (verdict: Time Limit Exceeded):
import math


def bfs_168(n,W):
    work_stack = []
    visited = set()
    check = [[0]]

    work_stack = check[0].copy()
    visited.add(check[0][0])
    t = 0
    result =  [math.inf] * n
    while work_stack:
        check.append([])
        for i in work_stack:
            for j, node in enumerate(W[i]):
                if node == 0:
                    continue
                if j not in visited:
                    visited.add(j)
                    check[t+1].append(j)
                    result[j] = i
        t += 1
        work_stack = check[t].copy()
    return result[1:]

def num():
    return int(input())
def nums():
    return map(int,input().split())

n,m = nums()
x = set([i for i in range(n)])
y = {i:[] for i in range(n)}

root = [list(nums()) for _ in range(m)]

W = [[0]*n  for _ in range(n)]

for i,j in root:
    W[i-1][j-1] = W[j-1][i-1] = 1

res = bfs_168(n,W)
if math.inf in res:
    print('No')
else:
    print('Yes')
    [print(i+1) for i in res]


Fixed submission:
from collections import deque
import sys


readline = sys.stdin.readline
readall = sys.stdin.read
ins = lambda: readline().rstrip()
ini = lambda: int(readline().rstrip())
inm = lambda: map(int, readline().split())
inl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,m = inm()
W = [[] for _ in range(n)]
for _ in range(m):
    a,b = inm()
    W[a-1].append(b-1)
    W[b-1].append(a-1)

res = [0] * n
dis = [n] * n
dis[0] = 0

q = deque()
q.append(0)
while q:
    c = q.popleft()
    for i in W[c]:
        if dis[i] > dis[c] + 1:
            dis[i] = dis[c] + 1
            res[i] = c + 1
            q.append(i)

print('Yes')
prl(res[1:])

Buggy python submission (verdict: Time Limit Exceeded):
import math


def bfs_168(n,W):
    work_stack = []
    visited = set()
    check = [[0]]

    work_stack = check[0].copy()
    visited.add(check[0][0])
    t = 0
    result =  [math.inf] * n
    while work_stack:
        check.append([])
        for i in work_stack:
            for j, node in enumerate(W[i]):
                if node == 0:
                    continue
                if j not in visited:
                    visited.add(j)
                    check[t+1].append(j)
                    result[j] = i
        t += 1
        work_stack = check[t].copy()
    return result[1:]

def num():
    return int(input())
def nums():
    return map(int,input().split())

n,m = nums()
x = set([i for i in range(n)])
y = {i:[] for i in range(n)}

root = [list(nums()) for _ in range(m)]

W = [[0]*n  for _ in range(n)]

for i,j in root:
    W[i-1][j-1] = W[j-1][i-1] = 1

res = bfs_168(n,W)
if math.inf in res:
    print('No')
else:
    print('Yes')
    [print(i+1) for i in res]


Fixed submission:
from collections import deque
import sys


readline = sys.stdin.readline
readall = sys.stdin.read
ins = lambda: readline().rstrip()
ini = lambda: int(readline().rstrip())
inm = lambda: map(int, readline().split())
inl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,m = inm()
W = [[] for _ in range(n)]
for _ in range(m):
    a,b = inm()
    W[a-1].append(b-1)
    W[b-1].append(a-1)

res = [0] * n
dis = [n] * n
dis[0] = 0

q = deque()
q.append(0)
while q:
    c = q.popleft()
    for i in W[c]:
        if dis[i] > dis[c] + 1:
            dis[i] = dis[c] + 1
            res[i] = c + 1
            q.append(i)

print('Yes')
prl(res[1:])

Buggy python submission (verdict: Time Limit Exceeded):
import math
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix


def num():
    return int(input())


def nums():
    return map(int,input().split())

n,m = nums()

root = [list(nums()) for _ in range(m)]

res = [[math.inf]*n  for _ in range(n)]

for i in range(n):
    res[i][i] = 0

for i,j in root:
     
    res[i-1][j-1] = res[j-1][i-1] = 1

res = csr_matrix(res)
res,t = shortest_path(res, indices=0, return_predecessors=True)

if -9999 in res[1:]:
    print('No')
else:
    print('Yes')
    [print(i+1) for i in t[1:]]

Fixed submission:
from collections import deque
import sys


readline = sys.stdin.readline
readall = sys.stdin.read
ins = lambda: readline().rstrip()
ini = lambda: int(readline().rstrip())
inm = lambda: map(int, readline().split())
inl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')


n,m = inm()
W = [[] for _ in range(n)]
for _ in range(m):
    a,b = inm()
    W[a-1].append(b-1)
    W[b-1].append(a-1)

res = [0] * n
dis = [n] * n
dis[0] = 0

q = deque()
q.append(0)
while q:
    c = q.popleft()
    for i in W[c]:
        if dis[i] > dis[c] + 1:
            dis[i] = dis[c] + 1
            res[i] = c + 1
            q.append(i)

print('Yes')
prl(res[1:])