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
input = sys.stdin.buffer.readline





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




from collections import deque

def main():
	n,m=MI()

	G=[[] for _ in range(n+1)]

	for _ in range(m):
		a,b=MI()
		G[a].append(b)
		G[b].append(a)

	ans=[0]*(n+1)

	Q=deque([1])
	sumi=set()

	while Q:
		now=Q.popleft()

		for next in G[now]:
			if next in sumi:
				continue
			else:
				Q.append(next)
				ans[next]=now
				sumi.add(next)

	print("Yes")

	print(*ans[2:],sep="\n")






































if __name__ == "__main__":
	main()

Solution (Python):








def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




from collections import deque

def main():
	n,m=MI()

	G=[[] for _ in range(n+1)]

	for _ in range(m):
		a,b=MI()
		G[a].append(b)
		G[b].append(a)

	ans=[0]*(n+1)

	Q=deque([1])
	sumi=set()

	while Q:
		now=Q.popleft()

		for next in G[now]:
			if next in sumi:
				continue
			else:
				Q.append(next)
				ans[next]=now
				sumi.add(next)

	print("Yes")

	print(*ans[2:],sep="\n")






































if __name__ == "__main__":
	main()

Solution (Python):








def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




from collections import deque

def main():
	n,m=MI()

	G=[[] for _ in range(n+1)]

	for _ in range(m):
		a,b=MI()
		G[a].append(b)
		G[b].append(a)

	ans=[0]*(n+1)

	Q=deque([1])
	sumi=set()

	while Q:
		now=Q.popleft()

		for next in G[now]:
			if next in sumi:
				continue
			else:
				Q.append(next)
				ans[next]=now
				sumi.add(next)

	print("Yes")

	print(*ans[2:],sep="\n")






































if __name__ == "__main__":
	main()

Solution (Python):
import collections

def main():
    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)
    q = collections.deque()
    q.append(1)
    
    check=[0]*(n+1)
    check[1]=1
    ans=[0]*(n+1)
    while len(q)!=0:
        v=q.popleft()
        for u in g[v]:
            if check[u] == 0:
                check[u] = 1
                ans[u] = v
                q.append(u)

    for i in range(2,n+1):
        if ans[i] == 0:
            print('No')
            return

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

main()

