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):
from collections import deque
n, m = map(int, input().split())
ab = []
for i in range(m):
    a, b = map(int, input().split())
    a-=1; b-=1
    ab.append((a, b))

edge = [[] for _ in range(n)]
for a, b in ab:
    edge[a].append(b)
    edge[b].append(a)

visited = [False]*n
visited[0] = True
dq = deque([0])
ans = [-1]*n

while dq:
    now = dq.popleft()
    for e in edge[now]:
        if visited[e]==False:
            visited[e] = True
            dq.append(e)
            ans[e]=now

f = True
for a in ans[1:]:
    if a == -1: f = False
if not f:
    print('No')
else:
    print('Yes')
    for a in ans[1:]:
        print(a+1)

Solution (Python):
from collections import defaultdict,deque

N, M = map(int, input().split())
d = defaultdict(list)
for i in range(M):
    a, b = map(int, input().split())
    d[a].append(b)
    d[b].append(a)

dist = [None]*N
q = deque()

s = 1  
q.append(s)
dist[s-1] = 0

while len(q)!=0:
    From = q.popleft()
    for To in d[From]:
        if dist[To-1] == None:
            q.append(To)
            dist[To-1] = From


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

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

    def bfs(s):
        G_color = ['WHITE' for _ in range(N)]
        G_distance = [float('inf') for _ in range(N)]
        G_parent = ['NIL' for _ in range(N)]
        G_color[s] = 'GRAY'
        G_distance[s] = 0
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    G_distance[v] = G_distance[u] + 1
                    G_parent[v] = u
                    Q.append(v)
            G_color[u] = 'BLACK'
        return G_parent

    par = bfs(0)
    if all(par[i] != 'NIL' for i in range(1, N)):
        print("Yes")
        for i in range(1, N):
            print(par[i]+1)
    else:
        print("No")


if __name__ == '__main__':
    main()

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


def read():
    N, M = map(int, input().strip().split())
    G = [list() for i in range(N+1)]
    for i in range(M):
        a, b = map(int, input().strip().split())
        G[a].append(b)
        G[b].append(a)
    return N, M, G


def solve(N, M, G, INF = 10**8):
    depth = [-1 for i in range(N+1)]
    landmark = [-1 for i in range(N+1)]
    q = deque()
    q.append(1)
    depth[1] = 0
    while q:
        x = q.popleft()
        d = depth[x]
        for y in G[x]:
            if depth[y] < 0:
                landmark[y] = x
                depth[y] = d + 1
                q.append(y)
    
    if not all([d>=0 for d in depth[1:]]):
        print("No")
    else:
        print("Yes")
        for a in landmark[2:]:
            print(a)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):



from collections import defaultdict
from collections import deque

def main():
    n, m = map(int, input().split())
    routes = defaultdict(list)
    for _ in range(m):
        a, b = map(int, input().split())
        routes[a].append(b)
        routes[b].append(a)

    rec = [0] * (n + 1)
    rec[1] = 1
    pos_queue = deque([1])
    while len(pos_queue) > 0:
        p = pos_queue.popleft()
        for next_p in routes[p]:
            if not rec[next_p]:
                rec[next_p] = p
                pos_queue.append(next_p)

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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
  n,m=map(int,input().split())
  ab=[list(map(int,input().split()))for _ in range(m)]
  g=[[]for _ in range(n)]
  for a,b in ab:
    g[a-1].append(b-1)
    g[b-1].append(a-1)
  from collections import deque
  todo=deque([0])
  seen=[-1]*n
  seen[0]=0
  while todo:
    t=todo.popleft()
    l=g[t]
    for li in l:
      if seen[li]==-1:
        seen[li]=t
        todo.append(li)
  print('Yes')
  for si in seen[1:]:
    print(si+1)
if __name__=='__main__':
  main()

Solution (Python):

from collections import deque

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

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

visited = [-1]*N
visited[0] = 0
mae = [-1]*N
mae[0] = 0

D = deque()
for v in V[0]:
    visited[v] = 1
    mae[v] = 1
    D.append(v)

while D:
    d = D.popleft()
    Vd = V[d]
    
    for v in Vd:
        if visited[v] == -1:
            visited[v] = visited[d]+1
            mae[v] = d+1
            D.append(v)
        

print("Yes")
for i in range(1,N):
    print(mae[i])

