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
from collections import deque
 
 
N, M = [int(i) for i in input().split()]
A = []
B = []
for i in range(M):
    l = [int(i) for i in input().split()]
    A.append(l[0])
    B.append(l[1])
 

G = [[] for _ in range(N)]
for i in range(M):
    G[A[i] - 1].append(B[i] - 1)
    G[B[i] - 1].append(A[i] - 1)
G = [list(set(x)) for x in G]
 
q = deque([])
dist = [-1] * N
ans = [-1] * N
dist[0] = 0
ans[0] = 0
q.appendleft(0)
while(len(q) > 0):
    v = q.pop()
    for nv in G[v]:
        if not dist[nv] == -1:
            continue
        dist[nv] = dist[v] + 1
        ans[nv] = v
        q.appendleft(nv)
 
for i in ans[1:]:
    if i == -1:
        print('No')
        sys.exit()
 
for i, v in enumerate(ans):
    if i == 0:
        print('Yes')
        continue
    print(v + 1)

Solution (Python):
from collections import deque


def main():
    n, m = map(int, input().split())
    g = [[] for _ in range(n)]

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

    q = deque([0])
    chk = [False] * n
    res = [0] * n

    while q:
        x = q.popleft()
        for y in g[x]:
            if chk[y]:
                continue
            chk[y] = True
            res[y] = x
            q.append(y)
    print("Yes")
    for i in res[1:]:
        print(i + 1)


if __name__ == '__main__':
    main()

Solution (Python):
N, M = map(int, input().split())
AB = []
cons = [[] for _ in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    cons[a - 1].append(b - 1)
    cons[b - 1].append(a - 1)
visited = [False for _ in range(N)]
visited[0] = True
Q = [0]
sirusi = [0 for _ in range(N)]
while Q:
    v = Q.pop(0)
    for i in cons[v]:
        if visited[i]: continue
        visited[i] = True
        sirusi[i] = v
        Q.append(i)
print("Yes")
for i in range(1, N):
    print(sirusi[i] + 1)

Solution (Python):
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)


queue=[1]
d=0
dist=[-1]*(N+1)
while queue:
  new_queue=set()
  for q in queue:
    if dist[q]!=-1:
      continue      
    dist[q]=d
    for v in graph[q]:
      if dist[v]==-1:
        new_queue.add(v)
        
  queue=list(new_queue)
  d+=1


parent=[-1]*(N+1)
for i in range(1,N+1):
  for v in graph[i]:
    if dist[v]==dist[i]+1:
      parent[v]=i


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

Solution (Python):
from collections import deque
n,m = map(int,input().split())
data = [[] for i in range(n)]
ans = [0]*n
for i in range(m):
    a,b =map(int,input().split())
    a -= 1
    b -= 1
    data[a].append(b)
    data[b].append(a)
hantei = [False]*n
hantei[0] = True
deq = deque([0])
while deq:
    k = deq.popleft()
    for i in data[k]:
        if hantei[i]:
            continue
        hantei[i] = True
        ans[i] = k
        deq.append(i)
print("Yes")
for i in range(1,n):
    print(ans[i]+1)

Solution (Python):


import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import breadth_first_order

N, M = list(map(int, input().split()))
ABs = np.array([list(map(int, input().split())) for i in range(M)])

row = ABs.T[0]-1
col = ABs.T[1]-1
data = [1] * (M)

csr = csr_matrix((data, (row, col)), shape=(N, N))
_, proc = breadth_first_order(csr, 0, directed=False)

if -9999 in proc[1:]:
  print('No')
else:
  print('Yes')
  print('\n'.join((proc[1:]+1).astype('str')))

Solution (Python):
from collections import deque

n,m=map(int, input().split())
g = [[] for _ in range(n + 1)]
d = deque()
ans = [0]*(n+1)

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

d = deque()
for i in g[1]: 
    d.append(i)
    ans[i]=1

while d:
    tmp = d[0]
    for i in g[tmp]:
        if ans[i] == 0:
            ans[i]=tmp
            d.append(i)
    d.popleft()

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

Solution (Python):
import collections