Solution (Python):
from collections import deque
N, M = map(int, input().split())
nodes = [set() for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    nodes[a-1].add(b-1)
    nodes[b-1].add(a-1)
ans = [-1 for i in range(N)]
ans[0] = 0
q = deque([])
q.append(0)

cur = 0
done = set()
done.add(0)
while q:
    node = q.popleft()
    for x in nodes[node]:
        if x in done:
            continue
        q.append(x)
        done.add(x)
        ans[x] = node + 1
if -1 in ans:
    print('No')
else:
    print('Yes')
    for x in ans[1:]:
        print(x)

Solution (Python):
from collections import deque
N, M = map(int, input().split())
Mlist = [[] for _ in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    Mlist[a].append(b)
    Mlist[b].append(a)

ans = [0] * (N+1)

d = deque()
d.append(1)
count = 0
while count != N:
    Q = d.popleft()
    for i in Mlist[Q]:
        if ans[i] == 0:
            ans[i] = Q
            d.append(i)
            count = count+1
print('Yes')
for i in range(N-1):
    print(ans[i+2])

Solution (Python):
from collections import deque

n,m,*ab = map(int, open(0).read().split())

to = [[] for _ in range(n+1)]
for i in range(0, m*2, 2):
    a = ab[i]
    b = ab[i+1]
    to[a].append(b)
    to[b].append(a)

ans = [None]*(n+1)
q = deque([1])
while q:
    p = q.pop()
    children = to[p]
    for child in children:
        if ans[child] is None:
            ans[child] = p
            q.appendleft(child)

print("Yes")
print("\n".join(map(str, ans[2:])))

Solution (Python):
from collections import deque
n,m,*ab = map(int, open(0).read().split())

to = [[] for _ in range(n+1)]
ans = [None] * (n+1)
for i in range(0, m*2, 2):
    a = ab[i]
    b = ab[i+1]
    to[a].append(b)
    to[b].append(a)

q = deque([1])
while q:
    p = q.pop()
    children = to[p]
    for child in children:
        if ans[child] is None:
            ans[child] = p
            q.appendleft(child)

print("Yes")
print("\n".join(map(str, ans[2:])))

Solution (Python):
from collections import deque

def main():
    n, m = map(int, input().split())
    to = [[] for _ in range(n+1)]
    ans = [None]*(n+1)
    for _ in range(m):
        a, b = map(int, input().split())
        to[a].append(b)
        to[b].append(a)
    q = deque([1])
    while q:
        p = q.pop()
        children = to[p]
        for child in children:
            if ans[child] is None:
                ans[child] = p
                q.appendleft(child)
    print("Yes")
    print("\n".join(list(map(str, ans[2:]))))
    

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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


prenodes = [-1 for i in range(n)]

G = [[] for i in range(n)]
ab = []
for i in range(m):
    ab.append(list(map(int, input().split())))

for i in range(m):
    G[ab[i][0]-1].append(ab[i][1]-1)
    G[ab[i][1]-1].append(ab[i][0]-1)



prenodes[0] = 0
que = deque([0])

while len(que) != 0:
    v = que.popleft()

    for vi in G[v]:
        if prenodes[vi] != -1:
            continue
        que.append(vi)
        
        prenodes[vi] = v

ans = "Yes"
for i in range(n):
    if prenodes[i] == -1:
        ans = "No"
        break

if ans == "No":
    print(ans)
else:
    print(ans)
    for i in range(1, n):
        print(prenodes[i]+1)

Solution (Python):
import collections

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

q=collections.deque()
q.append(1)
visit=[-1]*(n+1)
visit[1] = 1
ans=[0]*(n+1)
while q:
	v=q.popleft()
	for i in g[v]:
		if visit[i] == -1:
			ans[i] = v
			visit[i] = 1
			q.append(i)
print('Yes')
for i in range(2,n+1):
	print(ans[i])

Solution (Python):
import sys
from collections import deque
n,m = map(int, sys.stdin.readline().rstrip("\n").split())
edge = [[] for i in range(n)]
for i in range(m):
    a,b = map(int, sys.stdin.readline().rstrip("\n").split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
footprint = [10**9+7]*n
q = deque()
footprint[0] = 0
for i in edge[0]:
    q.append((i,0))
while q:
    nn,fn = q.popleft()
    if footprint[nn] == 10**9+7:
        footprint[nn] = fn
        for i in edge[nn]:
            if footprint[i] == 10**9+7:
                q.append((i,nn))
if 10**9+7 in footprint:
    print('No')
else:
    print('Yes')
    for i in range(1,n):
        print(footprint[i]+1)

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

n, m = map(int, input().split())
roots = [[] for _ in range(n)]
signs = [-1 for _ in range(n)]

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

queue = deque([0])
signs[0] = 0

while queue:
    room = queue.popleft()
    for root in roots[room]:
        if signs[root] == -1:
            signs[root] = room
            queue.append(root)

print('Yes')
for i in signs[1:]:
    print(i + 1)

Solution (Python):
import queue
I=lambda:map(int,input().split())
N,M=I()
ls=[[] for _ in range(N+1)]
for _ in[0]*M:
  A,B=I()
  ls[A]+=[B]
  ls[B]+=[A]
q = queue.Queue()
v=[-1]*(N+1)
v[0]=v[1]=0
q.put(1)
while not(q.empty()):
  top=q.get()
  for l in ls[top]:
    if v[l]<0:
      v[l]=top
      q.put(l)
if -1 in v:
  print("No")
else:
  print("Yes")
  [print(k) for k in v[2::]]

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7

N,M = I()
A = [I() for _ in range(M)]
links = [[] for _ in range(N)]
ans = [-1]*(N)
ans[0] = 0
for a,b in A:
    links[a-1].append(b-1)
    links[b-1].append(a-1)
que = deque([0])
while que:
    i = que.pop()
    for j in links[i]:
        if ans[j] == -1:
            ans[j] = i+1
            que.appendleft(j)
print('Yes')
for i in range(1,N):
    print(ans[i])

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(lambda x:int(x)-1,input().split())
    
    g[a].append(b)
    g[b].append(a) 
to = {}   
dist = [-1 for _ in range(n)]
dist[0] = 0
q = deque()
q.append(0)
while (len(q) > 0):
    v = q.popleft()
    for nv in g[v]:
        if (dist[nv] != -1):continue
        dist[nv] = dist[v] + 1
        q.append(nv)
        to[nv] = v
        
print('Yes')
for i in range(1,n):
    print(to[i]+1)

Solution (Python):
n,m=map(int,input().split())
ans=[0]*n
num1=[[] for _ in range(n)]
def ap(num01,num02):
    num1[num01-1].append(num02)
    num1[num02-1].append(num01)
for i in range(m):
    a,b=map(int,input().split())
    ap(a,b)
queue=[1]
def apq(num01):
    queue.append(num01)
def qp():
    return queue.pop(0)
for i in range(n):
    if len(queue)==0:
        break
    num=qp()
    for j in range(len(num1[num-1])):
        if ans[num1[num-1][j]-1]==0:
            apq(num1[num-1][j])
            ans[num1[num-1][j]-1]=num
if ans.count(0)!=0:
    print("No")
else:
    print("Yes")
    for i in range(1,n):
        print(ans[i])

Solution (Python):
from collections import deque

n,m=map(int,input().split())
graph=[[] for _ in range(n+1)]

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

ans=[-1]*(n+1)
ans[0]=0
ans[1]=0

d=deque()
d.append(1)

while d:
    v=d.popleft()
    for i in graph[v]:
        if ans[i]!=-1:
            continue
        ans[i]=v
        d.append(i)

ans=ans[2:]
print("Yes")
print(*ans,sep="\n")

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

N,M = map(int,readline().split())
G = [[] for i in range(N)]
for i in range(M):
  a,b = map(int,readline().split())
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)

ans = [None] * N
from collections import deque
q = deque([[0,-1]])
while q:
  v,parent = q.popleft()
  if ans[v] is not None:
    continue
  ans[v] = parent
  for child in G[v]:
    if ans[child] is None:
      q.append([child, v])
print("Yes")
for i in range(1,len(ans)):
  print(ans[i] + 1)

Solution (Python):
from collections import deque

N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]

graph = [[] for _ in range(N + 1)]
for a, b in X:
    graph[a].append(b)
    graph[b].append(a)

prevs = [-1] * (N + 1)
prevs[1] = 0
q = deque([1])

while q:
    u = q.popleft()
    for v in graph[u]:
        if prevs[v] < 0:
            prevs[v] = u
            q.append(v)
            
print("Yes")
print(*prevs[2:], sep="\n")

Solution (Python):
from collections import deque

N,M=map(int,input().split())
m=[[] for _ in range(N)]
n=[-1]*N
n[0]=0
for _ in range(M):
	a,b=map(int,input().split())
	m[a-1].append(b-1)
	m[b-1].append(a-1)

def bfs():
	que=deque([])
	que.append(0)
	while que:
		p=que.popleft()
		for i in m[p]:
			if n[i]==-1:
				que.append(i)
				n[i]=p+1

bfs()

print('Yes')
for j in range(1,N):
	print(n[j])

Solution (Python):
def main():
    from collections import deque

    N, M = map(int, input().split())
    connected_rooms = [[] for i in range(N + 1)]
    
    for i in range(M):
        a, b = map(int, input().split())
        connected_rooms[a].append(b)
        connected_rooms[b].append(a)
    
    def Breadth_First_Search(root):
        queue = deque([root]) 
        distance = [None] * (N + 1) 
        distance[root] = 0
        
        while (queue): 
            ver = queue.popleft()
            
            for index in connected_rooms[ver]:
                if (distance[index] == None):
                    distance[index] = distance[ver] + 1
                    queue.append(i)
        
        return (distance)
    
    dist = [None] * (N + 1)
    dist[0] = True
    
    Q = deque([1])
    while (Q):
        index = Q.popleft()
        for j in connected_rooms[index]:
            if (dist[j] == None):
                dist[j] = index
                Q.append(j)
    
    if (all(dist)):
        print('Yes')
        for i in dist[2:]:
            print(i)
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque
 
    n,m =map(int,input().split())
    abl=[[] for i in range(n+1)]
    for i in range(m):
        a,b = map(int,input().split())
        abl[a].append(b)
        abl[b].append(a)
 
    an=[0]*(n+1)
    an[0]=1
    tg = deque([1])
    while len(tg):
        itg = tg.popleft()
        for j in abl[itg]:
            if an[j]==0:
                an[j]=itg
                tg.append(j)

    if all(an):
        print('Yes')
        for j in an[2:]:
            print(j)
    else:
        print('No')
 
main()

Solution (Python):
from collections import deque
n, m = [int(i) for i in input().split()]
rooms = [[] for i in range(n)]
dist = [-1] * n
for i in range(m):
    a, b = [int(i) for i in input().split()]
    rooms[a-1].append(b)
    rooms[b-1].append(a)

que = deque()
que.append(1)
dist[0] = 0
ans = [0] * n
while(len(que) != 0):
    v = que.popleft()
    for i in rooms[v-1]:
        if dist[i-1] == -1:
            dist[i-1] = dist[v-1] + 1
            que.append(i)
            ans[i-1] = v
print("Yes")
for i in ans[1:]:
    print(i)

Solution (Python):
n,m = map(int,input().split())
root = [[] for _ in range(n)]
for i in range(m):
    aa,bb =map(int,input().split())
    aa-=1
    bb-=1
    root[aa].append(bb)
    root[bb].append(aa)

from collections import deque

q = deque([(-1,0)])
been=[0]*n
ans=[0]*n
been[0]=1
while q:
    parent,now = q.popleft()
    ans[now]=parent
    
    for to in root[now]:
        if been[to]==0:
            q.append((now,to))
            been[to]=1

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

Solution (Python):
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *AB = map(int, read().split())
graph = [[] for _ in range(N + 1)]
for A, B in zip(*[iter(AB)] * 2):
    graph[A].append(B)
    graph[B].append(A)

answer = [-1] * (N + 1)
queue = deque([1])
answer[1] = 0
while queue:
    room = queue.popleft()
    for i in graph[room]:
        if answer[i] != -1:
            continue
        answer[i] = room
        queue.append(i)

print('Yes')
print('\n'.join(map(str, answer[2:])))

Solution (Python):
from collections import deque

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

graph = [[] for _ in range(n+1)] 
for i in range(m):
  a,b = map(int, input().split())
  graph[a].append(b)
  graph[b].append(a)



dist = [None] * (n+1)
dist[0] = 0
dist[1] = 0

d = deque()
d.append(1)

while d:
    v = d.popleft()
    for i in graph[v]:
        if dist[i] is None: 
            dist[i] = v
            d.append(i)

ans = dist[2:]

if all(d == None for d in dist):
  print('No')
  exit()

print('Yes')
for a in ans:
  print(a)

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())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

