Buggy python submission (verdict: Runtime Error):
def nearlist(N, LIST):
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック
    dist = [n for _ in range(N)]  # 前処理
    PATH = [-1 for _ in range(N)]
    dist[S], PATH[S] = 0, 's'
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i in NEAR[q]:  # 移動先の候補
            if i != 0 and i in frag:
                dist[i] = dist[PATH[i]] + 1
            if dist[i] > dist[q] + 1:
                dist[i] = dist[q] + 1
                PATH[i] = q
            if i in frag:  # 処理済みか否か
                continue
            stack.append(i), frag.add(i)
        print(dist,PATH)
    return PATH


n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

near = nearlist(n, ab)
path = dfs(near, 0, n)

print('Yes')
for i in range(1, n):
    print(path[i] + 1)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    pas = [-1 for _ in range(N)]
    pas[S] = 's'
    frag = set([S])
    que = deque([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:
            if i in frag:  # 処理済みか否か
                continue
            pas[i]=q
            que.append(i)
            frag.add(i)
    return pas


n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

near = [[] for _ in range(n)]
for a, b in ab:
    near[a - 1].append(b - 1)
    near[b - 1].append(a - 1)
pas = bfs(near, 0, n)

if all(pas[i] != -1 for i in range(n)):
    print('Yes')
    for i in range(1, n):
        print(pas[i] + 1)
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
def nearlist(N, LIST):
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック
    dist = [n for _ in range(N)]  # 前処理
    PATH = [-1 for _ in range(N)]
    dist[S], PATH[S] = 0, 's'
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i in NEAR[q]:  # 移動先の候補
            dist[i] = dist[PATH[i]] + 1
            if dist[i] > dist[q] + 1:
                dist[i] = dist[q] + 1
                PATH[i] = q
            if i in frag:  # 処理済みか否か
                continue
            stack.append(i), frag.add(i)
    return PATH


n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

near = nearlist(n, ab)
path = dfs(near, 0, n)

print('Yes')
for i in range(1, n):
    print(path[i] + 1)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def bfs(NEAR, S, N):  # 幅優先探索  # キュー
    pas = [-1 for _ in range(N)]
    pas[S] = 's'
    frag = set([S])
    que = deque([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:
            if i in frag:  # 処理済みか否か
                continue
            pas[i]=q
            que.append(i)
            frag.add(i)
    return pas


n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]

near = [[] for _ in range(n)]
for a, b in ab:
    near[a - 1].append(b - 1)
    near[b - 1].append(a - 1)
pas = bfs(near, 0, n)

if all(pas[i] != -1 for i in range(n)):
    print('Yes')
    for i in range(1, n):
        print(pas[i] + 1)
else:
    print('No')