Solution (Python):
from collections import deque
N, M  = map(int, input().split())
AB =[[] for i in range(N)]

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


print("Yes")

D = deque([1])
ans = [0]* N
ans[0] = -1

for i in range(N-1):
  C = D.popleft()
  for j in AB[C-1]:
    if ans[j-1] == 0:
      ans[j-1] = C
      D.append(j)

for i in range(1,N):
  print(ans[i])

Solution (Python):
from collections import deque
n,m = map(int,input().split())
ad_ls = [[] for i in range(n+1)]
ls = [0] * (n+1)
done_ls = [0] * (n+1)
for i in range(m):
    a,b = map(int,input().split())
    ad_ls[a].append(b)
    ad_ls[b].append(a)
q = deque()
for node in ad_ls[1]:
    q.append([1,node])
done_ls[1] = 1
while q:
    start, goal = q.popleft()
    if done_ls[goal] != 1:
        ls[goal] = start
        done_ls[goal] = 1
        for node in ad_ls[goal]:
            q.append([goal, node])

res = 1
for node in range(2,n+1):
    if ls[node] == 0:
        res = 0
if res:
    print('Yes')
    for node in range(2,n+1):
        print(ls[node])
else:
    print('No')

Solution (Python):
class Dijkstra:
  R = {}
  import heapq
  pop = heapq.heappop
  push = heapq.heappush
  
  def __init__(s, N):
    s.N = N

  def add(s, a, b, x):
    if a not in s.R: s.R[a] = {b: x}
    else: s.R[a][b] = x
    if b not in s.R: s.R[b] = {a: x}
    else: s.R[b][a] = x

  def exe(s, start):
    D = [-1] * s.N 
    q  = []
    s.push(q, [0, start])
    cnt = 0
    while cnt < s.N:
      if len(q) == 0:
        return None
      d, x = s.pop(q)
      if D[x] != -1: continue
      D[x] = d
      cnt += 1
      for i in s.R[x]:
        if D[i] != -1: continue
        j = s.R[x][i]
        s.push(q, [d + j, i])
    return D


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

D = Dijkstra(N)

for a, b in AB:
  D.add(a - 1, b - 1, 1)

L = D.exe(0)

if L == None:
  print("No")
  exit()

R = {}

for i in range(N):
  if L[i] in R:
    R[L[i]].append(i)
  else:
    R[L[i]] = [i]

Ans = [0] * N
for i in R.keys():
  for j in R[i]:
    for k in D.R[j].keys():
      if L[k] == L[j] + 1:
        Ans[k] = j + 1

print("Yes")
for i in range(1, N):
  print(Ans[i])

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import deque


def main():
    input = stdin.buffer.readline
    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)

    result = []

    d = [[10 ** 12, -1] for _ in range(n)]
    d[0] = [0, 0]

    q = deque()
    q.append(0)

    while q:
        now_i = q.popleft()

        for next_i in g[now_i]:
            if d[next_i][0] > d[now_i][0] + 1:
                d[next_i] = [d[now_i][0] + 1, now_i]
                q.append(next_i)

    for di in d:
        if di[0] == 10 ** 12:
            print('No')
            exit()

    print('Yes')
    for di in d[1:]:
        print(di[1] + 1)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

next_vec = [0]
visited = [1] + [0] * (N-1)
parent = [-1] + [0] * (N-1)
flag = 1


while next_vec:
    current_vec = next_vec[:]
    next_vec = []
    while current_vec:
        u = current_vec.pop()
        visited[u] = 1
        for v in path[u]:
            if visited[v] == 0:
                visited[v] = 1
                parent[v] = u
                next_vec.append(v)

ans_flag = 1
for i in visited:
    if i == 0:
        ans_flag = 0
if ans_flag:
    print('Yes')
    for i in range(1,N):
        print(parent[i]+1)
else:
    print('No')

Solution (Python):


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

n,m,*ab = [int(i) for i in read().split()]

dist = [-1]*n
dist[0] = 0
ans = [-1]*n

g = [[] for _ in range(n)]
M = iter(ab)
for a,b in zip(M,M):
    g[a-1].append(b-1)
    g[b-1].append(a-1)

from collections import deque
q = deque([0])
while q:
    v = q.popleft()
    for c in g[v]:
        if dist[c] == -1:
            dist[c] = dist[v]+1
            ans[c] = v+1
            q.append(c)
    

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

