Problem:
 Score: 400 points    Problem Statement  There is a cave.  The cave has N rooms and M passages. The rooms are numbered 1 to N , and the passages are numbered 1 to M . Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.  It is dark in the cave, so we have decided to place a signpost in each room except Room 1 . The signpost in each room will point to one of the rooms directly connected to that room with a passage.  Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1 .   If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.   Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.      Constraints   All values in input are integers.  2 \leq N \leq 10^5  1 \leq M \leq 2 \times 10^5  1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)  A_i \neq B_i\ (1 \leq i \leq M)  One can travel between any two rooms by traversing passages.         Input  Input is given from Standard Input in the following format:  N  M  A_1  B_1  :  A_M  B_M       Output  If there is no way to place signposts satisfying the objective, print No .  Otherwise, print N lines. The first line should contain Yes , and the i -th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i .        Sample Input 1 4 4
1 2
2 3
3 4
4 2      Sample Output 1 Yes
1
2
2  If we place the signposts as described in the sample output, the following happens:   Starting in Room 2 , you will reach Room 1 after traversing one passage: (2) \to 1 . This is the minimum number of passages possible.  Starting in Room 3 , you will reach Room 1 after traversing two passages: (3) \to 2 \to 1 . This is the minimum number of passages possible.  Starting in Room 4 , you will reach Room 1 after traversing two passages: (4) \to 2 \to 1 . This is the minimum number of passages possible.   Thus, the objective is satisfied.       Sample Input 2 6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6      Sample Output 2 Yes
6
5
5
1
1  If there are multiple solutions, any of them will be accepted.

Solution (Python):
import sys
input = sys.stdin.readline
n, m = map(int,input().split())
C = [list(map(int,input().split())) for i in range(m)]

V = [0] * (n+1)
P = [-1] * (n+1)

M = [[] for i in range(n+1)]
for i in range(m):
    M[C[i][0]].append(C[i][1])
    M[C[i][1]].append(C[i][0])


Q = [1]
s = 0
V[1] = 1
while s < len(Q):
    i = Q[s]
    for x in M[i]:
        
        if V[x] == 0:
            V[x] = 1
            P[x] = i
            Q.append(x)
    s += 1
print("Yes")
for i in range(2, len(P)):
    print(P[i])

Solution (Python):
from collections import deque
n,m,*L=map(int,open(0).read().split())
con=[[]for _ in range(n)]
for a,b in zip(*[iter(L)]*2):
	con[a-1].append(b-1)
	con[b-1].append(a-1)
q=deque([0])
par=[-1]*n
par[0]=1
while q:
	cur=q.popleft()
	for nxt in con[cur]:
		if par[nxt]==-1:
			par[nxt]=cur+1
			q.append(nxt)
if any(x==-1 for x in par):
	print("No")
	exit()
print("Yes")
print("\n".join(map(str,par[1:])))

Solution (Python):
N, M = map(int, input().split())
edge = [[] for _ in range(N)]
for i in range(M):
    a,b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

from collections import deque
def solve():
    prev = [-1]*N
    prev[0]='Yes'
    d = deque([(0,0)])
    while len(d):
        v,cnt = d.popleft()
        for u in edge[v]:
            if prev[u]==-1:
                prev[u]=v+1
                d.append((u,cnt+1))
    if -1 in prev:
        return ['No']
    return prev
print(*solve(),sep='\n')

Solution (Python):
from collections import deque

import sys
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*M:
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

visited = [0]*N
path = [0]*N

q = deque()
q.append((0, -1))

while q:
    v, p = q.popleft()
    if visited[v]:
        continue

    visited[v] = 1
    path[v] = p

    for u in G[v]:
        if visited[u]:
            continue
        q.append((u, v))

print('Yes')
for p in path[1:]:
    print(p+1)