que = deque([0])
guideposts = [-1] * N
guideposts[0] = None

while que:
    n = que.pop()
    for to in G[n]:
        if guideposts[to] != -1:
            continue
        guideposts[to] = n
        que.appendleft(to)

print('Yes')
print(*[guisepost + 1 for guisepost in guideposts[1:]], sep="\n")

Solution (Python):
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'
    
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase

read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
input=lambda: sys.stdin.readline().rstrip()

class Tree:
    def __init__(self,inp_size=None,ls=None,init=True,index=1):
        self.LCA_init_stat=False
        self.ETtable=[]
        if init:
            if ls==None:
                self.stdin(inp_size,index=index)
            else:
                self.node_size=len(ls)+1
                self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index)
        return
 
    def stdin(self,inp_size=None,index=1):
        if inp_size==None:
            self.node_size=int(input())
        else:
            self.node_size=inp_size
        self.edges,_=GI(self.node_size,self.node_size-1,index=index)
        return
    
    def listin(self,ls,index=0):
        self.node_size=len(ls)+1
        self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index)
        return
 
    def dfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v
 
    def bfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.popleft()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v
 
    def parent(self,x):
        return self.dfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1)
 
    def topological_sort(self,x):  
        tps=[]
        q=deque([x])
        v=[None]*self.node_size
        v[x]=0
        while q:
            c=q.popleft()
            tps.append(c)
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=0
        return tps
 
    def EulerTour(self,x):
        q=deque()
        q.append(x)
        self.depth=[None]*self.node_size
        self.depth[x]=0
        self.ETtable=[]
        self.ETdepth=[]
        self.ETin=[-1]*self.node_size
        self.ETout=[-1]*self.node_size
        cnt=0
        while q:
            c=q.pop()
            if c<0:
                ce=~c
            else:
                ce=c
                for nb,d in self.edges[ce]:
                    if self.depth[nb]==None:
                        q.append(~ce)
                        q.append(nb)
                        self.depth[nb]=self.depth[ce]+1
            self.ETtable.append(ce)
            self.ETdepth.append(self.depth[ce])
            if self.ETin[ce]==-1:
                self.ETin[ce]=cnt
            else:
                self.ETout[ce]=cnt
            cnt+=1
        return
    
    def LCA_init(self,root):
        self.EulerTour(root)
        self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
        
        self.LCA_init_stat=True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            self.LCA_init(root)
        xin,xout=self.ETin[x],self.ETout[x]
        yin,yout=self.ETin[y],self.ETout[y]
        a=min(xin,yin)
        b=max(xout,yout,xin,yin)
        id_of_min_dep_in_et=self.st.query_id(a,b+1)
        return self.ETtable[id_of_min_dep_in_et]
 
    def __str__(self):
        return  str(self.edges)
 
    def show(self):
        if all([all([d==1 for nd,d in edge]) for edge in self.edges]):
            print( [[nd for nd,d in edge] for edge in self.edges] )
        else:
            print(self)
 
