Buggy python submission (verdict: Runtime Error):
from collections import deque

N,M=list(map(int,input().split()))

g = [[] for _ in range(N+1)] # 隣接リスト

for _ in range(M):
    a, b = [int(x) for x in input().split()]
    print(a,b,g)
    g[a].append(b)
    g[b].append(a)

#road_list=[]
#for i in range(M):
#  road_list.append(list(map(int,input().split())))

queue = deque([1])
d = [None] * (N+1) # uからの距離の初期化
d[1] = 0 # 自分との距離は0
while queue:
  v = queue.popleft()
  for i in g[v]:
    if d[i] is None:
      d[i] = v
      queue.append(i)

#print(d)


#ans_dict=dict([])
#before_room=[1]
#while True:
#  temp_room=[]
#  for j in range(len(before_room)):
#    for i in road_list:
#      if i[0]==before_room[j]:
#        if i[1] not in ans_dict:
#          ans_dict[i[1]]=before_room[j]
#          temp_room.append(i[1])
#      if i[1]==before_room[j]:
#        if i[0] not in ans_dict:
#          ans_dict[i[0]]=before_room[j]
#          temp_room.append(i[0])
#  before_room=temp_room
#  if len(ans_dict)==N:
#    break
    
d[0]=0
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(2,N+1):
    print(d[i])

    

Fixed submission:
from collections import deque

N,M=list(map(int,input().split()))

g = [[] for _ in range(N+1)] # 隣接リスト

for _ in range(M):
    a, b = [int(x) for x in input().split()]
    #print(a,b,g)
    g[a].append(b)
    g[b].append(a)

#road_list=[]
#for i in range(M):
#  road_list.append(list(map(int,input().split())))

queue = deque([1])
d = [None] * (N+1) # uからの距離の初期化
d[1] = 0 # 自分との距離は0
while queue:
  v = queue.popleft()
  for i in g[v]:
    if d[i] is None:
      d[i] = v
      queue.append(i)

#print(d)


#ans_dict=dict([])
#before_room=[1]
#while True:
#  temp_room=[]
#  for j in range(len(before_room)):
#    for i in road_list:
#      if i[0]==before_room[j]:
#        if i[1] not in ans_dict:
#          ans_dict[i[1]]=before_room[j]
#          temp_room.append(i[1])
#      if i[1]==before_room[j]:
#        if i[0] not in ans_dict:
#          ans_dict[i[0]]=before_room[j]
#          temp_room.append(i[0])
#  before_room=temp_room
#  if len(ans_dict)==N:
#    break
    
d[0]=0
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(2,N+1):
    print(d[i])

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
road_list=[]
for i in range(M):
  road_list.append(list(map(int,input().split())))
ans_dict=dict([])
before_room=[1]
while True:
  temp_room=[]
  for j in range(len(before_room)):
    temp_delete=[]
    for i in road_list:
      if i[0]==before_room[j]:
        if i[1] not in ans_dict:
          ans_dict[i[1]]=before_room[j]
          temp_room.append(i[1])
        #temp_delete.append(i)
      if i[1]==before_room[j]:
        if i[0] not in ans_dict:
          ans_dict[i[0]]=before_room[j]
          temp_room.append(i[0])
        #temp_delete.append(i)
    #for temp in temp_delete:
    #  road_list.remove(temp)
  before_room=temp_room
  #print(ans_dict)
  #print(before_room)
  if len(ans_dict)==N:
    break
    
if len(ans_dict)==N:
  print("Yes")
  for i in range(2,len(ans_dict)+1):
    print(ans_dict[i])
else:
  print("No")

    

Fixed submission:
from collections import deque

N,M=list(map(int,input().split()))

g = [[] for _ in range(N+1)] # 隣接リスト

for _ in range(M):
    a, b = [int(x) for x in input().split()]
    #print(a,b,g)
    g[a].append(b)
    g[b].append(a)

#road_list=[]
#for i in range(M):
#  road_list.append(list(map(int,input().split())))

queue = deque([1])
d = [None] * (N+1) # uからの距離の初期化
d[1] = 0 # 自分との距離は0
while queue:
  v = queue.popleft()
  for i in g[v]:
    if d[i] is None:
      d[i] = v
      queue.append(i)

#print(d)


#ans_dict=dict([])
#before_room=[1]
#while True:
#  temp_room=[]
#  for j in range(len(before_room)):
#    for i in road_list:
#      if i[0]==before_room[j]:
#        if i[1] not in ans_dict:
#          ans_dict[i[1]]=before_room[j]
#          temp_room.append(i[1])
#      if i[1]==before_room[j]:
#        if i[0] not in ans_dict:
#          ans_dict[i[0]]=before_room[j]
#          temp_room.append(i[0])
#  before_room=temp_room
#  if len(ans_dict)==N:
#    break
    
d[0]=0
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(2,N+1):
    print(d[i])

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
road_list=[]
for i in range(M):
  road_list.append(list(map(int,input().split())))
ans_dict=dict([])
before_room=[1]
while True:
  temp_room=[]
  for j in range(len(before_room)):
    temp_delete=[]
    for i in road_list:
      #print(road_list)
      #print("before_room=",before_room,j)
      if i[0]==before_room[j]:
        if i[1] not in ans_dict:
          ans_dict[i[1]]=before_room[j]
          temp_room.append(i[1])
        #road_list.remove(i)
        temp_delete.append(i)
      if i[1]==before_room[j]:
        if i[0] not in ans_dict:
          ans_dict[i[0]]=before_room[j]
          temp_room.append(i[0])
        #road_list.remove(i)
        temp_delete.append(i)
    for temp in temp_delete:
      road_list.remove(temp)
  before_room=temp_room
    #if len(ans_dict)==N-1:
    #  break
  if len(ans_dict)==N-1:
    break
#print(ans_dict)
if len(ans_dict)==N-1:
  print("Yes")
  for i in range(len(ans_dict)):
    print(ans_dict[i+2])
else:
  print("No")

Fixed submission:
from collections import deque

N,M=list(map(int,input().split()))

g = [[] for _ in range(N+1)] # 隣接リスト

for _ in range(M):
    a, b = [int(x) for x in input().split()]
    #print(a,b,g)
    g[a].append(b)
    g[b].append(a)

#road_list=[]
#for i in range(M):
#  road_list.append(list(map(int,input().split())))

queue = deque([1])
d = [None] * (N+1) # uからの距離の初期化
d[1] = 0 # 自分との距離は0
while queue:
  v = queue.popleft()
  for i in g[v]:
    if d[i] is None:
      d[i] = v
      queue.append(i)

#print(d)


#ans_dict=dict([])
#before_room=[1]
#while True:
#  temp_room=[]
#  for j in range(len(before_room)):
#    for i in road_list:
#      if i[0]==before_room[j]:
#        if i[1] not in ans_dict:
#          ans_dict[i[1]]=before_room[j]
#          temp_room.append(i[1])
#      if i[1]==before_room[j]:
#        if i[0] not in ans_dict:
#          ans_dict[i[0]]=before_room[j]
#          temp_room.append(i[0])
#  before_room=temp_room
#  if len(ans_dict)==N:
#    break
    
d[0]=0
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(2,N+1):
    print(d[i])