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.



Sample input:
4 4
1 2
2 3
3 4
4 2


Sample output:
Yes
1
2
2

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

N, M = map(int, input().split())
A = [0]*M
B = [0]*M
for i in range(M):
    A[i], B[i] = map(int, input().split())
A=np.array(A)
B=np.array(B)

C = np.array([1])
C_old = np.array([0])
D = np.array(list(range(M)))
D_old=np.array([0]*M)
E = np.array([0]*N)
F = np.array([0]*N)
#print("{}".format(D))
for i in range(M/2):
    if not 0 in F[1:N]:
        break
    #print("{}".format(i))
    C_old=[0]
#    print("{}".format(C))
#    print("{}".format(D))
    for j in D:
        #print("A={},B={},j={}".format(A[j],B[j],j))
        if A[j] in C:
            if E[B[j]-1]==0:
                C_old = np.append(C_old, B[j])
                E[B[j]-1]=A[j]
                F[B[j]-1]=1
                D_old[j]=1
             #else:
            #     E[B[j]-1]=min(A[j],E[B[j]-1])
            #print("D={}".format(D))
            #print("{}".format(E))

        elif B[j] in C:
             if E[A[j]-1]==0:
                 C_old = np.append(C_old, A[j])
                 E[A[j]-1]=B[j]
                 F[A[j]-1]=1
                 D_old[j]=1
             #else:
            #     E[A[j]-1]=min(B[j],E[A[j]-1])
             #print("D={}".format(D))
             #print("{}".format(E))

    C = np.append(C, C_old[1:])
    D=[i for i in D if D_old[i]== 0]


if not 0 in F[1:N]:
    print("Yes")
    for i in range(N-1):
        print("{}".format(E[i+1]))

else:
    print("No")

Submission status: Runtime Error

Solution code:
from collections import deque

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

def intinp():
    return int(input())


N, M = mapint_inp()
ABs = [list(mapint_inp()) for _ in range(N)]

adjacency_matrix = [[]*(N+1) for _ in range(N+1)]

for AB in ABs:
    A, B = AB
    # 隣接しているA,Bに関して、
    # A番目のリストに値Bを、B番目のリストに値Aを追加して
    # 隣接を管理する
    adjacency_matrix[A].append(B)
    adjacency_matrix[B].append(A)

result = [0]*(N+1)

q = deque([1])
while q:
    now = q.popleft()
    for i in adjacency_matrix[now]:
        if result[i] == 0:
            result[i] == now
            q.append(i)

print("Yes")
print('\n'.join(str(i) for i in result[2:]))

Submission status: Runtime Error

Solution code:
from collections import deque

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

def intinp():
    return int(input())


N, M = mapint_inp()
ABs = [list(mapint_inp()) for _ in range(N)]

adjacency_matrix = [[0]*(N+1) for _ in range(N+1)]

for AB in ABs:
    A, B = AB
    # 隣接しているA,Bに関して、
    # A番目のリストに値Bを、B番目のリストに値Aを追加して
    # 隣接を管理する
    adjacency_matrix[A].append(B)
    adjacency_matrix[B].append(A)

result = [0]*(N+1)

q = deque([1])
while q:
    now = q.popleft()
    for i in adjacency_matrix[now]:
        if result[i] == 0:
            result[i] == now
            q.append(i)

print("Yes")
print('\n'.join(str(i) for i in result[2:]))

Submission status: Runtime Error

Solution code:
def function():
    N,M = map(int, input().split())
    paths = []
    for _m in range(M+1):
        paths.append([])    
    
    for _m in range(M):
        A,B = map(int, input().split())
        paths[A].append(B)
        paths[B].append(A)
    
    dist = [-1]*(N+1)
    dist[1] = 0
    pre = [-1]*(N+1)
    q = [1] # 1からスタート
    while len(q) != 0:
        i = q.pop()
        for ne in paths[i]:
            if dist[ne] == -1:
                dist[ne] = dist[i]+1
                pre[ne] = i
                q.append(ne)
    #print(dist)
    print('Yes')
    pre.pop()
    pre.pop()
    for d in pre:
        print(d)

if __name__ == '__main__':
    function()

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
  N,M=map(int,input().split())
  r=[[]for i in range(M)]
  d,p=[10**6]*N,[10**6]*N

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

  q=deque([0])
  p[0],d[0]=-1,0

  while len(q)!=0:
    i=q.pop()
    for j in r[i]:
      if d[j]>d[i]+1:
        d[j],p[j]=d[i]+1,i
        q.append(j)

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