Solution (Python):
import heapq
n, m = map(int, input().split())
G = [[] for i in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
inf = 10**20
D = [inf for i in range(n)]
D[0] = 0
M = [None for i in range(n)]
Q = [(0, 0)]
heapq.heapify(Q)
while Q:
    _, x = heapq.heappop(Q)
    for y in G[x]:
        if D[x] + 1 < D[y]:
            D[y] = D[x] + 1
            M[y] = x
            heapq.heappush(Q, (D[y], y))
print('Yes')
for i in range(1, n):
    print(M[i] + 1)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline().strip()


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def rangeI(it, l, r):
    for i, e in enumerate(it):
        if l <= i < r:
            yield e
        elif l >= r:
            break

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

from heapq import heappush, heappop

def dijkstra(su):
    costs = [ INF ] * (n+1)

    hq = []
    hq.append((0, su))

    while hq:
        c, u = heappop(hq)
        
        if c >= costs[u]:
            continue
        costs[u] = c

        for v in edges[u]:
            heappush(hq, (c + 1, v))

    return costs



INF = 999999999999999999999999
MOD = 10**9+7

n,m = get_nums_l()
edges = [ [] for _ in range(n+1)]
for _ in range(m):
    a,b = get_nums_l()
    edges[a].append(b)
    edges[b].append(a)


costs = dijkstra(1)



ans = [0] * (n+1)
for u in range(2, n+1):
    c = costs[u]
    for v in edges[u]:
        if costs[v]+1 == c:
            ans[u] = v
            break

print("Yes")
for u in range(2,n+1):
    print(ans[u])

Solution (Python):
class Dijkstra():
    """ ダイクストラ法
    重み付きグラフにおける単一始点最短路アルゴリズム

    * 使用条件
        - 負のコストがないこと
        - 有向グラフ、無向グラフともにOK

    * 計算量はO(E*log(V))

    * ベルマンフォード法より高速なので、負のコストがないならばこちらを使うとよい
    """
    class Edge():
        """ 重み付き有向辺 """
        def __init__(self, _to, _cost):
            self.to = _to
            self.cost = _cost

    def __init__(self, V):
        """ 重み付き有向辺
        無向辺を表現したいときは、_fromと_toを逆にした有向辺を加えればよい

        Args:
            V(int): 頂点の数
        """
        self.G = [[] for i in range(V)]  
        self._E = 0  
        self._V = V  

    @property
    def E(self):
        """ 辺数
        無向グラフのときは、辺数は有向グラフの倍になる
        """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        """ 2頂点と、辺のコストを追加する """
        self.G[_from].append(self.Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):
        """ 始点sから頂点iまでの最短路を格納したリストを返す
        Args:
            s(int): 始点s
        Returns:
            d(list): d[i] := 始点sから頂点iまでの最短コストを格納したリスト。
                     到達不可の場合、値はfloat("inf")
        """
        import heapq
        que = []  
        d = [10**15] * self.V
        d[s] = 0
        heapq.heappush(que, (0, s))  

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                
                e = self.G[v][i]  
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost  
                    heapq.heappush(que, (d[e.to], e.to))  
        return d

import sys

input=sys.stdin.readline

N,M=map(int,input().split())
pro=Dijkstra(N)
edge=[[] for i in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    pro.add(a-1,b-1,1)
    pro.add(b-1,a-1,1)
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

data=pro.shortest_path(0)

ans=[]
for i in range(1,N):
    for j in edge[i]:
        if data[j]==data[i]-1:
            ans.append(j)
            break
    else:
        print("No")
        exit()

print("Yes")
for i in range(N-1):
    print(ans[i]+1)

Solution (Python):
from collections import deque
n,m = map(int,input().split())
path = [[] for _ in range(n)]
inf = float('inf')
for _ in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
que = deque([0])
parent = [-1]*n
d = [inf]*n
d[0] = 0
while que:
    p = que.popleft()
    for nxt in path[p]:
        if d[nxt] <= d[p]+1:continue
        d[nxt] = d[p] + 1
        parent[nxt] = p+1
        que.append(nxt)
print('Yes')
print('\n'.join(str(i) for i in parent[1:]))

Solution (Python):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
adj = [[] for i in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)

from collections import deque
q = deque()
q.append(0)

parents = [-1] * N
parents[0] = 0
while q:
    nex = q.popleft()
    for v in adj[nex]:
        if parents[v] == -1:
            parents[v] = nex
            q.append(v)

out = ['Yes']
for i in range(1, N):
    out.append(str(parents[i] + 1))
print(*out, sep='\n')

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
n,m=map(int,input().split())
l=[[] for i in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  l[a-1].append(b-1)
  l[b-1].append(a-1)
ans=[-1]*n
ans[0]=0
print('Yes')
d=deque([0])
while d:
  now=d.pop()
  for x in l[now]:
    if ans[x]<0:
      ans[x]=now
      d.appendleft(x)
for i in range(1,n):
  print(ans[i]+1)

Solution (Python):

from collections import deque
import sys
try:
    from typing import List
except ImportError:
    pass

YES = "Yes"  
NO = "No"  


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    G = [[] for _ in range(N)]  
    for Ai, Bi in zip(A, B):
        Ai -= 1
        Bi -= 1
        G[Ai].append(Bi)
        G[Bi].append(Ai)

    ans = [-1] * N
    ans[0] = 0
    q = deque([0])
    while q:
        s = q.popleft()
        for t in G[s]:
            if ans[t] >= 0:
                continue
            ans[t] = s
            q.append(t)

    if -1 in ans:
        print(NO)
        return

    print(YES)
    for ansi in ans[1:]:
        print(ansi + 1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    A = [int()] * (M)  
    B = [int()] * (M)  
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
for _ in range(M):
    A, B = map(int, input().split())
    G[A].append(B)
    G[B].append(A)
dist = [-1] * (N+1)
dist[1] = 0
queue = deque([1])
while queue:
    v = queue.popleft()
    for c in G[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            queue.append(c)
print('Yes')
for v in range(2, N+1):
    for c in G[v]:
        if dist[c] < dist[v]:
            print(c)
            break

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(m)]
graph = [[] for i in range(n+1)]
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
q = deque()
q.append(1)
ans = [0 for i in range(n+1)]
ans[1] = -1
while q:
  x = q.popleft()
  for y in graph[x]:
    if ans[y] == 0:
      ans[y] = x
      q.append(y)
print("Yes")
print(*ans[2:],sep="\n")

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split()))for i in range(m)]
ans=[0]*n
edge=[[]for _ in range(n)]
for a,b in ab:
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)
visited={0}
stack=[0]
for i in stack:
  for j in edge[i]:
    if j in visited:continue
    stack.append(j)
    visited.add(j)
    ans[j]=i+1