N, M = map(int, input().split())

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

queue = collections.deque([0])
visited = [0]*N
visited[0] = 1
while queue:
    x = queue.popleft()
    for i in G[x]:
        if visited[i] == 0:
            visited[i] = x + 1
            queue.append(i)
if 0 in visited:
    print("No")
else:
    print("Yes")
    print(*visited[1:], sep="\n")

Solution (Python):
from copy import deepcopy
def main():
    global flags, edges
    n, m = map(int, input().split())

    flags = {e:[n+1, n+1] for e in range(1, n + 1)}
    flags[1][1] = 0
    edges = {e:set() for e in range(1, n + 1)}
    edge_list = []
    for _ in range(m):
        a, b = map(int, input().split())
        edges[a].add(b)
        edges[b].add(a)
        edge_list.append((a, b))

    distance = 1
    node_next = list(edges[1])
    node_next_next = []
    while node_next:
        for next in node_next:
            if flags[next][1] > distance:
                flags[next][1] = distance
                node_next_next += edges[next]
        node_next = deepcopy(node_next_next)
        node_next_next = []
        distance += 1
    for edge in edge_list:
        e1 = edge[0]
        e2 = edge[1]
        if flags[e1][1] == flags[e2][1]:
            pass
        elif flags[e1][1] > flags[e2][1]:
            flags[e1][0] = min(flags[e1][0], e2)
        else:
            flags[e2][0] = min(flags[e2][0], e1)
    result = [flags[f][1] < (n + 1) for f in flags if f > 1]
    if not all(result):
        print('No')
    else:
        print('Yes')
        for i1 in range(2, n + 1):
            print(flags[i1][0])
            
if __name__ == '__main__':
    main()

Solution (Python):


import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import breadth_first_order

N, M = list(map(int, input().split()))

row = []
col = []
for i in range(M):
    r, c = list(map(int, input().split()))
    row.append(c-1)
    col.append(r-1)

data = [1] * (M)

csr = csr_matrix((data, (row, col)), shape=(N, N))
_, proc = breadth_first_order(csr, 0, directed=False)

if -9999 in proc[1:]:
  print('No')
else:
  print('Yes')
  print('\n'.join((proc[1:]+1).astype('str')))

Solution (Python):
from heapq import heappush, heappop
 
INT_MAX = 10 ** 9
 
 
def main():
  n, m = map(int, input().split())
  ab = (map(int, input().split()) for i in range(m))
  
  g = [[] for i in range(n+1)]
  gp = [INT_MAX] * (n+1)
  gf = [0] * (n+1)
  
  for a, b in ab:
    g[a].append(b)
    g[b].append(a)
  
  h = [(0, 1, 1)]
  
  while h:
    p, a, f = heappop(h)
    if p < gp[a]:
      gp[a] = p
      gf[a] = f
    else:
      continue
    for b in g[a]:
      heappush(h, (p + 1, b, a))
  
  r = gf[2:]
  s = 'Yes' if all(0 < i < INT_MAX for i in r) else 'No'
  print(s)
  if s == 'Yes':
    for i in r:
      print(i)
  
if __name__ == '__main__':
  main()

Solution (Python):
from collections import Counter 
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq
import numpy as np
import sys
sys.setrecursionlimit(10**6)




ceil=lambda x,y: (x+y-1)//y
input_list = lambda : list(map(int,input().split()))




def bfs(s,n,node):
    check=[-1 for _ in range(n)]
    check[0]=0
    
    side_num_arr=list(map(lambda x :len(x),node))
    l=max(side_num_arr)
    
    queue=[-1 for _ in range(n)]
    now=0
    queue[now]=s
    ans=[-1 for _ in range(n)]
    ans[0]=[0]
    last_ind=1
    while last_ind<n:
        
        if now>=n:
            return []
        check[queue[now]]=True
        if side_num_arr[queue[now]]<=0:
            now+=1
            continue
        for i in node[queue[now]]:
            edge=i[0]
            
            ind=i[1]
            if side_num_arr[edge]<=0 or check[edge]==True:
                continue
            side_num_arr[edge]-=1
            side_num_arr[queue[now]]-=1
            queue[last_ind]=edge
            last_ind+=1
            check[edge]=True
            
            ans[edge]=queue[now]
        now+=1
    return ans