class SparseTable: 
    def __init__(self,ls,init_func=min,init_idl=float('inf')):
        self.func=init_func
        self.idl=init_idl
        self.size=len(ls)
        self.N0=self.size.bit_length()
        self.table=[ls[:]]
        self.index=[list(range(self.size))]
        self.lg=[0]*(self.size+1)
        
        for i in range(2,self.size+1):
            self.lg[i]=self.lg[i>>1]+1
 
        for i in range(self.N0):
            tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
            tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
            self.table+=[tmp]
            self.index+=[tmp_id]
    
    
    def query(self,l,r):
        if r>self.size:r=self.size
        
        N=self.lg[r-l]
        return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
    
    
    def query_id(self,l,r):
        if r>self.size:r=self.size
        
        N=self.lg[r-l]
        a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
        if self.table[0][a]==self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))]):
            b=a
        return b
    
    
    def right_bound(self,pos,x):
        k=(self.size-pos).bit_length()
        for j in range(k)[::-1]:
            nx=pos+(1<<j)
            if nx<n and self.query(pos,nx+1)<=x:
                pos+=(1<<j)
        return pos
    
    
    def left_bound(self,pos,x):
        k=pos.bit_length()
        for j in range(k)[::-1]:
            nx=pos-(1<<j)
            if 0<=nx and self.query(nx,pos+1)<=x:
                pos-=(1<<j)
        return pos
 
    def __str__(self):
        return str(self.table[0])
 
    def print(self):
        for i in self.table:
            print(*i)