Solution (Python):


import sys
import math
import collections
import itertools
import bisect
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return x if y == 0 else gcd(y, x%y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, M = LI()
AB = LRI(M)

visited = [False] * N

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

ret = [-1] * N
Q = collections.deque([[0,0]])
while Q:
    qs = Q.popleft()
    b = qs[0]
    q = qs[1]

    if visited[q]:
        continue
    else:
        visited[q] = True
    
    ret[q] = b

    for r in root[q]:
        Q.append([q, r])

if -1 in ret:
    print("No")
else:
    print("Yes")
    for i, r in enumerate(ret):
        if i == 0:
            continue

        print(r+1)

Solution (Python):
N,M = map(int,input().split())
A = []
B = []
for m in range(M):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
from collections import defaultdict as ddict
route = ddict(list)
for i in range(M):
    route[A[i]].append(B[i])
    route[B[i]].append(A[i])
for i in range(1,N+1):
    if route[i] == []:
        print("No")
        exit()
ans = [0 for i in range(N+1)]
from collections import deque
Q = deque([1])
while Q:
    q = Q.popleft()
    for r in route[q]:
        if ans[r] == 0:
            ans[r] = q
            Q.append(r)
print("Yes")
print(*ans[2:],sep="\n")

Solution (Python):
from collections import deque

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

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

d = deque()
d.append(0)
ans = [-1] * n
ans[0] = 0
cnt = 0
while d:
    idx = d.popleft()
    for i in route[idx]:
        if ans[i] < 0:
            ans[i] = idx
            d.append(i)
            cnt += 1

if cnt == n - 1:
    print("Yes")
    for i in ans[1:]:
        print(i + 1)
else:
    print("No")

Solution (Python):
cin = open(0).read().strip().split('\n')
n,m = map(int, cin[0].split(' '))
ab = [tuple(map(int, a.split(' '))) for a in cin[1:]]

g = [[] for _ in range(n)]
for abi in ab:
    g[abi[0]-1].append(abi[1]-1)
    g[abi[1]-1].append(abi[0]-1)

for ii in range(n):
    g[ii] = sorted(g[ii])

dist = [-1]*n
dist[0] = 0

import queue
q = queue.Queue()
q.put(0)

while not q.empty():
    idx = q.get()
    for i in g[idx]:
        if dist[i] != -1: continue
        q.put(i)
        dist[i] = idx

aa = min(dist)
if aa == -1:
    print('No')
else:
    print('Yes')
    for a in dist[1:]:
        print(a+1)

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10 ** 9)
INF = 1 << 60


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


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)

dist = [-1] * N
dist[0] = 0
prev = [0] * N
q = deque([0])
while q:
    v = q.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            q.append(nv)
            dist[nv] = dist[v] + 1
            prev[nv] = v
    
print('Yes')
for v in prev[1:]:
    print(v+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)

mark=[-1]*n
mark[0]=0
check=[0]*n
check[0]=1

que=deque()
que.append(0)

while que:
    p=que.popleft()
    for i in g[p]:
        if check[i]:
            continue
        check[i]=1
        mark[i]=p+1
        que.append(i)

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

Solution (Python):


import sys
from collections import defaultdict, deque

sys.setrecursionlimit(10 ** 7)


INF = float("inf")
MOD = 10 ** 9 + 7


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


q = deque()


ans = [-1 for _ in range(N)]
ans[0] = 0
q.append(0)

while q:
    v = q.popleft()
    for u in G[v]:
        if ans[u] != -1:
            continue
        ans[u] = v + 1
        q.append(u)

print('Yes')
for i in range(1,N):
    print(ans[i])

Solution (Python):
N, M = map(int, input().split())
root = [[] for _ in range(N)]
for _ in range(M):
    A, B = map(int, input().split())
    root[A-1].append(B-1)
    root[B-1].append(A-1)
ans = [[-1, -1] for _ in range(N)]

S = [[0, 0, 0]]

while len(S) > 0:
    S_ = []
    for s in S:
        if ans[s[0]][0] == -1 or s[2] < ans[s[0]][0]:
            ans[s[0]][0] = s[2]
            ans[s[0]][1] = s[1]
            for next in root[s[0]]:
                S_.append([next, s[0], s[2]+1])
    S = S_

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

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

