Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
d = {}
for _ in range(M):
    A, B = map(int, input().split())
    if A not in d:
        d[A] = [B]
    else:
        d[A].append(B)
    if B not in d:
        d[B] = [A]
    else:
        d[B].append(A)

ans = [(-1, float("inf"))] * N
cnt = 0
stack = [(1, 0)]
while stack:
    val, step = stack.pop()
    next_vals = d[val]
    for nxt in next_vals:
        if ans[nxt - 1][1] > step + 1:
            if ans[nxt - 1][0] == -1:
                cnt += 1
            ans[nxt - 1] = (val, step + 1)
            stack.append((nxt, step + 1))

if cnt < N - 1:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])

Fixed submission:
from collections import deque

N, M = map(int, input().split())
d = {}
for _ in range(M):
    A, B = map(int, input().split())
    if A not in d:
        d[A] = [B]
    else:
        d[A].append(B)
    if B not in d:
        d[B] = [A]
    else:
        d[B].append(A)

ans = [(-1, float("inf"))] * N
cnt = 0
# stack = [(1, 0)]
queue = deque([(1, 0)])

while queue:
    val, step = queue.popleft()
    next_vals = d[val]
    for nxt in next_vals:
        #        if ans[nxt - 1][1] > step + 1:
        if ans[nxt - 1][0] == -1:
            cnt += 1
            ans[nxt - 1] = (val, step + 1)
            queue.append((nxt, step + 1))

if cnt < N - 1:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])

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

N, M = map(int, input().split())
d = defaultdict(list)
for _ in range(M):
    A, B = map(int, input().split())
    d[A].append(B)
    d[B].append(A)
ans = [(-1, float("inf"))] * N
cnt = 0
stack = [(1, 0)]
while stack:
    val, step = stack.pop()
    next_vals = d[val]
    for nxt in next_vals:
        if ans[nxt - 1][1] > step + 1:
            ans[nxt - 1] = (val, step + 1)
            stack.append((nxt, step + 1))

flag_no = False
for i in range(1, N):
    if ans[i][0] == -1:
        flag_no = True
        break

if flag_no:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])

Fixed submission:
from collections import deque

N, M = map(int, input().split())
d = {}
for _ in range(M):
    A, B = map(int, input().split())
    if A not in d:
        d[A] = [B]
    else:
        d[A].append(B)
    if B not in d:
        d[B] = [A]
    else:
        d[B].append(A)

ans = [(-1, float("inf"))] * N
cnt = 0
# stack = [(1, 0)]
queue = deque([(1, 0)])

while queue:
    val, step = queue.popleft()
    next_vals = d[val]
    for nxt in next_vals:
        #        if ans[nxt - 1][1] > step + 1:
        if ans[nxt - 1][0] == -1:
            cnt += 1
            ans[nxt - 1] = (val, step + 1)
            queue.append((nxt, step + 1))

if cnt < N - 1:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])

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

N, M = map(int, input().split())
d = defaultdict(list)
for _ in range(M):
    A, B = map(int, input().split())
    d[A].append(B)
    d[B].append(A)
ans = [(-1, float("inf"))] * N
cnt = 0
stack = [(1, 0)]
while stack:
    val, step = stack.pop()
    next_vals = d[val]
    for nxt in next_vals:
        if ans[nxt - 1][1] > step + 1:
            ans[nxt - 1] = (val, step + 1)
            stack.append((nxt, step + 1))

flag_no = False
for i in range(1, N):
    if ans[i][0] == -1:
        flag_no = True
        break

if flag_no:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])

Fixed submission:
from collections import deque

N, M = map(int, input().split())
d = {}
for _ in range(M):
    A, B = map(int, input().split())
    if A not in d:
        d[A] = [B]
    else:
        d[A].append(B)
    if B not in d:
        d[B] = [A]
    else:
        d[B].append(A)

ans = [(-1, float("inf"))] * N
cnt = 0
# stack = [(1, 0)]
queue = deque([(1, 0)])

while queue:
    val, step = queue.popleft()
    next_vals = d[val]
    for nxt in next_vals:
        #        if ans[nxt - 1][1] > step + 1:
        if ans[nxt - 1][0] == -1:
            cnt += 1
            ans[nxt - 1] = (val, step + 1)
            queue.append((nxt, step + 1))

if cnt < N - 1:
    print("No")
else:
    print("Yes")
    for i in range(1, N):
        print(ans[i][0])