n,m=input_list()
node=[[] for _ in range(n)]
for i in range(m):
    u,v=input_list()
    u-=1
    v-=1
    node[u].append([v,i])
    node[v].append([u,i])



ans=bfs(0,n,node)
if len(ans)==0:
    print("No")
    exit()

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

Solution (Python):
import sys
from collections import deque

n, m = map(int, input().split())
link = [ [] for i in range(n)]
for i in sys.stdin.readlines():
    a, b = map(int, i.split())
    link[a-1].append(b-1)
    link[b-1].append(a-1)

d = deque([0])
ans = [-1]*n
ans[0] = 0
while d:
    p = d.pop()
    for v in link[p]:
        if ans[v] == -1:
            ans[v] = p+1
            d.appendleft(v)

if -1 in ans:
    print("No")
else:
    print("Yes")
    print("\n".join(map(str, ans[1:])))

Solution (Python):
from collections import*
import itertools

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

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

for _ in range(m):
    ai, bi = map(int, input().split())
    posi[ai - 1] += [bi - 1]
    posi[bi-1] += [ai - 1]




p = [0]*n
q = deque([0])
while q:
    v = q.popleft()
    for c in posi[v]:
        if p[c] < 1:
            p[c] = v + 1
            q.append(c)



if 0 in p:
    print('No')
else:
    print('Yes', *p[1:], sep='\n')

Solution (Python):
import sys
from collections import deque


N, M = [int(i) for i in input().split()]
A = []
B = []
for i in range(M):
    l = [int(i) for i in input().split()]
    A.append(l[0])
    B.append(l[1])


G = [[] for _ in range(N)]
for i in range(M):
    G[A[i] - 1].append(B[i] - 1)
    G[B[i] - 1].append(A[i] - 1)


q = deque([])
ans = [-1] * N
ans[0] = 0
q.appendleft(0)
while(len(q) > 0):
    v = q.pop()
    for nv in G[v]:
        if not ans[nv] == -1:
            continue
        ans[nv] = v
        q.appendleft(nv)






for i, v in enumerate(ans):
    if i == 0:
        print('Yes')
        continue
    print(v + 1)

Solution (Python):
import collections

n,m = (int(x) for x in input().split())
d = [[] for i in range(n)]
ans = [0]*n
check = [0]*n
for i in range(m):
    a,b= (int(x) for x in input().split())
    d[a-1].append(b)
    d[b-1].append(a)

q = collections.deque()
q.append(1)

while len(q)!=0:
    v = q.popleft()
    for j in d[v-1]:
        if check[j-1]==0:
            check[j-1] = 1
            ans[j-1] = v
            q.append(j)

flag = True
for i in range(1,n):
    if check[i]==0:
        flag = False

if flag:
    print("Yes")
    for i in range(1,n):
        print(ans[i])
else:
    print("No")

Solution (Python):
import sys
from collections import deque

n, m = map(int, input().split())
link = [ [] for i in range(n)]
for i in sys.stdin.readlines():
    a, b = map(int, i.split())
    link[a-1].append(b-1)
    link[b-1].append(a-1)

d = deque([0])
ans = [-1]*n
ans[0] = 0
while d:
    p = d.popleft()
    for v in link[p]:
        if ans[v] == -1:
            ans[v] = p+1
            d.append(v)

if -1 in ans:
    print("No")
else:
    print("Yes")
    print("\n".join(map(str, ans[1:])))

Solution (Python):
from collections import deque

N, M = map(int, input().split())
AB = []
for i in range(M):
    ab = list(map(int, input().split()))
    AB.append(ab)


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


que = deque([1])
ans = [0 for _ in range(N)]

while que:
    x = deque.popleft(que)
    for i in graph[x-1]:
        if ans[i-1] != 0:
            continue
        ans[i-1] = x
        que.append(i)
for a in ans[1:]:
    if a == 0:
        print('No')
        break
else:
    print('Yes')
    print('\n'.join(map(str, ans[1:])))

