Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj = [[] for _ in range(N+1)]
    for ln in readlines():
        a, b = map(int, ln.split())
        adj[a].append(b)
        adj[b].append(a)

    dist = [-1] * (N+1)
    label = [-1] * (N+1)
    dist[0] = dist[1] = 0
    label[0] = label[1] = 0

    stack = [1]
    while stack:
        x = stack.pop()
        for ch in adj[x]:
            if dist[ch] != -1 and dist[ch] < dist[x] + 1: continue
            dist[ch] = dist[x] + 1
            label[ch] = x
            stack.append(ch)

    if -1 in label:
        print('No')
        return
    else:
        print('Yes')
        print(*label[2:], sep='\n')

abc168_d()

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj = [[] for _ in range(N+1)]
    for ln in readlines():
        a, b = map(int, ln.split())
        adj[a].append(b)
        adj[b].append(a)

    label = [-1] * (N+1)
    label[0] = label[1] = 0

    from collections import deque
    que = deque([1])

    while que:
        k = que.popleft()
        for x in adj[k]:
            if label[x] != -1: continue  # 訪問済み
            label[x] = k  # 行先表示が確定
            que.append(x)  # キューに追加

    if -1 in label:
        print('No')
        return
    else:
        print('Yes')
        print(*label[2:], sep='\n')

abc168_d()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj = [[] for _ in range(N+1)]
    for ln in readlines():
        a, b = map(int, ln.split())
        adj[a].append(b)
        adj[b].append(a)

    dist = [-1] * (N+1)
    label = [-1] * (N+1)
    dist[0] = dist[1] = 0
    label[0] = label[1] = 0

    stack = [1]
    while stack:
        x = stack.pop()
        for ch in adj[x]:
            if dist[ch] != -1 and dist[ch] < dist[x] + 1: continue
            dist[ch] = dist[x] + 1
            label[ch] = x
            stack.append(ch)

    if -1 in label:
        print('No')
        return
    else:
        print('Yes')
        print(*label[2:], sep='\n')

abc168_d()

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj = [[] for _ in range(N+1)]
    for ln in readlines():
        a, b = map(int, ln.split())
        adj[a].append(b)
        adj[b].append(a)

    label = [-1] * (N+1)
    label[0] = label[1] = 0

    from collections import deque
    que = deque([1])

    while que:
        k = que.popleft()
        for x in adj[k]:
            if label[x] != -1: continue  # 訪問済み
            label[x] = k  # 行先表示が確定
            que.append(x)  # キューに追加

    if -1 in label:
        print('No')
        return
    else:
        print('Yes')
        print(*label[2:], sep='\n')

abc168_d()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj_mtx = [[False] * (N+1) for _ in range(N+1)]  # 0行0列は使わない
    for ln in readlines():
        a, b = map(int, ln.split())
        adj_mtx[a][b] = adj_mtx[b][a] = True

    labels = [-1] * (N+1)  # -1は未訪問
    labels[0] = labels[1] = 0  # index=0はずっと未訪問だけど0にしておく

    from collections import deque
    que = deque([1])

    while que:
        k = que.popleft()
        for x in range(N+1):
            if not adj_mtx[k][x]: continue  # つながっていない
            if labels[x] != -1: continue  # 訪問済み
            labels[x] = k  # 行先表示が確定
            que.append(x)  # キューに追加

    if -1 in labels:
        print('No')
        return
    else:
        print('Yes')
        print(*labels[2:], sep='\n')

abc168_d()

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def abc168_d():
    N, _ = map(int, readline().split())
    adj = [[] for _ in range(N+1)]
    for ln in readlines():
        a, b = map(int, ln.split())
        adj[a].append(b)
        adj[b].append(a)

    label = [-1] * (N+1)
    label[0] = label[1] = 0

    from collections import deque
    que = deque([1])

    while que:
        k = que.popleft()
        for x in adj[k]:
            if label[x] != -1: continue  # 訪問済み
            label[x] = k  # 行先表示が確定
            que.append(x)  # キューに追加

    if -1 in label:
        print('No')
        return
    else:
        print('Yes')
        print(*label[2:], sep='\n')

abc168_d()