Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N , M = map(int, input().split())
room = [-1 for k in range(N)]
L = deque([] for k in range(M))
for k in range(M):
    L[k] = deque(map(int, input().split()))
nextvisit =[[1]]
visited =deque(0 for k in range(N))
visited[0]=1
not_used = deque(k for k in range(M))
for j in range(M-1):
    nextvisit.append([])
    for visit in nextvisit[0]:

        """
        print("L=",L)
        print("next=",nextvisit[j])
        print("not_used", not_used)
        """
        erase = []
        for k in not_used:
            if visit == L[k][1]:
                if visited[L[k][0]-1] == 0:
                    nextvisit[1].append(L[k][0])
                    visited[L[k][1]-1] = 1
                    visited[L[k][0]-1] = 1
                    room[L[k][0]-1] = L[k][1]
                erase.append(k)
                
            if visit == L[k][0]:
                if visited[L[k][1]-1] == 0:
                    nextvisit[1].append(L[k][1])
                    visited[L[k][0]-1] = 1
                    visited[L[k][1]-1] = 1
                    room[L[k][1]-1] = L[k][0]  
                erase.append(k)

        not_used = deque(set(not_used)-set(erase))
    del nextvisit[0]
    
    if len(not_used) == 0:
        break
print("Yes")
if N >2:
    for k in range(1, N):
        print(room[k])
if N ==2:
    print(1)

Fixed submission:
N, M = map(int, input().split())
nextroom = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  nextroom[A-1].append(B-1)
  nextroom[B-1].append(A-1)

sgn = [-1 for _ in range(N)]
sgn[0] = 0

depth = [float('inf') for _ in range(N)]
depth[0] = 0

now = [0]
nextvisit = []
for j in range(1, M+1):
  for n in now:
    for i in nextroom[n]: 
      if depth[i] > j:
        nextvisit.append(i)
        sgn[i] = n
        depth[i] = j
  now = [] + nextvisit
  nextvisit = []

if -1 in sgn:
  print('No')