Solution (Python):
import numpy as np
from collections import deque
n, m = [int(i) for i in input().split()]

e = [ [] for i in range(n)]



for _ in range(m):
    a, b = [int(i) for i in input().split()]
    e[a-1].append(b-1)
    e[b-1].append(a-1)
    
    
q = deque()
q.append(0)
ans = np.ones(n-1)
check = np.zeros(n)

while len(q) > 0:
    
    node = q.popleft()

    if node is not None:
        check[node] = 2
        con = e[node]
        for i in con:
            if check[i] ==0:
                ans[i-1] = int(node) + 1
                q.append(i)
                check[i] = 1

        

print('Yes')

for i in ans:
    print(int(i))

Solution (Python):
import math
from collections import deque
n,m=[int(_) for _ in input().split()]
A=[[int(_) for _ in input().split()] for l in range(m)]
R=deque([] for _ in range(n))
for a in A:
    R[a[0]-1].append(a[1]-1)
    R[a[1]-1].append(a[0]-1)
print("Yes")
Dis=deque([100000000]*n)
Dis[0]=0
PP=deque([0]*n)
l=deque()
l.append(0)
while l:
    x=l.popleft()
    for a in R[x]:
        if Dis[a]>Dis[x]+1:
            Dis[a]=Dis[x]+1
            l.append(a)
            PP[a]=x
for t in range(1,n):
    print(PP[t]+1)

Solution (Python):
import sys
from collections import deque

n, m = map(int, input().split())
link = [ [] for i in range(n)]
for i in sys.stdin.readlines():
    a, b = map(int, i.split())
    link[a-1].append(b-1)
    link[b-1].append(a-1)

d = deque([0])
ans = [-1]*n
ans[0] = 0
while d:
    p = d.pop()
    for v in link[p]:
        if ans[v] == -1:
            ans[v] = p+1
            d.appendleft(v)

if -1 in ans:
    print("No")
else:
    print("Yes")
    print(*ans[1:], sep="\n")

Solution (Python):
from collections import Counter 
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq
import numpy as np
import sys
sys.setrecursionlimit(10**6)




ceil=lambda x,y: (x+y-1)//y
input_list = lambda : list(map(int,input().split()))




def bfs(s,n,node,c):
    
    side_num_arr=list(map(lambda x :len(x),node))
    l=max(side_num_arr)
    
    queue=[-1 for _ in range(n)]
    now=0
    queue[now]=s
    ans=[-1 for _ in range(n)]
    ans[0]=[0]
    last_ind=1
    while last_ind<n:
        
        if now>=n:
            return []
        c[queue[now]]=True
        if side_num_arr[queue[now]]<=0:
            now+=1
            continue
        for i in node[queue[now]]:
            edge=i[0]
            
            ind=i[1]
            if side_num_arr[edge]<=0 or c[edge]==True:
                continue
            side_num_arr[edge]-=1
            side_num_arr[queue[now]]-=1
            queue[last_ind]=edge
            last_ind+=1
            c[edge]=True
            
            ans[edge]=queue[now]
        now+=1
    return ans



n,m=input_list()
node=[[] for _ in range(n)]
for i in range(m):
    u,v=input_list()
    u-=1
    v-=1
    node[u].append([v,i])
    node[v].append([u,i])


c=[-1 for _ in range(n)]
c[0]=0
ans=bfs(0,n,node,c)
if len(ans)==0:
    print("No")
    exit()

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

Solution (Python):
from collections import deque

N, M = map(int, input().split())
A, B = [], []
graph = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

visited = [False for i in range(N+1)]
signs   = [-1 for i in range(N+1)]

D = deque([1])
while D:
    n = D.popleft()
    for node in graph[n]:
        if visited[node] is True:
            continue
        else:
            visited[node] = True
            signs[node] = n
            D.append(node)
print("Yes")
for sign in signs[2:]:
    print(sign)

Solution (Python):
from collections import deque