main()

Submission status: Runtime Error

Solution code:
from collections import deque

N,M=map(int,input().split())
r=[[]for i in range(M)]
d,p=[10**6]*N,[10**6]*N

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

q=deque([0])
p[0],d[0],c=-1,0,0

while len(q)!=0:
  i=q.pop()
  for j in r[i]:
    if d[j]>d[i]+1:
      d[j],p[j]=d[i]+1,i
      q.append(j)

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

Submission status: Runtime Error

Solution code:
port math
from queue import Queue
 
n, m = [int(i) for i in input().split()]
 
root = [[ for _ in range(n+1)] for _ in range(n+1)]
 
for i in range(m):
    a, b = [int(i) for i in input().split()]
    root[a][b] = 1
    root[b][a] = 1
 
 
check = [0 for _ in range(n+1)]
check[1] = 1
 
q = Queue()
q.put(1)
 
while ( not q.empty() ):
    now = q.get()
    for i in range(1,n+1):
        if(root[now][i] == 1 and check[i] == 0):
            check[i] = now
            q.put(i)
 
flag = 1
for i in range(2, n+1):
    if(check[i] == 0):
        flag = 0
 
if(flag == 1):
    print("Yes")
    for j in range(2, n + 1):
        print(check[j])
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
from math import cos, radians, pi, sqrt

inputs = input()
inputs = inputs.split('\\n')

N = int(inputs[0].split()[0])
M = int(inputs[0].split()[1])

i = 1
dic = {}
for i in range(1, M+1):
#	print("探索%d回目" % i)
	param = inputs[i].split()
	A = param[0]
	B = param[1]

	if A in dic.keys():
		dic[A].append(B)
	else:
		dic[A] = []
		dic[A].append(B)

	if B in dic.keys():
		dic[B].append(A)
	else:
		dic[B] = []
		dic[B].append(A)
	
depth = {'1': 0}
que = dic['1']
count=1

while not que == []:
	next_que = []
	for room in que:
		if room in depth.keys():
			continue

		depth[room] = count
		for next_room in dic[str(room)]:
			if not next_room in next_que:
				next_que.append(next_room)
	count += 1
	que = next_que

dist = {}
for i in range(2, N+1):
	for room, dep in depth.items():
		if depth[str(i)]-1 == dep:
			dist[str(i)] = room
			break

print("yes")
for opt in dist.items():
	print(opt[1])

Submission status: Runtime Error

Solution code:
import sys
from math import cos, radians, pi, sqrt
import numpy as np
import pandas as pd

inputs = input()
inputs = inputs.split('\n')

N = int(inputs[0].split()[0])
M = int(inputs[0].split()[1])

i = 1
dic = {}
for i in range(1, M+1):
#	print("探索%d回目" % i)
	param = inputs[i].split()
	A = param[0]
	B = param[1]

	if A in dic.keys():
		dic[A].append(B)
	else:
		dic[A] = []
		dic[A].append(B)

	if B in dic.keys():
		dic[B].append(A)
	else:
		dic[B] = []
		dic[B].append(A)
	
depth = {'1': 0}
que = dic['1']
count=1

while not que == []:
	next_que = []
	for room in que:
		if room in depth.keys():
			continue

		depth[room] = count
		for next_room in dic[str(room)]:
			if not next_room in next_que:
				next_que.append(next_room)
	count += 1
	que = next_que

dist = {}
for i in range(2, N+1):
	for room, dep in depth.items():
		if depth[str(i)]-1 == dep:
			dist[str(i)] = room
			break

print("yes")
for opt in dist.items():
	print(opt[1])

Submission status: Runtime Error

Solution code:
import sys
from math import cos, radians, pi, sqrt
import numpy as np
import pandas as pd

inputs = input()
inputs = inputs.split('\n')

N = int(inputs[0].split()[0])
M = int(inputs[0].split()[1])

i = 1
dic = {}
for i in range(1, M+1):
#	print("探索%d回目" % i)
	param = inputs[i].split()
	A = param[0]
	B = param[1]

	if A in dic.keys():
		dic[A].append(B)
	else:
		dic[A] = []
		dic[A].append(B)

	if B in dic.keys():
		dic[B].append(A)
	else:
		dic[B] = []
		dic[B].append(A)
	