print("Yes")
print(*ans[1:])

Solution (Python):
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

n, m = nm()
G = [list() for _ in range(n)]
for _ in range(m):
    u, v = nm()
    u -= 1; v -= 1
    G[u].append(v)
    G[v].append(u)

michi = [0]*n
dist = [n]*n
dist[0] = 0

q = deque()
q.append(0)
while q:
    v = q.popleft()
    for x in G[v]:
        if dist[x] > dist[v] + 1:
            dist[x] = dist[v] + 1
            michi[x] = v + 1
            q.append(x)
print('Yes')
prn(michi[1:])

Solution (Python):
from collections import deque
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    g[b - 1].append(a - 1)
q = deque([0])
d = [-1] * n
d[0] = 0
ans = [0] * n
print('Yes')
while q:
    p = q.popleft()
    for node in g[p]:
        if d[node] == -1:
            d[node] = d[p] + 1
            q.append(node)
        else:
            if d[node] + 1 == d[p]:
                ans[p] = node + 1
for i in range(1, n):
    print(ans[i])

Solution (Python):
def dijkstra_heap(graph: list, s: int, V: int=None):
    """
    :param graph: - adjacency list [(v, w), ...]:
    :param s: start
    :param V: number of vertices
    :return: dist, pred
    """
    import heapq

    pq = []
    INF = float("inf")

    if V is None:
        V = len(graph)

    dist = [INF] * V
    pred = [-1] * V

    dist[s] = 0

    heapq.heappush(pq, (0, s))

    while pq:
        d, u = heapq.heappop(pq)
        if d > dist[u]:
            continue

        for v, w in graph[u]:
            new_dist = dist[u] + w
            if new_dist < dist[v]:
                heapq.heappush(pq, (new_dist, v))
                dist[v] = new_dist
                pred[v] = u
    return dist, pred


