Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())

cost = [[float("inf") for i in range(n)] for i in range(n)] 

for i in range(m):
    ai,bi=map(int, input().split())
    cost[ai-1][bi-1] = 1
    cost[bi-1][ai-1] = 1
    

def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
        
q=dijkstra(0,n,m,cost)
r=[0]*n
for i in range(1,len(q)):
    qmin=10**10
    for j in range(n):
        if cost[i][j]==1:
            if qmin>q[j]:
                qmin=q[j]
                r[i]=j

if max(q)==float("inf"):
    print("No")
else:
    print("Yes")
    for i in range(1,len(q)):
        print(r[i]+1)
                

Fixed submission:
import sys
from collections import deque

icase=0
if icase==0:
    n,m=map(int,input().split())
    g = [[] for _ in range(n)]
    for i in range(m):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
elif icase==1:
    n,m=4,4
    g=[[1], [0, 2, 3], [1, 3], [2, 1]]
elif icase==2:
    n,m=6,9
    g=[[5, 4], [3, 5], [3, 4], [2, 1, 5, 4], [2, 0, 3, 5], [0, 3, 1, 4]]

q=deque([0])

stack=[0]
parent=[-1]*n
parent[0]=0
dist=[-1]*n
dist[0]=0
while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v])
for i in range(1,n):
    if parent[i]==-1:
        print("No")
        sys.exit()

print("Yes")
for i in range(1,n):
#    print(i+1,dist[i])    
    print(parent[i]+1)

Buggy python submission (verdict: Time Limit Exceeded):

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

g=[[] for i in range(n)]
cost = [[float("inf") for i in range(n)] for i in range(n)] 

for i in range(m):
    ai,bi=map(int, input().split())
    g[ai-1].append(bi-1)
    g[bi-1].append(ai-1)
    cost[ai-1][bi-1] = 1
    cost[bi-1][ai-1] = 1
    
seen=[False]*n
    
def dfs(g,v):
    seen[v]=True    
    for nv in g[v]:
        if seen[v]:
            continue
        dfs(g,nv)

def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
        
icnt=0
for v in range(n):
    if seen[v]:
        continue
    dfs(g,v)
    icnt+=1
    
if icnt!=n:
    print("No")
else:
    print("Yes")

    q=dijkstra(0,n,m,cost)
    r=[0]*n
    for i in range(1,len(q)):
        qmin=10**10
        for j in g[i]:
            if qmin>q[j]:
                qmin=q[j]
                r[i]=j
#            print("1:",g[i],j,q[j],qmin)
        print(r[i]+1)
                

Fixed submission:
import sys
from collections import deque

icase=0
if icase==0:
    n,m=map(int,input().split())
    g = [[] for _ in range(n)]
    for i in range(m):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
elif icase==1:
    n,m=4,4
    g=[[1], [0, 2, 3], [1, 3], [2, 1]]
elif icase==2:
    n,m=6,9
    g=[[5, 4], [3, 5], [3, 4], [2, 1, 5, 4], [2, 0, 3, 5], [0, 3, 1, 4]]

q=deque([0])

stack=[0]
parent=[-1]*n
parent[0]=0
dist=[-1]*n
dist[0]=0
while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v])
for i in range(1,n):
    if parent[i]==-1:
        print("No")
        sys.exit()

print("Yes")
for i in range(1,n):
#    print(i+1,dist[i])    
    print(parent[i]+1)

Buggy python submission (verdict: Time Limit Exceeded):

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

g=[[] for i in range(n)]
cost = [[float("inf") for i in range(n)] for i in range(n)] 

for i in range(m):
    ai,bi=map(int, input().split())
    g[ai-1].append(bi-1)
    g[bi-1].append(ai-1)
    cost[ai-1][bi-1] = 1
    cost[bi-1][ai-1] = 1
    
seen=[False]*n
    
def dfs(g,v):
    seen[v]=True    
    for nv in g[v]:
        if seen[v]:
            continue
        dfs(g,nv)

def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
        
icnt=0
for v in range(n):
    if seen[v]:
        continue
    dfs(g,v)
    icnt+=1
    
if icnt!=n:
    print("No")
else:
    print("Yes")

    q=dijkstra(0,n,m,cost)
    r=[0]*n
    for i in range(1,len(q)):
        qmin=10**10
        for j in g[i]:
            if qmin>q[j]:
                qmin=q[j]
                r[i]=j
#            print("1:",g[i],j,q[j],qmin)
        print(r[i]+1)
                

Fixed submission:
import sys
from collections import deque

icase=0
if icase==0:
    n,m=map(int,input().split())
    g = [[] for _ in range(n)]
    for i in range(m):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
elif icase==1:
    n,m=4,4
    g=[[1], [0, 2, 3], [1, 3], [2, 1]]
elif icase==2:
    n,m=6,9
    g=[[5, 4], [3, 5], [3, 4], [2, 1, 5, 4], [2, 0, 3, 5], [0, 3, 1, 4]]

q=deque([0])

stack=[0]
parent=[-1]*n
parent[0]=0
dist=[-1]*n
dist[0]=0
while q:
    state = q.popleft()
#    print("--- q:",q,"state:",state,"g:",g[state],"p:",parent)
    for v in g[state]:
#        print("  state:",state,"v:",v,"q:",q)
#        if parent[state] == v:
        if parent[v] !=-1:
            continue
        else:
#            print("parent:",parent,"xx:",xx,"stack",stack)
            parent[v]=state
            dist[v]=dist[state]+1
            q.append(v)
#            print(" state:",state,"v:",v,"parent:",parent,"stack",stack,"dist[v]",dist[v])
for i in range(1,n):
    if parent[i]==-1:
        print("No")
        sys.exit()

print("Yes")
for i in range(1,n):
#    print(i+1,dist[i])    
    print(parent[i]+1)