ans=[0]*N
con=set()
now=[1]
while now!=[]:
  _now=[]
  for n in now:
    for p in P[n]:
      if p in con:
        continue
      ans[p-1]=n
      _now.append(p)
      con.add(p)
    
  now=_now
print('Yes')
for a in ans[1:]:
  print(a)

Solution (Python):
N, M = map(int, input().split())
from collections import defaultdict
d = defaultdict(list)

for _ in range(M):
  A, B = map(int, input().split())
  d[A].append(B)
  d[B].append(A)



ans = [0] * (N+1)
visited = {1}

current = [1]
for _ in range(N):
  
  nxt = []
  for e in current:
    for e2 in d[e]:
      if not e2 in visited:
        ans[e2] = e
        visited.add(e2)
        nxt.append(e2)
  if nxt:
    current = nxt
  else:
    break

if len(visited) == N:
  print('Yes')
  for i in range(2, 1+N):
    print(ans[i])
else:
  print('No')

Solution (Python):
from collections import deque, defaultdict

def bfs(i, V, E):
    S = deque()
    S.append(i)
    V[i] = 1
    while(len(S) > 0):
        vi = S.popleft()
        for j in E[vi]:
            if V[j] == 0:
                V[j] = vi
                S.append(j)
    return V

n, m = map(int, input().split())
V = [0 for _ in range(n + 1)]
E = [[] for _ in range(n + 1)]
for i in range(m):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)
V = bfs(1, V, E)
print('Yes')
print(*V[2:], sep='\n')

Solution (Python):
from collections import deque
N,M=map(int,input().split())
tr=[[] for _ in range(N)]
for _ in range(M):
    a,b=map(int,input().split())
    tr[a-1].append(b-1)
    tr[b-1].append(a-1)
ans=[-1]*N
q=deque([0])
v=[True]*N
x=[True]*N
while q:
    t=q.popleft()
    if v[t]:
        v[t]=False
        for j in tr[t]:
            if x[j]:
                x[j]=False
                ans[j]=t
                q.append(j)
print('Yes')
for i in range(1,N):
    print(ans[i]+1)

Solution (Python):
n,m=map(int,input().split())
path=[[] for i in range(n+1)]
anser=[0 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)
nowlis=[1]
nowdone=1
anser[1]=1
while nowdone<n:
  now=nowlis.pop(0)
  done=path[now]
  for i in done:
    if anser[i]==0:
      anser[i]=now
      nowlis.append(i)
      nowdone+=1
  
nuki=anser.pop(0)
nuku=anser.pop(0)
print("Yes")
for i in anser:
  print(i)

Solution (Python):
def d_double_dots():
    from collections import deque
    N, M = [int(i) for i in input().split()]
    Edges = [[int(i) - 1 for i in input().split()] for j in range(M)]  

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

    ans = [-1] * N
    is_visit = [False] * N
    queue = deque([0])
    while queue:
        v = queue.pop()
        for u in graph[v]:
            if not is_visit[u]:
                is_visit[u] = True
                ans[u] = v
                queue.appendleft(u)
    ans = ['Yes'] + list(map(lambda x: x + 1, ans[1:]))
    return '\n'.join(map(str, ans))

print(d_double_dots())

Solution (Python):
import threading, queue
N, M = map(int, input().split())

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

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

q = queue.Queue(N)
q.put(1)
ans[1] = 0
while q.empty() == False:
  t = q.get()
  for j in E[t]:
    if ans[j] == -1:
      ans[j] = t
      q.put(j)
  



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

Solution (Python):
from collections import defaultdict
from collections import deque
N,M=list(map(int,input().split()))
root = defaultdict(list)

for _ in range(M):
    A,B=list(map(int,input().split()))    
    root[A].append(B)
    root[B].append(A)

visited = [-1]*(N+1)
queue = deque([1])
visited[1] = 0
while queue:
    x = queue.popleft()
    for nex in root[x]:
        if visited[nex] == -1:
            visited[nex] = x
            queue.append(nex)

for i in range(2,N+1):
    if visited[i]==-1:
        print('No')
        exit()
print('Yes')
for i in range(2,N+1):
    print(visited[i])

Solution (Python):
from collections import deque
n, m = map(int, input().split())
G = [[] for _ in range(n)]
q = deque([])
d = [float("inf")]*n
p = [0]*n
for _ in range(m):
    u, v = map(int, input().split())
    G[u-1].append(v-1)
    G[v-1].append(u-1)