depth = {'1': 0}
que = dic['1']
count=1

while not que == []:
	next_que = []
	for room in que:
		if room in depth.keys():
			continue

		depth[room] = count
		for next_room in dic[str(room)]:
			if not next_room in next_que:
				next_que.append(next_room)
	count += 1
	que = next_que

dist = {}
for i in range(2, N+1):
	for room, dep in depth.items():
		if depth[str(i)]-1 == dep:
			dist[str(i)] = room
			break

print("yes")
for opt in dist.items():
	print(opt[1])

Submission status: Runtime Error

Solution code:
from sys import stdin
from collections import deque

n,m=map(int,input().split())
ab=[[] for _ in range(n)]
for i in range(m):
    a,b=map(int,readline().split())
    a-=1
    b-=1
    ab[a].append(b)
    ab[b].append(a)
queue=[0]
ans=[-1]*n
ans[0]=0
while len(queue)!=0:
    now_check=queue.pop(0)
    for i in ab[now_check]:
        if ans[i]==-1:
            queue.append(i)
            ans[i]=now_check
print('Yes')
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
ab=[[] for _ in range(m)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    ab[a].append(b)
    ab[b].append(a)
queue=[0]
ans=[-1]*n
ans[0]=0
while queue:
    now_check=queue.pop(0)
    for i in ab[now_check]:
        if ans[i]==-1:
            queue.append(i)
            ans[i]=now_check
print('Yes')
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
AB = []
ans = [-1] * (n) #####

edge = [[] for _ in range(n)] #edge[i] := 頂点iから伸びてる辺の行き先のリスト #####

for i in range(m):
    a,b = map(int,input().split())
    edge[a].append(b)    
    edge[b].append(a)    
    
import collections
que = collections.deque()
que.append(0) #####
    
while que:
    x = que.popleft()
    
    for to in edge[x]:
        a = x
        b = to
        
        if ans[b] == -1: #####
            ans[b] = x + 1 #####
            que.append(b)
            
print("Yes")
print(*ans,sep="\n")

Submission status: Runtime Error

Solution code:
import math
def culc(N,X,Y,L):

  if N in L :
    L.append(N)
    return
  else:
    L.append(N)
    
  for i in range(0,len(X)):
    if X[i]==N:
      if Y[i]==1:
        return True
      else:
        if culc(Y[i],X,Y,L):
          return True
        L.pop(-1)
    if Y[i]==N:
      if X[i]==1:
        return True
      else:
        if culc(X[i],X,Y,L):
          return True
        L.pop(-1)

def main():
  num = list(map(int,input().split()))
  xy = [map(int, input().split()) for _ in range(num[1])]
  x, y = [list(i) for i in zip(*xy)]
  ans_lists = []
  
  flg=0
  
  for i in range(2,num[0]+1):
    for j in range(0,num[1]):
      if x[j]==i:
        lists=[]
        if(culc(i,x,y,lists)):
          ans_lists.append(y[j])
          break
      if y[j]==i:
        lists=[]
        if(culc(i,x,y,lists)):
          ans_lists.append(x[j])
          break
    if j==num[1]-1:
      flg=1
      break
    
  if flg==1:
    print('No')
  else:
    print('Yes')
    for i in range(0,len(ans_lists)):
      print(ans_lists[i])
  
main()

Submission status: Runtime Error

Solution code:
n, m = list(map(int,input().split()))
to = [] * 100000
for i in range(m):
  a0, b0 = list(map(int,input().split()))
  to[a - 1].append(b0)
  to[b - 1].append(a0)
  
exp = [0] * n
exp[0] = 1
q = [1]
pre = [0] * n

while q.empty():
  v = q[len(q) - 1]
  q.pop()
  
  for i in to[v - 1]:
    pre[i - 1] = v
    q.append(i)
    
print('Yes')
for i in range(n):
  if i == 0:
    continue
  ans = pre[i - 1]
  print(ans)

Submission status: Runtime Error

Solution code:
s = input().split()

n = int(s[0])
m = int(s[1])

ld = {}
root = []

for i in range(m):
    t = input().split()
    if int(t[0]) in ld:
       ld[int(t[0])].add(int(t[1]))
    else:
       ld[int(t[0])] = {int(t[1])}
    if int(t[1]) in ld:
        ld[int(t[1])].add(int(t[0]))
    else:
        ld[int(t[1])] = {int(t[0])}
    root.append({int(t[0]),int(t[1])})


if sum(ld.keys()) < int(((n + 1) * n ) / 2):
    print("No")
else:
    goal = [1]
    result = {}

    for i in range(n):
        count = 0
        for j in range(m-len(goal)):
            if goal[i] in root[j-count]:
                result[int(list(root[j-count] - {goal[i]})[0])] = goal[i]
                goal.append(int(list(root[j-count] - {goal[i]})[0]))
                #print(root[j-count])
                del root[j-count]
                count += 1
        if len(goal) >= n:
            break

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

    #print(goal)
    #print(result)

Submission status: Runtime Error

Solution code:
from sys import stdin
from heapq import heappush, heappop

class Dijkstra:
    def __init__(self, N):
        self.N = N # #vertices
        self.E = [[] for _ in range(N)]

    def add_edge(self, init, end, weight, undirected=False):
        self.E[init].append((end, weight))
        if undirected: self.E[end].append((init, weight))
    
    def distance(self, s):
        INF = float('inf')
        E, N = self.E, self.N
        self.dist = dist = [INF] * N # the distance of each vertex from s
        self.prev = prev = [-1] * N # the previous vertex of each vertex on a shortest path from s
        dist[s] = 0
        n_visited = 1 # #(visited vertices)
        heap = []
        heappush(heap, (0, s))
        while heap:
            d, v = heappop(heap)
            if dist[v] < d: continue
            for u, c in E[v]:
                temp = d + c
                if dist[u] > temp:
                    dist[u] = temp; prev[u] = v
                    heappush(heap, (temp, u))
            n_visited += 1
            if n_visited == N: break
        return dist
    
    def shortest_path(self, t):
        P = []
        prev = self.prev
        while True:
            P.append(t)
            t = prev[t]
            if t == -1: break
        return P[::-1]


def get_result(data):
    N, M = data[0]
    A = data[1:]
    solver = Dijkstra(N)
    for val in A:
        solver.add_edge(val[0]-1, val[1]-1, undirected=True)
    _, prev = solver.distance(0)
    print('Yes')
    for p in prev[1:]:
        print(p + 1)
    return None

if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    get_result(data)

Submission status: Runtime Error

Solution code:
import networkx as nx
n,m,*s = map(int, open(0).read().split())
 
g=nx.Graph()
g.add_nodes_from([i for i in range(1,n+1)])
 
for x in s:
    g.add_edge(x[0],x[1])
di=nx.predecessor(g,source=1)
 
if len(di)!=n:
    print("No");exit()
print("Yes")
for x in range(2,n+1):
    print(di[x][0])

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
room = [-1 for _ in range(N)]
room_kazu = [-1 for _ in range(N)]
rooms = [0 for _ in range(N)]
rooms[0] = 1
room[0] = 0
B = []
room_tree = [[]for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    room_tree[a-1].append(b-1)
    room_tree[b-1].append(a-1)
for i in range(N):
    room_tree[i] = list(set(room_tree[i]))
count = 0
def dfs(A,count):
    if room[A[-1]] == -1 or room[A[-1]] > count:
        room[A[-1]] = count
        room_kazu[A[-1]] = A[-2]
    while room_tree[A[-1]]:
        if rooms[room_tree[A[-1]][-1]] == 1:
            return
        temp = room_tree[A[-1]].pop()
        rooms[temp] = 1
        A.append(temp)
        count += 1
        dfs(A,count)
        rooms[A.pop()] = 0
        count -= 1
    return


dfs([0],count)

print("Yes")
for i in range(1,N):
    a = str(room_kazu[i]+1)
    print(a)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
room = [-1 for _ in range(N)]
room_kazu = [-1 for _ in range(N)]
rooms = [0 for _ in range(N)]
rooms[0] = 1
room[0] = 0
B = []
room_tree = [[]for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    room_tree[a-1].append(b-1)
    room_tree[b-1].append(a-1)
for i in range(N):
    room_tree[i] = list(set(room_tree[i]))
count = 0
def dfs(A,count):
    if room[A[-1]] == -1 or room[A[-1]] > count:
        room[A[-1]] = count
        room_kazu[A[-1]] = A[-2]
    while room_tree[A[-1]]:
        if rooms[room_tree[A[-1]][-1]] == 1:
            return
        temp = room_tree[A[-1]].pop()
        rooms[temp] = 1
        A.append(temp)
        count += 1
        dfs(A,count)
        rooms[A.pop()] = 0
        count -= 1
    return


dfs([0],count)

print("Yes")
for i in range(1,N):
    a = room_kazu[i]+1
    print(a)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
room = [-1 for _ in range(N)]
room_kazu = [-1 for _ in range(N)]
rooms = [0 for _ in range(N)]
rooms[0] = 1
room[0] = 0
B = []
room_tree = [[]for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    room_tree[a-1].append(b-1)
    room_tree[b-1].append(a-1)
for i in range(N):
    room_tree[i] = list(set(room_tree[i]))
count = 0
def dfs(A,count):
    if room[A[-1]] == -1 or room[A[-1]] > count:
        room[A[-1]] = count
        room_kazu[A[-1]] = A[-2]
    while room_tree[A[-1]]:
        if rooms[room_tree[A[-1]][-1]] == 1:
            return
        temp = room_tree[A[-1]].pop()
        rooms[temp] = 1
        A.append(temp)
        count += 1
        dfs(A,count)
        rooms[A.pop()] = 0
        count -= 1
    return


dfs([0],count)

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

Submission status: Runtime Error

Solution code:
import sys
import math
from colletions import deque
N, M = map(int, input().split())
G=[[] for i 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)
q=deque()
done = [-1]*N
done[0]=1
q.appendleft(0)
while q:
    now=q.pop()
    for to in G[now]:
        if done[to] != -1:
            continue
        done[to]=now+1
        q.appendleft(to)
print('Yes')
print(*done[1:],sep='\n')

Submission status: Runtime Error

Solution code:
from collections import deque

n, m = map(int, input().split())
l = [[] for i in range(n + 1)]
for i in range(m):
    a, b = map(int, input().split())
    l[a].append(b)
    l[b].append(a)
ans = [0] * (n + 1)
f = [0] * (n + 1)
d = deque()
d.append(1)
print(len(d))
while len(d) > 0:
    print(d)
    k = d.popleft()
    f[k] = 1
    for i in l[k]:
        if f[i] == 1:
            continue
        ans[i] = k
        d.append(i)
        f[i] = 1
for i in range(2, n + 1):
    if ans[i] == 0:
        print("No")
        exit()
print("Yes")
for i in range(2, n + 1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
n , m = map(int , input().split())
g = defaultdict(list)
for i in range(m):
	u , v = map(int , input().split())
	g[u].append(v)
	g[v].append(u)
#print(g)
visited = [False]*(n+1)
a = [10**10]*(n+1)
q = deque()
q.append(1)
while q:
	node = q.popleft()
	visited[node] = True
	for ele in g[node]:
		if not visited[ele]:
			a[ele] = node
			q.append(ele)
	depth+=1
#print(ans)
#print(a)
print("YES")
for i in a[2:]:
	print(i)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
room = [-1 for _ in range(N)]
room_kazu = [-1 for _ in range(N)]
rooms = [0 for _ in range(N)]
rooms[0] = 1
room[0] = 0
B = []
room_tree = [[]for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    room_tree[a-1].append(b-1)
    room_tree[b-1].append(a-1)
for i in range(N):
    room_tree[i] = list(set(room_tree[i]))
count = 0
def dfs(A,count):
    if room[A[-1]] == -1 or room[A[-1]] > count:
        room[A[-1]] = count
        room_kazu[A[-1]] = A[-2]
    while room_tree[A[-1]]:
        if rooms[room_tree[A[-1]][-1]] == 1:
            return
        temp = room_tree[A[-1]].pop()
        rooms[temp] = 1
        A.append(temp)
        count += 1
        dfs(A,count)
        rooms[A.pop()] = 0
        count -= 1
    return


dfs([0],count)
if -1 in room:
    print("No")
else:
    print("Yes")
    for i in range(1,N):
        print(room_kazu[i]+1)

Submission status: Runtime Error

Solution code:
from collections import deque

N, M = map(int, input().split())
AB = [map(int, input().split()) for _ in range(M)]
print(AB)
links = [[] for _ in range(N + 1)]
for a, b in AB:
    links[a].append(b)
    links[b].append(a)
print(AB)
print(links)
result = [-1] * (N + 1)
q = deque([1])
print(q)
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
            print(q)
print('Yes')
print('\n'.join(str(i) for i in result[2:]))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

memo =  [[] for _ in range(M+1)]

for _ in range(M):
    Ai, Bi = map(int, input().split())
    memo[Ai].append(Bi)
    memo[Bi].append(Ai)

# print(memo)

visited = [False] * (N+1)
visited[1] = True
ans = [0]*(N+1)

queue = [1]

while queue:
    room = queue.pop(0)
    for connected_room in memo[room]:
        # print(connected_room)
        if visited[connected_room] == True:
            continue
        ans[connected_room] = room
        visited[connected_room] = True
        queue.append(connected_room)
print("Yes")
for a in ans[2:]:
    print(a)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
room = [-1 for _ in range(N)]
rooms = [0 for _ in range(N)]
rooms[0] = 1
B = []
room_tree = [[]for _ in range(N)]
for i in range(M):
    a,b = map(int,input().split())
    room_tree[a-1].append(b-1)
    room_tree[b-1].append(a-1)
for i in range(N):
    room_tree[i] = list(set(room_tree[i]))
count = 0
def dfs(A,count):
    if room[A[-1]] == -1 or room[A[-1]] > count:
        room[A[-1]] = count
    while room_tree[A[-1]]:
        if rooms[room_tree[A[-1]][-1]] == 1:
            return
        temp = room_tree[A[-1]].pop()
        rooms[temp] = 1
        A.append(temp)
        count += 1
        dfs(A,count)
        rooms[A.pop()] = 0
        count -= 1
    return


dfs([0],count)
if -1 in room:
    print("No")
else:
    print("Yes")
    for i in range(1,N):
        print(room[i])

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(10005)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
import queue
N,M = map(int,input().split())
tree = [[] for _ in range(M+1)]
for i in range(M):
    a,b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

que = queue.Queue()
que.put(1)

result = [-1]*(N+1)
result[1] = 0
while(True):
    if que.empty():
        break
    start = que.get()
    print("start"+str(start))
    print(que)
    for i in tree[start]:
        if result[i] == -1:
            result[i] = start
            que.put(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v+1]
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
import queue
N,M = map(int,input().split())
tree = [[] for _ in range(M+1)]
for i in range(M):
    a,b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

que = queue.Queue()
que.put(1)

result = [-1]*(N+1)
while(True):
    if que.empty():
        break
    start = que.get()
    for i in tree[start]:
        if result[i] == -1:
            result[i] = start
            que.put(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [] * m
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v+1]
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = []
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v+1]
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [0] * n
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v+1]
        pre[i] = v
        q.append(i)

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

Submission status: Runtime Error

Solution code:
import queue
N,M = map(int,input().split())
tree = [[] for _ in range(M+1)]
for i in range(M):
    a,b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

que = queue.Queue()
que.put(1)

result = [-1]*(N+1)
while(True):
    if que.empty():
        break
    start = que.get()
    for i in tree[start]:
        if result[i] == -1:
            result[i] = start
            que.put(i)


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

Submission status: Runtime Error

Solution code:
import queue
N,M = map(int,input().split())
tree = [[] for _ in range(M+1)]
for i in range(M):
    a,b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

que = queue.Queue()
que.put(1)
rank = [-1]*(N+1)
result = [0]*(N+1)
while(True):
    if que.empty():
        break
    start = que.get()
    for i in tree[start]:
        if rank[i] == -1:
            rank[i] = rank[start]+1
            result[i] = start
            que.put(i)


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

Submission status: Runtime Error

Solution code:
import collections

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

s = {i:[] for i in range(m+1)}
for i in range(m):
    a,b = map(int,input().split())
    s[a].append(b)
    s[b].append(a)

bfs = [0]*(n+1)
bfs[1] = s[1][0]
q = collections.deque()
q.append({1:s[1]})

now = 1
while  q:
    tmp = q.popleft()#現在地点からたどり着ける部屋の辞書
    for k,v in tmp.items():
        for room in v:
            if bfs[room] == 0:
                bfs[room] = k
                q.append({room:s[room]})

if bfs.count(0) <= 1:
    print("Yes")
    for i in bfs[2:]:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    
from collections import deque
todo = deque([(1,0)])
while todo:
    x,parent = todo.pop()
    #ans[x] = parent
    for y in graph[x]:
        ans[y] = x
        # if y == parent:
        #     continue
        if ans[y] != -1:
            continue
        todo.append((y,x))
        
            
for i in range(2,N+1):
    if ans[i] == -1:
        print('No')
        sys.exit()
        
print('Yes')
for i in range(2,N+1):
    print(ans[i])