show_flg=False
show_flg=True
ans=0


class Graph:
    def __init__(self,inp_node_size=None,inp_edge_size=None,ls=None,init=True,Directed=False,index=1):
        if init:
            if ls==None:
                self.stdin(inp_node_size,inp_edge_size,Directed=Directed,index=index)
            else:
                self.listin(ls,Directed=Directed,index=index)
        else:
            self.edges=[]
        return
 
    def stdin(self,inp_node_size=None,inp_edge_size=None,Directed=False,index=1):
        if (inp_node_size,inp_edge_size)==(None,None):
            self.node_size,self.edge_size=LI()
            self.edges,_=GI(self.node_size,self.edge_size,Directed=Directed,index=index)
        else:
            self.node_size=inp_node_size
            self.edge_size=inp_edge_size
            self.edges,_=GI(self.node_size,self.edge_size,Directed=Directed,index=index)
        return
    
    def listin(self,inp_node_size,ls,Directed=False,index=0):
        self.node_size=inp_node_size
        self.edge_size=len(ls)
        self.edges,_=GI(self.node_size,self.edge_size,ls,Directed=Directed,index=index)
        return
 
    def dfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v
 
    def bfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0):
        q=deque([x])
        v=[None]*self.node_size
        v[x]=root_v
        while q:
            c=q.popleft()
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=func(c,v[c],nb,d)
        return v

    def parent(self,x):
        return self.dfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1)
 
    def topological_sort(self,x):  
        tps=[]
        q=deque([x])
        v=[None]*self.node_size
        v[x]=0
        while q:
            c=q.popleft()
            tps.append(c)
            for nb,d in self.edges[c]:
                if v[nb]==None:
                    q.append(nb)
                    v[nb]=0
        return tps

    def __str__(self):
        return  str(self.edges)
 
    def show(self):
        if all([all([d==1 for nd,d in edge]) for edge in self.edges]):
            print( [[nd for nd,d in edge] for edge in self.edges] )
        else:
            print(self)