d[0] = 0
q.append(0)
while q:
    v = q.popleft()
    for u in G[v]:
        if d[v] + 1 < d[u]:
            d[u] = d[v] + 1
            p[u] = v + 1
            q.append(u)
print("Yes")
for i in range(1, n):
    print(p[i])

Solution (Python):
from collections import defaultdict, deque

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

for _ in range(M):
    u, v = map(int, input().split())
    edges[u - 1].append((v - 1, 1))
    edges[v - 1].append((u - 1, 1))

checked = [None] * N
checked[0] = 0

que = deque([0])

while que:
    u = que.popleft()
    for v, w in edges[u]:
        if checked[v] is not None:
            continue
        if edges[v] != []:
            que.append(v)
        checked[v] = u

print('Yes')
for i in range(N - 1):
    print(checked[i + 1] + 1)

Solution (Python):
import sys, 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 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 dijkstra(E, start):
    N_d = len(E)
    dist = [INF] * N_d
    dist[start] = 0
    q = [(0, start)]
    while q:
        dist_v, v = heappop(q)
        if dist[v] != dist_v:
            continue
        for u, dist_vu in E[v]:
            dist_u = dist_v + dist_vu
            if dist_u < dist[u]:
                dist[u] = dist_u
                heappush(q, (dist_u, u))
    return dist

N, M = MAP()
L = [LIST() for i in range(M)]
graph = defaultdict(list)

q = deque([0])
for a, b in L:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)



visited = [False] * N
goto = [INF] * N
dist = [INF] * N

dist[0] = 0
while q:
    a = q.popleft()
    visited[a] = True
    for node in graph[a]:
        if dist[node] > dist[a] + 1:
            dist[node] = dist[a] + 1
            
            goto[node] = a
            q.append(node)

if False in visited:
    print("No")
else:
    print("Yes")
    for g in goto[1:]:
        print(g+1)

Solution (Python):
from heapq import heappush, heappop
import sys
input = sys.stdin.buffer.readline

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

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

for i in range(m):
    A, B = map(int, input().split())
    neighbor[A].append(B)
    neighbor[B].append(A)

h = []

heappush(h, (0, 1, 0))

ans = [0] * (n+1)
while h:
    step, cur, prev = heappop(h)

    for i in neighbor[cur]:
        if ans[i] == 0:
            ans[i] = cur
            heappush(h, (step+1, i, cur))

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