n,m = map(int,input().split())
graph = [[] for i in range(n)]
mark = [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
        mark[i] = v
        d.append(i)



print('Yes')

for i in mark[1:]:
    print(i+1)

Solution (Python):
from collections import deque

N,M = map(int, input().split())
ways = [[] for _ in range(N+1)]

for i in range(M):
    a,b = map(lambda x: int(x), input().split())
    ways[a].append(b)
    ways[b].append(a)

dq = deque([1])
sign = [None] * (N+1)
sign[0] = 0
sign[1] = 0

while(dq):
    now = dq.popleft()
    for w in ways[now]:
        if not sign[w]:
            sign[w] = now
            dq.append(w)

if None in sign:
    print("No")
else:
    print("Yes")
    for i in range(2, N+1):
        print(sign[i])

Solution (Python):
import collections

N, M = map(int, input().split())

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

queue = collections.deque([0])
visited = [0]*N
visited[0] = 1
while queue:
    x = queue.popleft()
    for i in G[x]:
        if visited[i] == 0:
            visited[i] = x + 1
            queue.append(i)

print("Yes")
print(*visited[1:], sep="\n")

Solution (Python):
import sys
from collections import deque

n, m = map(int, input().split())
link = [ [] for i in range(n)]
for i in sys.stdin.readlines():
    a, b = map(int, i.split())
    link[a-1].append(b-1)
    link[b-1].append(a-1)

d = deque([0])
ans = [-1]*n
ans[0] = 0
while d:
    p = d.popleft()
    for v in link[p]:
        if ans[v] == -1:
            ans[v] = p+1
            d.append(v)

if -1 in ans:
    print("No")
else:
    print("Yes")
    print(*ans[1:], sep="\n")

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


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


def INT(): return int(input())


def MAP(): return map(int, input().split())


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, M = MAP()
    AB = [LIST() for _ in range(M)]

    conn = [[] for _ in range(N)]
    for x, y in AB:
        conn[y-1].append(x-1)
        conn[x-1].append(y-1)

    

    queue = deque([0])
    ans = [0] * N
    while queue:
        ind = queue.popleft()
        
        for x in conn[ind]:
            if ans[x] < 1:
                ans[x] = ind + 1
                queue.append(x)

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


if __name__ == '__main__':
    main()

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

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

ima = [0]
tugi = []

while len(ima) > 0:
    for i in ima:
        for j in arr[i]:
            if flg[j] == -1:
                tugi.append(j)
                flg[j] = i+1

    ima = tugi
    tugi = []
print("Yes")
for i in flg[1:]:
    print(i)

Solution (Python):
from collections import deque
def main():
    N,M = map(int,input().split())
    G = [deque([]) 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)

    stack = deque([0])
    sign = [0] * N
    while stack:
        v = stack.popleft()
        for w in G[v]:
            if sign[w] == 0:
                sign[w] = v+1
                stack.append(w)   

    sign = sign[1:]
    if all(s > 0 for s in sign):
        print('Yes')
        for s in sign:
            print(s)
    else:
        print('No')
        
main()

Solution (Python):
from collections import deque, defaultdict
N, M = map(int, input().split())
D = defaultdict(list)
for i in range(M):
    s, t = map(int, input().split())
    D[s].append(t)
    D[t].append(s)
    
q = deque()
q.append(1)
d = defaultdict(lambda: float("inf"))
d[1] = 0

while q:
  u = q.popleft()
  
  for v in D[u]:
    if d[v] == float("inf"):
      d[v] = u
      q.append(v)
      
if len(d) == N:
  print("Yes")
else:
  print("No")
  exit()
  
for i in range(2, N+1):
  print(d[i])

Solution (Python):
from collections import deque

def main():
  n, m = map(int, input().split())
  ab = (map(int, input().split()) for i in range(m))
  
  ggg = [[] for i in range(n+1)]
  ff = [0] * (n+1)
  
  for a, b in ab:
    ggg[a].append(b)
    ggg[b].append(a)
    
  qq = deque()
  qq.append(1)
  ff[1] = 1
  
  while qq:
    a = qq.popleft()
    for b in ggg[a]:
      if not ff[b]:
        ff[b] = a
        qq.append(b)
  
  rr = ff[2:]
  y = all(rr)
  
  print('Yes' if y else 'No')
  if y:
    for r in rr:
      print(r)

if __name__ == '__main__':
  main()

Solution (Python):
from collections import defaultdict

class que(object):
    def __init__(self):
        self.item = []

    def push(self,x):
        self.item.append(x)
    
    def front(self):
        return self.item[0]
    
    def pop(self):
        self.item.pop(0)
    
    def empty(self):
        return len(self.item) == 0

n,m = map(int , input().split())
path = defaultdict(list)

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

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

q = que()

q.push(0)

ans[0] = 0

while not q.empty():
    now = q.front()
    q.pop()


    for tmp in path[now]:
        
        if tmp == now:
            continue
        
        if ans[tmp] != -1:
            continue
            
        else:
            ans[tmp] = now
            q.push(tmp)
    



for tmp in ans:
    if tmp == -1:
        print("No")
        exit()

print("Yes")

for tmp in ans[1:]:
    
    print(tmp+1)

Solution (Python):
from collections import deque
n, m = map(int, input().split())
c = [[] for _ in range(n)]  
ans = [-1]*n
ans[0] = 0
for _ in range(m):
    a,b = map(int, input().split())
    c[a-1].append(b-1)
    c[b-1].append(a-1)

d = deque([0])  

while d:
    cur = d.pop()
    for x in c[cur]:
        if ans[x]<0:
            ans[x] = cur
            d.appendleft(x)  
            
print('Yes')
for ans in ans[1:]:
    print(ans+1)

Solution (Python):
from collections import deque

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

ans = [None] * n
ans[0] = -1
visited = set()
vs = deque([0])
while vs:
    v = vs.popleft()
    for other_side in e[v]:
        if other_side in visited:
            continue
        visited.add(other_side)
        vs.append(other_side)
        if ans[other_side] is None:
            ans[other_side] = v

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

Solution (Python):
import queue

q = [1]
N, M = map(int, input().split())

ans = [-1 for i in range(N+1)]
path = [[] for i in range(N+1)]

for i in range(M):
  A, B = map(int, input().split())
  path[A].append(B)
  path[B].append(A)
COUNTER = 0
while len(q)>0 :
  now = q.pop(0)
  for p in path[now]:
    if ans[p]==-1:
      ans[p]=now
      q.append(p)
      
      COUNTER+=1
      
print("Yes")
for i in ans[2:]:
  print(i)

Solution (Python):
n, m = [int(v) for v in input().split()]
d = {}
for i in range(m):
    ai, bi = [int(v) for v in input().split()]
    d[ai] = d.get(ai, [])
    d[bi] = d.get(bi, [])
    d[ai].append(bi)
    d[bi].append(ai)

from collections import deque

r = {1: None}
q = deque([1])
while q:
    no = q.popleft()
    for c in d[no]:
        if c not in r:
            r[c] = no
            q.append(c)

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

Solution (Python):
from collections import deque
n,m = map(int,input().split())
g = [[] for i in range(n)]
for i in range(m):
    x,y = map(int,input().split())
    g[x-1].append(y-1)
    g[y-1].append(x-1)
a = [-1]*n
d = deque([0])
while d:
    f = d.popleft()
    for o in g[f]:
        if a[o] == -1:
            a[o] = f
            d.append(o)
print("Yes")
for i in a[1:]:print(i+1)

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

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

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

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



count = 0
q = [0]
ans = [-1]*n

while q:
    l = q.pop(0)
    for i in root[l]:
        if ans[i] == -1:
            ans[i] = l
            q.append(i)
            count += 1
    if count  == n:
        break

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

else:
    print("No")

Solution (Python):
n, m = map(int, input().split())
L=[set() for i in range(n)]
for i in range(m):
    a, b =  map(int, input().split())
    L[a-1] |= {b}
    L[b-1] |= {a}
Y = {range(1, n+1)}
Z = [0]*(n-1) 
S = {1}
X = {1}
while len(S) != n:
    X_next = set()
    for j in X:
        R = L[j-1]-S
        S |= R
        X_next |= R
        for k in R:
            Z[k-2] += j
    X = X_next
    continue
else:
    print("Yes")
    print('\n'.join(map(str, Z)))

Solution (Python):
import queue

q = queue.Queue()
N, M = map(int, input().split())

ans = [-1 for i in range(N+1)]
path = [[] for i in range(N+1)]

for i in range(M):
  A, B = map(int, input().split())
  path[A].append(B)
  path[B].append(A)
q.put(1)
COUNTER = 0
while not q.empty() :
  now = q.get()
  for p in path[now]:
    if ans[p]==-1:
      ans[p]=now
      q.put(p)
      
      COUNTER+=1
      
print("Yes")
for i in ans[2:]:
  print(i)

Solution (Python):
import collections
n, m = map(int, input().split())

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

que = collections.deque([(0, -1)])
visited = [None] * n
while que:
    p, parent = que.popleft()
    if visited[p] is not None:
        continue
    visited[p] = parent
    for to in graph[p]:
        que.append((to, p))

print("Yes")
for v in visited[1:]:
    print(v + 1)

Solution (Python):

from collections import deque

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

    sign_lst = [-1 for _ in range(n+1)]
    sign_lst[1] = 0
    check_lst = [False for _ in range(n+1)]
    check_lst[1] = True

    queue = deque([1])
    while queue:
        room = queue.popleft()
        for way in rw_lst[room]:
            if not check_lst[way]:
                check_lst[way] = True
                sign_lst[way] = room
                queue.append(way)

    check = True
    for i in range(1, n+1):
        check = check and check_lst[i]

    if check:
        print("Yes")
        for i in range(2, n+1):
            print(sign_lst[i])
    else:
        print("No")


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

N, M = map(int, input().split())

room = [[] for i in range(N)]
temp = [0 for i in range(N)]
for i in range(M):
    A, B = map(int, input().split())
    room[B - 1].append(A - 1)
    room[A - 1].append(B - 1)

way = [0 for i in range(N)]
way[0] = 1
que = deque([0])

while True:
    if len(que) == 0:
        break
    a = que.popleft()
    for i in room[a]:
        if way[i] == 0:
            way[i] = a + 1
            que.append(i)

if 0 in way:
    print("No")
else:
    print("Yes")
    for i in way[1:]:
        print(i)

Solution (Python):

import sys
sys.setrecursionlimit(500000)

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

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

for i in range(m):
    ta,tb=map(int, input().split())
    if ta!=1:
        a[tb-1].append(ta-1)
    if tb!=1:
        a[ta-1].append(tb-1)

KITENAI=900000
ans=[0]*n
mk=[KITENAI]*n
mk[0]=0
qu=[0]

def setnext(now):
    global mk
    global qu

    for i in a[now]:
        if mk[i]==KITENAI:
            qu.append(i)
            mk[i]=mk[now] + 1
            ans[i]=now+1

while len(qu)>0:
    setnext(qu.pop(0))

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

Solution (Python):
from collections import deque
n,m = map(int,input().split())
g = [[] for i in range(n)]
for i in range(m):
    x,y = map(int,input().split())
    g[x-1].append(y-1)
    g[y-1].append(x-1)
ans = [-1]*n
deq = deque([0])
while deq:
    fr = deq.popleft()
    for go in g[fr]:
        if ans[go] == -1:
            ans[go] = fr
            deq.append(go)
print("Yes")
for i in ans[1:]:print(i+1)

Solution (Python):



import queue 












mod = 998244353
INF = 1<<29
lINF = 1<<35

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

def main():
    n,m = readInt()
    
    graph = [[] for i in range(n)]
    for i in range(m):
        x,y = readInt()
        graph[x-1].append(y-1)
        graph[y-1].append(x-1)
    
    v = [-1 for i in range(n)]
	
    v[0] = [0]
    
    q = queue.Queue()
    q.put(0)
    while not q.empty():
      	
        x = q.get()
        
        for y in graph[x]:
          	
            if v[y]!=(-1):
                continue
            
            q.put(y)
            v[y] = x
    print("Yes")
    for i in range(1,n):
        print(v[i]+1)
    return

if __name__=='__main__':
    main()