gr=Graph(init=False)
'''
n,m=LI()
ls=[]
for i in rep(m):
    ls+=[LI_()]


v=gr.dfs(0)
'''

gr=Graph()
v=gr.bfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1)

show('Yes')
for i in v[1:]:
    show(i+1)

Solution (Python):




import sys
input = sys.stdin.readline

n, m =  map(int, input().split()) 
pic = [[] for _ in range(n+1)] 

for _ in range(m): 
    a, b = map(int, input().split())
    pic[a].append(b) 
    pic[b].append(a) 


from collections import deque

def BFS(u): 
    queue = deque([u]) 
    dis = [None] * (n+1) 
    dis[u] = 0 
    ans = [-1] * (n+1) 
    while queue:
        v = queue.popleft() 
        for i in pic[v]: 
            if ans[i] == -1: 
                ans[i] = v
            if dis[i] is None:
                dis[i] = dis[v] + 1 
                queue.append(i)
    
    
    return ans

print('Yes')

tmp = BFS(1) 

for j in tmp[2:]:
    print(j)

Solution (Python):

import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()] 
g = [[] for _ in range(n)] 

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

from collections import deque
di= [0]*n
def bfs(u):
    queue = deque([u])
    d = [None] * n 
    d[u] = 0 
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                di[i] = v
                queue.append(i)
    return d


d = bfs(0)
print("Yes")
for i in range(1,n):
    print(di[i]+1)