Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6)

def main():
  N,M = map(int,input().split())
  path = [[] for i in range(N)]
  inf = 10 ** 6 * 2
  ans = [[None,inf] for i in range(N - 1)]
  
  for _ in range(M):
    a,b = map(int,input().split())
    a = a - 1
    b = b - 1
    path[a].append(b)
    path[b].append(a)
  
  def dfs(cur,par,con):
    if con == M + 1:
      return
    for chi in path[cur]:
      if chi == par:
        continue
      if con < ans[chi - 1][1]:
        ans[chi - 1][0] = cur
        ans[chi - 1][1] = con + 1
        dfs(chi,cur,con + 1)
  
  dfs(0,-1,0)
  
  if all([i[0] != None for i in ans]):
    print("Yes")
    for i in range(N - 1):
      print(ans[i][0] + 1)
  else:
    print("No")

if __name__=='__main__':
  main()

Fixed submission:
from collections import deque

import sys
input = sys.stdin.readline 

N,M = map(int,input().split())
path = [[] for i in range(N)]
ans = [None for i in range(N)]
visited = [0 for i in range(N)]

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

def bfs(path, root,visited):
  visited[root] = 1
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if visited[chi] == 1:
        continue
      else:
        ans[chi] = cur + 1
        visited[chi] = 1
        queue.append(chi)

  if all([i == 1 for i in visited]):
    return "Yes"
  else:
    return "No"
  
flag = bfs(path,0,visited)

print(flag)

if flag == "Yes":
  for i in range(1,N):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6)

def main():
  N,M = map(int,input().split())
  path = [[] for i in range(N)]
  inf = 10 ** 6 * 2
  
  ans = [[None,inf] for i in range(N - 1)]
  
  for _ in range(M):
    a,b = map(int,input().split())
    a = a - 1
    b = b - 1
    path[a].append(b)
    path[b].append(a)
  
  def dfs(cur,par,con):
    if con == N + 1:
      return
    for chi in path[cur]:
      if chi == par:
        continue
      if con < ans[chi - 1][1]:
        ans[chi - 1][0] = cur
        ans[chi - 1][1] = con + 1
        dfs(chi,cur,con + 1)
  
  dfs(0,-1,0)
  
  if all([i[0] != None for i in ans]):
    print("Yes")
    for i in range(N - 1):
      print(ans[i][0] + 1)
  else:
    print("No")

if __name__=='__main__':
  main()

Fixed submission:
from collections import deque

import sys
input = sys.stdin.readline 

N,M = map(int,input().split())
path = [[] for i in range(N)]
ans = [None for i in range(N)]
visited = [0 for i in range(N)]

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

def bfs(path, root,visited):
  visited[root] = 1
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if visited[chi] == 1:
        continue
      else:
        ans[chi] = cur + 1
        visited[chi] = 1
        queue.append(chi)

  if all([i == 1 for i in visited]):
    return "Yes"
  else:
    return "No"
  
flag = bfs(path,0,visited)

print(flag)

if flag == "Yes":
  for i in range(1,N):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6)

N,M = map(int,input().split())
path = [[] for i in range(N)]
inf = 10 ** 6 * 2

ans = [[None,inf] for i in range(N - 1)]

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

def dfs(cur,par,con):
  if con == N + 1:
    return
  for chi in path[cur]:
    if chi == par:
      continue
    if con < ans[chi - 1][1]:
      ans[chi - 1][0] = cur
      ans[chi - 1][1] = con + 1
      dfs(chi,cur,con + 1)

dfs(0,-1,0)

if all([i[0] != None for i in ans]):
  print("Yes")
  for i in range(N - 1):
    print(ans[i][0] + 1)
else:
  print("No")

Fixed submission:
from collections import deque

import sys
input = sys.stdin.readline 

N,M = map(int,input().split())
path = [[] for i in range(N)]
ans = [None for i in range(N)]
visited = [0 for i in range(N)]

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

def bfs(path, root,visited):
  visited[root] = 1
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if visited[chi] == 1:
        continue
      else:
        ans[chi] = cur + 1
        visited[chi] = 1
        queue.append(chi)

  if all([i == 1 for i in visited]):
    return "Yes"
  else:
    return "No"
  
flag = bfs(path,0,visited)

print(flag)

if flag == "Yes":
  for i in range(1,N):
    print(ans[i])