Problem:
There is a cave.  The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.  It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.  Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.    * If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.  Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Incorrect submission (verdict: Wrong Answer):
from collections import deque

n,m = map(int,input().split())

res = {}
for i in range(m):
    a,b = map(int,input().split())
    res.setdefault(a,[]).append(b)
    res.setdefault(b,[]).append(a)

dist =[0] * (n+1)
sirusi = [0] * (n+1)
dist[1] = -1

que = deque([])
ique = deque([])
x = res.pop(1)
#print(x)
que.append(x)
ique.append(1)

d = 1
#print(len(que))

while(len(que)>0):
    data = que.popleft()
    index = ique.popleft()
    #print(data)
    ind = 0
    for i in data:
        #print(int(i))

        if dist[int(i)] == 0 :
            # print("d")
            # print(index)
            dist[i] = d
            sirusi[i] = index
            que.append(res.pop(i))
            ique.append(int(i))
        ind += 1
    d = d + 1
#
# while(len(next) > 0):
#     data = next.pop()
#     print("data = ",end="")
#     print(data)
#     if(not(type(data) is int)):
#         print("not int")


#                 dist[data[i]] = d
#                 print("res[data[i]] = ")
#                 #print(res[data[i]])
#                 next.append(res.pop(data[i]))
#     else:
#         print("int")

#             dist[data] = d
#             print("res = ",end="")
#             print(res[data])
#             next.append(res[data])
#
#     d = d +1



#print(dist)

for i in sirusi:
    print(i)

Fixed, accepted submission:
from collections import deque

n,m = map(int,input().split())

res = {}
for i in range(m):
    a,b = map(int,input().split())
    res.setdefault(a,[]).append(b)
    res.setdefault(b,[]).append(a)

dist =[0] * (n+1)
sirusi = [0] * (n+1)
dist[1] = -1

que = deque([])
ique = deque([])
x = res.pop(1)
#print(x)
que.append(x)
ique.append(1)

d = 1
#print(len(que))

while(len(que)>0):
    data = que.popleft()
    index = ique.popleft()
    #print(data)
    ind = 0
    for i in data:
        #print(int(i))

        if dist[int(i)] == 0 :
            # print("d")
            # print(index)
            dist[i] = d
            sirusi[i] = index
            que.append(res.pop(i))
            ique.append(int(i))
        ind += 1
    d = d + 1
#
# while(len(next) > 0):
#     data = next.pop()
#     print("data = ",end="")
#     print(data)
#     if(not(type(data) is int)):
#         print("not int")


#                 dist[data[i]] = d
#                 print("res[data[i]] = ")
#                 #print(res[data[i]])
#                 next.append(res.pop(data[i]))
#     else:
#         print("int")

#             dist[data] = d
#             print("res = ",end="")
#             print(res[data])
#             next.append(res[data])
#
#     d = d +1



#print(dist)

print("Yes")
for i in range(2,len(sirusi)):
    print(sirusi[i])