Solution (Python):
n, m = map(int, input().split())
ab = [[] for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    ab[a - 1].append(b)
    ab[b - 1].append(a)
flag = [False for i in range(n)]
prev = [float("inf") for i in range(n)]

num = [i for i in ab[0]]
for i in num:
    prev[i - 1] = 1
    flag[i - 1] = True
flag[0] = True
while num:
    test = num.pop(0)
    for i in ab[test - 1]:
        if flag[i - 1] == False:
            prev[i - 1] = test
            flag[i - 1] = True
            num.append(i)
print("Yes")
for i in range(n - 1):
    print(prev[i + 1])

Solution (Python):
n,m=(int(i) for i in input().split())
l=[[] for i in range(n+1)]
for i in range(m):
  a,b=(int(i) for i in input().split())
  l[a].append(b)
  l[b].append(a)
queue = []
visited = [] 
visited = [0 for i in range(n+1)]
ans=[None for i in range(n+1)]
queue.append([1,0])
visited[1]=1
while len(queue)>0:
  now=queue.pop(0)
  for i in l[now[0]]:
    if visited[i]==0:
      visited[i]=1
      queue.append([i,now[0]])
  ans[now[0]]=now[1]
print('Yes')
for i in range(n-1):
  print(ans[i+2])

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

d = [-1]*n
d[0] = 0
from collections import deque
q = deque()
q.append(0)
ans = [0]*n
while q:
    p = q.popleft()
    for i in g[p]:
        if d[i]==-1:
            q.append(i)
            d[i] = d[p]+1
            ans[i] = p+1
if min(ans)>=0:
    print("Yes")
    for i in ans[1:]:
        print(i)
else:
    print("No")

Solution (Python):
from collections import deque


def main():
    N, M = map(int, input().split())
    G = [set() for _ in range(N)]
    for i in range(M):
        A, B = map(lambda x: int(x) - 1, input().split())
        G[A].add(B)
        G[B].add(A)

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

    while q:
        v = q.popleft()
        for n in G[v]:
            if vis[n] != -1:
                continue
            vis[n] = v
            q.append(n)

    print('Yes')
    print("\n".join(map(lambda x: str(x + 1), vis[1:])))


if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
t = []
l = [0]*(n+1)
for i in range(n+1):
    t.append([])
for i in range(m):
    a,b = map(int,input().split())
    t[a].append(b)
    t[b].append(a)
x = [1]
p = 0
while(p < n):
    for i in range(len(t[x[p]])):
        c = t[x[p]][i]
        if(l[c] == 0):
            x.append(c)
            l[c] = x[p]
    p += 1
print("Yes")
for i in range(2,n+1):
    print(l[i])

Solution (Python):



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


import collections
buf = collections.deque()
parents = [-1 for _ in range(N)]

parents[0] = 0
buf.append(0)

while buf:
    src = buf.popleft()
    for dst in graph[src]:
        if parents[dst] == -1:
            parents[dst] = src
            buf.append(dst)

if all([p != -1 for p in parents]):
    print('Yes')
    for p in parents[1:]:
        print(p+1)
else:
    print('No')

Solution (Python):
n, m = map(int, input().split())
l = [[] for _ in range(n)]

ans = [None] * (n)
queue = [0]

ans[0] = 0

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

def bfs(r):
    for i in l[r]:
        if (ans[i] == None):
            queue.append(i)
            ans[i] = r + 1  
    queue.pop(0)

while (len(queue) > 0):
    bfs(queue[0])

if(None not in ans):
    print("Yes")
    for i in ans[1:]:
        print(i)
else:
    print("No")

Solution (Python):
def main():
    import sys
    from collections import deque
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline
    N, M = [int(x) for x in input().strip().split()]
    G = [deque([]) for _ in range(N)]
    for m in range(M):
        a, b = [int(x) for x in input().strip().split()]
        G[a-1].append(b)
        G[b-1].append(a)
    
    vec = [-1] * N
    q = deque([(0, 1)])
    while q:
        cur, np = q.popleft()
        if vec[np-1] == -1:
            vec[np-1] = cur
            for g in G[np-1]:
                q.append((np, g))
    
    if -1 in vec:
        print('No')
    else:
        print('Yes')
        for v in vec[1:]:
            print(v)

if __name__ == '__main__':
    main()

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

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

dist = [-1]*n
dist[0] = 0
prev = [-1]*n

que = [0]
pos = 0
while pos < len(que) :
	v = que[pos]; pos += 1;
	for w in g[v]:
		if dist[w] == -1 :
			dist[w] = dist[v] + 1
			prev[w] = v;
			que.append(w)

if -1 in dist :
	print("No");
else :
	print("Yes");
	for i in range(1,n):
		print(prev[i]+1);

Solution (Python):
import sys
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

def main():
    n, m = map(int, input().split())
    rooms = [set() for _ in range(n)] 
    for _ in range(m):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        rooms[a].add(b)
        rooms[b].add(a)
    checked = set({0})
    
    
    queue = deque([])
    queue.append((0, 0, 0)) 
    ans = [(-1, 10 ** 7)] * n
    while len(queue) > 0:
        
        current, path, parent = queue.popleft()
        
        
        if ans[current][0] == -1 or path <= ans[current][1]:
            ans[current] = (parent, path)
        
        for i in rooms[current]:
            if i not in checked:
                queue.append((i, path + 1, current))
                checked.add(i)
    print('Yes')
    for i in range(1, n):
        print(ans[i][0] + 1)


if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int, input().split())
es=[[] for i in range(n)]

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

from heapq import heappush, heappop

def dijkstra(edges, size, source):
    
    
    
    distance = [float('inf')] * size
    muki=[size]*size
    distance[source] = 0
    visited = [False] * size
    pq = []
    heappush(pq, (0, source))
    while pq:
        dist_v, v = heappop(pq)
        visited[v] = True
        for u, weight in edges[v]:
            if not visited[u]:
                new_dist = dist_v + weight
                if distance[u] > new_dist:
                    muki[u]=v
                    distance[u] = new_dist
                    heappush(pq, (new_dist, u))
    return distance, muki

dis, muki=dijkstra(es,n,0)

if n not in muki[1:]:
    print('Yes')
    for i in range(1,n):
        print(muki[i]+1)
else:
    print('No')