else:
  print('Yes')
  for k in range(1, len(sgn)):
    print(sgn[k]+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N , M = map(int, input().split())
room = [-1 for k in range(N)]
L = [[] for k in range(M)]
for k in range(M):
    L[k] = deque(map(int, input().split()))
nextvisit =[[1]]
visited =deque(0 for k in range(N))
visited[0]=1
not_used = deque(k for k in range(M))
for j in range(M-1):
    nextvisit.append([])
    for visit in nextvisit[0]:

        """
        print("L=",L)
        print("next=",nextvisit[j])
        print("not_used", not_used)
        """
        erase = []
        for k in not_used:
            if visit == L[k][1]:
                if visited[L[k][0]-1] == 0:
                    nextvisit[1].append(L[k][0])
                    visited[L[k][1]-1] = 1
                    visited[L[k][0]-1] = 1
                    room[L[k][0]-1] = L[k][1]
                erase.append(k)
                
            if visit == L[k][0]:
                if visited[L[k][1]-1] == 0:
                    nextvisit[1].append(L[k][1])
                    visited[L[k][0]-1] = 1
                    visited[L[k][1]-1] = 1
                    room[L[k][1]-1] = L[k][0]  
                erase.append(k)

        not_used = deque(set(not_used)-set(erase))
    del nextvisit[0]
    
    if len(not_used) == 0:
        break
print("Yes")
if N >2:
    for k in range(1, N):
        print(room[k])
if N ==2:
    print(1)

Fixed submission:
N, M = map(int, input().split())
nextroom = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  nextroom[A-1].append(B-1)
  nextroom[B-1].append(A-1)

sgn = [-1 for _ in range(N)]
sgn[0] = 0

depth = [float('inf') for _ in range(N)]
depth[0] = 0

now = [0]
nextvisit = []
for j in range(1, M+1):
  for n in now:
    for i in nextroom[n]: 
      if depth[i] > j:
        nextvisit.append(i)
        sgn[i] = n
        depth[i] = j
  now = [] + nextvisit
  nextvisit = []

if -1 in sgn:
  print('No')
else:
  print('Yes')
  for k in range(1, len(sgn)):
    print(sgn[k]+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N , M = map(int, input().split())
room = [-1 for k in range(N)]
L = [[] for k in range(M)]
for k in range(M):
    L[k] = list(map(int, input().split()))
nextvisit =[[1]]
visited =[0 for k in range(N)]
visited[0]=1
not_used = [k for k in range(M)]
for j in range(M-1):
    nextvisit.append([])
    for visit in nextvisit[0]:

        """
        print("L=",L)
        print("next=",nextvisit[j])
        print("not_used", not_used)
        """
        erase = []
        for k in not_used:
            if visit == L[k][1]:
                if visited[L[k][0]-1] == 0:
                    nextvisit[1].append(L[k][0])
                    visited[L[k][1]-1] = 1
                    visited[L[k][0]-1] = 1
                    room[L[k][0]-1] = L[k][1]
                erase.append(k)
                
            if visit == L[k][0]:
                if visited[L[k][1]-1] == 0:
                    nextvisit[1].append(L[k][1])
                    visited[L[k][0]-1] = 1
                    visited[L[k][1]-1] = 1
                    room[L[k][1]-1] = L[k][0]  
                erase.append(k)

        not_used = list(set(not_used)-set(erase))
    del nextvisit[0]
    
    if len(not_used) == 0:
        break
print("Yes")
if N >2:
    for k in range(1, N):
        print(room[k])
if N ==2:
    print(1)

Fixed submission:
N, M = map(int, input().split())
nextroom = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  nextroom[A-1].append(B-1)
  nextroom[B-1].append(A-1)

sgn = [-1 for _ in range(N)]
sgn[0] = 0

depth = [float('inf') for _ in range(N)]
depth[0] = 0

now = [0]
nextvisit = []
for j in range(1, M+1):
  for n in now:
    for i in nextroom[n]: 
      if depth[i] > j:
        nextvisit.append(i)
        sgn[i] = n
        depth[i] = j
  now = [] + nextvisit
  nextvisit = []

if -1 in sgn:
  print('No')
else:
  print('Yes')
  for k in range(1, len(sgn)):
    print(sgn[k]+1)

Buggy python submission (verdict: Time Limit Exceeded):
N , M = map(int, input().split())
room = [-1 for k in range(N)]
L = [[] for k in range(M)]
for k in range(M):
    L[k] = list(map(int, input().split()))
nextvisit =[[] for k in range(M)]
nextvisit[0].append(1)
visited =[0 for k in range(N)]
visited[0]=1
not_used = [k for k in range(M)]
for j in range(M-1):
    for visit in nextvisit[j]:
        """
        print("L=",L)
        print("next=",nextvisit[j])
        print("not_used", not_used)
        """
        erase = []
        for k in not_used:
            if visit == L[k][1]:
                if visited[L[k][0]-1] == 0:
                    nextvisit[j+1].append(L[k][0])
                    visited[L[k][1]-1] = 1
                    visited[L[k][0]-1] = 1
                    room[L[k][0]-1] = L[k][1]
                erase.append(k)
                
            if visit == L[k][0]:
                if visited[L[k][1]-1] == 0:
                    nextvisit[j+1].append(L[k][1])
                    visited[L[k][0]-1] = 1
                    visited[L[k][1]-1] = 1
                    room[L[k][1]-1] = L[k][0]  
                erase.append(k)    
            not_used = list(set(not_used)-set(erase))
    
    if len(not_used) == 0:
        break
print("Yes")
if N >2:
    for k in range(1, N):
        print(room[k])
if N ==2:
    print(1)

Fixed submission:
N, M = map(int, input().split())
nextroom = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  nextroom[A-1].append(B-1)
  nextroom[B-1].append(A-1)

sgn = [-1 for _ in range(N)]
sgn[0] = 0

depth = [float('inf') for _ in range(N)]
depth[0] = 0

now = [0]
nextvisit = []
for j in range(1, M+1):
  for n in now:
    for i in nextroom[n]: 
      if depth[i] > j:
        nextvisit.append(i)
        sgn[i] = n
        depth[i] = j
  now = [] + nextvisit
  nextvisit = []

if -1 in sgn:
  print('No')
else:
  print('Yes')
  for k in range(1, len(sgn)):
    print(sgn[k]+1)