def main():
    N, M = [int(x) for x in input().split()]
    graph = [list() for _ in range(N)]
    for _ in range(M):
        a, b = [int(x) for x in input().split()]
        a -= 1
        b -= 1
        graph[a].append((b,1))
        graph[b].append((a,1))
    dist, pred = dijkstra_heap(graph, 0)
    if any(d == float("inf") for d in dist):
        print("No")
    else:
        print("Yes")
        for i in range(1,N):
            print(pred[i]+1)
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10000)
    main()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left
from heapq import *
import sys

INF = 10 ** 17
MOD = 998244353

def check(mid, points, origin, rotate):
    height = origin * math.cos(math.radians(rotate))
    
    cs = math.cos(math.radians(rotate))
    sn = math.sin(math.radians(rotate))
    for px, py in points:
        py = py - origin
        rx, ry = px*cs - py * sn, px * sn + py * cs
        if abs(rx) > height + 0.0000001 or abs(ry) > height + 0.0000001:
            
            return False

    return True


class Segtree_op():
    def __init__(self, n):
        self.size = 1
        while (n >= 1):
            self.size = self.size << 1
            n = n // 2

        self.arr = [self.unit() for i in range(self.size * 2)]

    def op(self, lch, rch):
        return lch + rch

    def unit(self):
        return 0

    def update(self, k, val):
        k += self.size - 1
        self.arr[k] = val
        while (k):
            k = (k - 1) // 2
            self.arr[k] = self.op(self.arr[k * 2 + 1], self.arr[k * 2 + 2])

    def query(self, l, r):
        L = l + self.size
        R = r + self.size
        s = self.unit()
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(s, self.arr[R - 1])

            if L & 1:
                s = self.op(s, self.arr[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return s

    def show(self):
        idx = 1
        while (idx <= self.size):
            print(self.arr[idx - 1:idx * 2 - 1])
            idx *= 2


def solve():
    n, m = getList()
    graph = [[] for i in range(n)]
    michi = [-1  for i in range(n)]
    det = [INF for i in range(n)]
    det[0] = 0

    for _ in range(m):
        a, b = getList()
        a, b = a-1, b-1
        graph[a].append(b)
        graph[b].append(a)

    h = []
    for nx in graph[0]:
        heappush(h, (1, nx))
        det[nx] = 1
        michi[nx] = 1

    while(h):
        
        _, tgt = heappop(h)
        for nx in graph[tgt]:
            if det[nx] == INF:
                michi[nx] = tgt+1
                det[nx] = det[tgt] + 1
                heappush(h, (det[nx], nx))

    
    for d in det:
        if d == INF:
            print("No")
            return
    print("Yes")
    for m in michi[1:]:
        print(m)
def main():
    n = getN()
    for _ in range(n):
        solve()
if __name__ == "__main__":
    solve()

Solution (Python):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, m = na()
g = [[] for _ in range(n)]
for i in range(m):
    a, b = na()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
print("Yes")
q = [0]
ds = [9999999] * n
ds[0] = 0
z = 0
pre = [-1] * n
while z < len(q):
    cur = q[z]
    for e in g[cur]:
        if ds[e] > ds[cur] + 1:
            ds[e] = ds[cur] + 1
            pre[e] = cur
            q.append(e)
    z += 1

for i in range(1, n):
    print(pre[i]+1)

Solution (Python):
from collections import deque

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

g = [[] for _ in range(n)]

for i in range(m):
    a,b = map(int, input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

d = [10000000]*n
d[0] = 0
q = deque([0])

prev = [0] * n

while q:
    p = q.popleft()
    for v in g[p]:
        if d[v] == 10000000:
            d[v] = d[p] + 1
            prev[v] = p
            q.append(v)

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

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    N, M = map(int, input().split())
    adj = [[] for _ in range(N+1)]
    for _ in range(M):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    print("Yes")
    for v in range(2, N+1):
        print(par[v])


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import queue
sys.setrecursionlimit(10**7)


def inpl():
    return list(map(int, input().split()))


N, M = inpl()

lines = [[] for _ in range(N)]
for _ in range(M):
    a, b = inpl()
    a -= 1
    b -= 1
    lines[a].append(b)
    lines[b].append(a)


ans = [-1] * N


q = queue.Queue()
q.put((0, 0))
while not q.empty():
    s, b = q.get()

    if ans[s] != -1:
        continue
    else:
        ans[s] = b
        for t in lines[s]:
            q.put((t, s))


print("Yes")
for a in ans[1:]:
    print(a + 1)

Solution (Python):
from collections import deque
n,m=map(int,input().split())
G=[[] for i in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
D=deque([0])
V=[0]*n
V[0]=1
P=[0]*n
while len(D)>0:
  x=D[0]
  D.popleft()
  for i in range(len(G[x])):
    if V[G[x][i]]==0:
      V[G[x][i]]=1
      P[G[x][i]]=x+1
      D.append(G[x][i])
print("Yes")
for i in range(1,n):
  print(P[i])

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    v = [[] for i in range(n)]
    for _ in range(m):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
    q = [(0,0)]
    d = [float("inf")]*n
    d[0] = 0
    pre = [None]*n
    while q:
        dx,x = heappop(q)
        nd = dx+1
        for y in v[x]:
            if nd < d[y]:
                d[y] = nd
                pre[y] = x
                heappush(q,(nd,y))
    if float("inf") in d:
        print("No")
        return
    print("Yes")
    for i in pre[1:]:
        print(i+1)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import deque
def bfs(graph, N, start):
    visited = [0] * N
    visited[start] = 1
    que = deque([start])
    while que:
        node = que.popleft()
        for n in graph[node]:
            if not visited[n]:
                visited[n] = node + 1
                que.append(n)
    return visited

N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(lambda n: int(n) - 1, input().split())
    graph[a].append(b)
    graph[b].append(a)
visited = bfs(graph, N, 0)[1:]
if all(visited):
    print("Yes")
    print(*visited, sep="\n")
else:
    print("No")

Solution (Python):
from collections import deque

N, M = map(int, input().split())
d = [set() for _ in range(N+1)]
for _ in range(M):
    a, b = map(int, input().split())
    d[a].add(b)
    d[b].add(a)

queue=deque([1])
vs = set([1])
parents = [0] * (N+1)
dist = [0] * (N+1)
while queue:
    v = queue.popleft()
    for u in d[v]:
        if u in vs:
            continue
        parents[u] = v
        vs.add(u)
        queue.append(u)
        dist[u] = dist[v] + 1
print("Yes")
for x in parents[2:]:
    print(x)

Solution (Python):
from collections import deque
n,m=map(int,input().split())
graph=[[] for i in range(n)]
sirusi=[0]*n
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    graph[a].append(b)
    graph[b].append(a)
D=deque([0])
visited=[False]*n
visited[0]=True
while D:
    v=D.popleft()
    for i in graph[v]:
        if visited[i]:continue
        visited[i]=True
        sirusi[i]=v
        D.append(i)
print("Yes")
for i in range(1,n):
    print(sirusi[i]+1)