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

def file_input():
    f = open('ABC168/input.txt', 'r')
    sys.stdin = f

def main():
    if os.getenv("ENV")=='LOCAL':
        file_input()

    N,M=map(int, input().split())
    r={}
    for i in range(M):
        A,B=map(int, input().split())
        # print("A:"+str(A[i])+" B:"+str(B[i]))
        if A not in r:
            r[A]=[B]
        else:
            r[A].append(B)
        if B not in r:
            r[B]=[A]
        else:
            r[B].append(A)
        # print(tmp[A[i]-1])
    # print(r)

    q=deque()
    q.append(1)
    visited=set()
    out=[0]*(N-1)
    while len(q)>0:
        # print("q="+str(q))
        position=q.popleft()
        # print("p="+str(position))
        visited.add(position)
        for des in r[position]: #今いる場所から行ける場所を探索
            if des in visited:  #すでに訪問済み
                continue
            if des in q:    #ループよけ 2→2みたいな
                continue
            q.append(des)   #行ける場所だった場合キューに追加
            # print("destination="+str(des)+" p="+str(position)+" q="+str(q))
            out[des-2]=position
    # print(visited)



    print('Yes')
    [print(x) for x in out]

if __name__ == '__main__':
    main()


Fixed submission:
import sys
import os
from collections import deque

def file_input():
    f = open('ABC168/input.txt', 'r')
    sys.stdin = f

def main():
    if os.getenv("ENV")=='LOCAL':
        file_input()

    N,M=map(int, input().split())
    r={}
    for i in range(M):
        A,B=map(int, input().split())
        # print("A:"+str(A[i])+" B:"+str(B[i]))
        if A not in r:
            r[A]=[B]
        else:
            r[A].append(B)
        if B not in r:
            r[B]=[A]
        else:
            r[B].append(A)
        # print(tmp[A[i]-1])
    # print(r)

    q=deque()
    q.append(1)
    visited=set()
    visited.add(1)
    out=[0]*(N-1)
    while len(q)>0:
        # print("q="+str(q))
        position=q.popleft()
        # print("p="+str(position))
        for des in r[position]: #今いる場所から行ける場所を探索
            if des not in visited:  #訪問済みでないなら
                q.append(des)   #行ける場所だった場合キューに追加
                visited.add(des)   #訪問済みにする
                out[des-2]=position
                # print("destination="+str(des)+" p="+str(position)+" q="+str(q))
    # print(visited)



    print('Yes')
    [print(x) for x in out]

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import os

def file_input():
    f = open('ABC168/input.txt', 'r')
    sys.stdin = f

def main():
    if os.getenv("ENV")=='LOCAL':
        file_input()

    N,M=map(int, input().split())
    A=[0]*M
    B=[0]*M
    for i in range(M):
        A[i],B[i]=map(int, input().split())
        # print("A:"+str(A[i])+" B:"+str(B[i])+" tmp:"+str(tmp))
        # print(tmp[A[i]-1])

    al=[False]*N    #すでに探索したか
    al[0]=True
    out=[0]*N   #何番目に通るか
    li=[]
    li.append([1])     #深さ毎の数値
    for de in range(N): #深さで探索
        tmp=[]
        # print(de)
        for l in li[de]:
            for i in range(M):  #A,Bの探索
                if A[i]==l and not al[B[i]-1]: #A[i]が一個前とつながってたら
                    tmp.append(B[i])
                    out[B[i]-1]=l
                    al[B[i]-1]=True
                    # print("A="+str(B[i])+" l="+str(l)+" out"+str([B[i]-1])+"="+str(out[B[i]-1]))
                if B[i]==l and not al[A[i]-1]:
                    tmp.append(A[i])
                    out[A[i]-1]=l
                    al[A[i]-1]=True
                    # print("B="+str(A[i])+" l="+str(l)+" out"+str([A[i]-1])+"="+str(out[A[i]-1]))
        li.append(tmp)
        # print(al)


    print('Yes')
    [print(x) for x in out[1:]]

if __name__ == '__main__':
    main()


Fixed submission:
import sys
import os
from collections import deque

def file_input():
    f = open('ABC168/input.txt', 'r')
    sys.stdin = f

def main():
    if os.getenv("ENV")=='LOCAL':
        file_input()

    N,M=map(int, input().split())
    r={}
    for i in range(M):
        A,B=map(int, input().split())
        # print("A:"+str(A[i])+" B:"+str(B[i]))
        if A not in r:
            r[A]=[B]
        else:
            r[A].append(B)
        if B not in r:
            r[B]=[A]
        else:
            r[B].append(A)
        # print(tmp[A[i]-1])
    # print(r)

    q=deque()
    q.append(1)
    visited=set()
    visited.add(1)
    out=[0]*(N-1)
    while len(q)>0:
        # print("q="+str(q))
        position=q.popleft()
        # print("p="+str(position))
        for des in r[position]: #今いる場所から行ける場所を探索
            if des not in visited:  #訪問済みでないなら
                q.append(des)   #行ける場所だった場合キューに追加
                visited.add(des)   #訪問済みにする
                out[des-2]=position
                # print("destination="+str(des)+" p="+str(position)+" q="+str(q))
    # print(visited)



    print('Yes')
    [print(x) for x in out]

if __name__ == '__main__':
    main()