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:
def bfs(v,G,queue,dist):
    dist[0] = 0
    seen[v] = True
    for vertex in G[v]:
        if seen[vertex]:
            continue
        elif vertex in queue:
            continue
        queue.append(vertex)
        dist[vertex] = dist[v] + 1
    for elements in queue:
        queue.remove(elements)
        bfs(elements,G,queue,dist)
N,M = map(int,input().split())
dist = [-1 for _ in range(N)]
seen = [False for _ in range(N)]
G = dict()
for i in range(M):
    G[i] = []   
for _ in range(M):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
queue = []
step = 0
bfs(0,G,queue,dist)
for v in range(1,N): 
    for el in G[v]:
        if dist[v] == dist[el] + 1:
            print(el+1)
            break

Submission status: Runtime Error

Solution code:
(N, M), *miti = [[*map(int, line.split())] for line in open(0)]

# print(miti)

A = [list() for _ in range(N+1)]
for m in miti:
    a, b = m
    A[a].append(b)
    A[b].append(a)

# print(A)

ans = [0] * (N + 1)
dist = [N] * (N + 1)


def check(p, d):
    for t in A[p]:
        if d + 1 < dist[t]:
            dist[t] = d + 1
            ans[t] = p
            check(t, d + 1)


dist[1] = 0
ans[1] = 88888
check(1, 0)

dist = dist[1:]
ans = ans[2:]
print("foo")
exit()
if all(d < N for d in dist):
    print("Yes")
    for a in ans:
        print(a)
else:
    print("No")

Submission status: Runtime Error

Solution code:
(N, M), *miti = [[*map(int, line.split())] for line in open(0)]

# print(miti)

A = [list() for _ in range(N+1)]
for m in miti:
    a, b = m
    A[a].append(b)
    A[b].append(a)

# print(A)

ans = [0] * (N + 1)
dist = [N] * (N + 1)


def check(p, d):
    for t in A[p]:
        if d + 1 < dist[t]:
            dist[t] = d + 1
            ans[t] = p
            check(t, d + 1)


dist[1] = 0
ans[1] = 88888
check(1, 0)

dist = dist[1:]
ans = ans[2:]
if all(d < N for d in dist):
    print("Yes")
    for a in ans:
        print(a)
else:
    print("No")

Submission status: Runtime Error

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


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

print(graph)

dist = [-1]*2 + [0 for i in range (n-1)]
dist[1] = 0
d = deque([1])
print(d)

while d:
    v = d.popleft()
    graph.remove(v)
    for i in graph[v]:
        if dist[i] == 0:
            dist[i] = v
            d.append(i)
            print(i ," " ,dist[i], d)
            

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [0] * n
visited = [False] * n
visited[0] = True

for _ in range(n):
  s, t = map(int, input().split())
  grid[s].append(t)
  grid[t].append(s)

queue = deque([1])

while queue:
  node = queue.popleft()
  for i in grid[node]:
    if visited[i - 1]:
      continue
    visited[i - 1] = True
    ans[i - 1] = node
    queue.append(i)

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

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

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

an=[0]*(n-1)
wk=[0]*n
awk=np.array(wk)
aab=np.array(ab)
tg =[1]
wtg=[]

while not(all(an)):# or len(aab)<=0:
    if len(aab)<=0:
        break
    for i in tg:
        awk=aab[np.any(aab==i,axis=1)]
        for j in range(len(awk)):
            if awk[j][0]==i:
                if an[awk[j][1]-2]==0:
                    an[awk[j][1]-2]=i
                    wtg.append(awk[j][1])
            else:
                if an[awk[j][0]-2]==0:
                    an[awk[j][0]-2]=i
                    wtg.append(awk[j][0])
            aab=aab[~np.any(aab==tg,axis=1)]

    tg=list(wtg)
    wtg=[]
if all(an):
    print('Yes')
    for i in an:
        print(i)
else:
    print('No')

Submission status: Runtime Error

Solution code:
#inport
import sys
import numpy as np
from collections import deque


#input
N,M = [int(x) for x in sys.stdin.readline().split()]
g = [[] for _ in range(M)]

for _ in range(M):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    g[a].append(b)
    g[b].append(a)



quetmp = deque([1])
d = [None] * (N+1)
d[1] = 0
num_crew = 1
while quetmp:
    v = quetmp.popleft()
    for i in g[v]:
        print(i)
        print(d[i])
        if d[i] is None:
            d[i] = v
            quetmp.append(i)
            num_crew = num_crew + 1

if num_crew == N:
    print('Yes')
    for i in range(N-1):
        print(d[i+1])
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
N,M = list(map(int,input().split(" ")))
AB =  [list(map(int,input().split(" "))) for _ in range(M)]
links = [[] for i in range(M+1)]

for a,b in AB:
  links[a].append(b)
  links[b].append(a)

result = [-1] * (N+1)
q = deque([1])
while q:
  i = q.popleft()
  for j in links[i]:
    if result[j] == -1:
      result[j] = i
      q.append(j)
      
print('Yes')
print('\n'.join(str(i) for i in result[2:]))

Submission status: Runtime Error

Solution code:
#inport
import sys
import numpy as np

#input
N,M = map(int, sys.stdin.readline().split())
P = [list(map(int, sys.stdin.readline().split())) for i in range(M)]

#shori
##初期化
P = np.array(P)     
ans = np.zeros((N,2))
tmp_latest = np.zeros(1)
tmp_latest_tmp = np.zeros(1)
tmp_latest[0] = 1
pos_latest = 0
num_latest = 1
ans[0][1] = 1 #部屋1をてがかりありに
flg_pos = 1
flg_fale = 0
num_crew = 1
num_left_room = N-1
left_room = np.zeros(N)

for i in range(N):
    ans[i][0] = i + 1

while num_crew < N:
    if flg_pos == 0:
        flg_fale = 1
        break

    flg_pos = 0
    for i in range(num_latest):
        tmp_room_num = tmp_latest[i]
        tmp_root = P[np.any( P == int(tmp_room_num), axis = 1)]
        root_loop_n = int(np.size(tmp_root)/2)
        for j in range(root_loop_n):
            if tmp_root[j][0]==tmp_latest[i]:
                if(ans[tmp_root[j][1]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][1])
                    ans[tmp_root[j][1]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1
            elif tmp_root[j][1]==tmp_latest[i]:
                if(ans[tmp_root[j][0]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][0])
                    ans[tmp_root[j][0]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1               
        P = np.delete(P, np.where(P == int(tmp_room_num))[0], axis = 0)

    #Nループ終了時にtmp_latestを更新
    tmp_latest = np.insert(tmp_latest, num_latest, tmp_latest_tmp)
    for j in range(num_latest):
        tmp_latest = np.delete(tmp_latest,0)
    num_latest = np.size(tmp_latest)
    tmp_latest = np.delete(tmo_latest,num_latest-1)
    num_latest = np.size(tmp_latest)
    tmp_latest_tmp = np.zeros(1)

    
if flg_fale == 0:
    print('Yes')
    for i in range(N-1):
        print(int(ans[i+1][1]))
else:
    print('No')

Submission status: Runtime Error

Solution code:
# import heapq
import numpy as np

N, M = [int(x) for x in input()]

e = {i: [] for i in range(N)}
for _ in range(M):
  a, b = [int(x)-1 for x in input()]
  e[a].append(b)
  e[b].append(a)

d = np.ones(N, dtype=int) * -1

q = [0]
while len(q) != 0:
  v = q.pop(0)
  for ne in e[v]:
    if d[ne] == -1:
      d[ne] = v
      q.append(ne)

print("Yes")
# for p in prev[1:]:
#   print(p+1)
print("\n".join([str(p+1) for p in d[1:]]))

Submission status: Runtime Error

Solution code:
import heapq
import numpy as np

N, M = [int(x) for x in input().split(" ")]

e = {i: [] for i in range(N)}
for _ in range(M):
  a, b = [int(x)-1 for x in input().split(" ")]
  e[a] = e[a] + [b]
  e[b] = e[b] + [a]

# roots = [[int(x) for x in input().split(" ")] for _ in range(M)]

d = np.ones(N, dtype=int) * -1
# prev = np.ones(N, dtype=int) * -1
# q = [(d[0], 0)]
# heapq.heapify(q)

# while len(q) != 0:
#   c, u = heapq.heappop(q)
#   for nv in e[u]:
#     alt = d[u] + 1
#     if d[nv] > alt:
#       d[nv] = alt
#       prev[nv] = u
#       heapq.heappush(q, (alt, nv))

q = [d[0]]
while len(q) != 0:
  v = q.pop(0)
  for ne in e[v]:
    if d[ne] == -1:
      d[ne] = v
    q.append(ne)

print("Yes")
# for p in prev[1:]:
#   print(p+1)
print("\n".join([str(p+1) for p in d[1:]]))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
for i in range(m):
    ab.append(list(map(int, input().split())))

d = [0 for i in range(n)]
d[0] = -1
used = []
k = 0

while True:
    for i in range(n):
        if (d[i] != 0) & (((i + 1) in used) == False):
            k = i + 1
            break
    if k in used:
        break
    l = []
    for j in range(len(ab)):
        if ab[j][0] == k:
            if d[ab[j][1] - 1] == 0:
                d[ab[j][1] - 1] = k
            l.append(ab[j])
        elif ab[j][1] == k:
            if d[ab[j][0] - 1] == 0:
                d[ab[j][0] - 1] = k
            l.append(ab[j])
    for j in range(len(l)):
        ab.remove(l[j])
    used.append(k)

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

Submission status: Runtime Error

Solution code:
# 入力の処理
N, M = map(int, input().split())
A = []
B = []
Guide = []
for i in range(N):
    Guide = Guide + [0]
for i in range(M):
    Ai, Bi = map(int, input().split())
    A = A + [Ai]
    B = B + [Bi]

# 道しるべの付与されていない部屋を順番に検討する
# 通路にj番の部屋に通じているものがあれば、その通路が通じている部屋に「j」の道しるべを配置する
nextcheck = [1]
while(len(nextcheck) > 0):
    check = [] + nextcheck # ここなんで[]が必要なのか。
    for k in range(len(check)):
        j = check[k]
        dellist = []
        for i in range(len(A)):
            if A[i] == j:
                # その通路はいずれにせよ後々検討する必要はないので、削除リストに追加。
                dellist = dellist + [i]
                # 最初に1番の部屋に戻ることができる道を見つけたときか、1番の部屋により早く戻ることができる道を見つけたときに、
                if Guide[B[i] - 1] == 0 or MIN[B[i] - 1] < MIN[j - 1] - 1:
                    Guide[B[i] - 1] = j
                    MIN[B[i] - 1] = MIN[j - 1] + 1
                    nextcheck = list(set(nextcheck + [B[i]]))
            elif B[i] == j:
                dellist = dellist + [i]
                if Guide[A[i] - 1] == 0 or MIN[A[i] - 1] < MIN[j - 1] - 1:
                    Guide[A[i] - 1] = j
                    MIN[A[i] - 1] = MIN[j - 1] + 1
                    nextcheck = list(set(nextcheck + [A[i]]))

        for i in range(len(dellist)):
            del A[dellist[i] - i]
            del B[dellist[i] - i]

        nextcheck.remove(j)

# whileを抜けたら、Guide[0]以外にはすべてに何か知らの0でない番号が入っているはず。
# 0が残っていなかったら、no
del Guide[0]

if Guide.count(0) == 0:
    print('Yes')
    for i in range(len(Guide)):
        print(Guide[i])
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np
max_dis = 100001
n, m = map(int, input().split())
road = np.zeros([n, n])
for i in range(m):
    a, b = map(int, input().split())
    road[a-1][b-1] = 1
    road[b-1][a-1] = 1

flag = np.zeros([n])
flag[0] = 1
distance = np.zeros([n])
forward = np.zeros([n])
for i in range(1, n):
    if road[0][i]:
        distance[i] = 1
        forward[i] = 0
    else:
        distance[i] = max_dis+1

while 0 in flag:
    mind = max_dis
    node = 0
    for i in range(1, n):
        if not flag[i] and distance[i] < mind:
            node = i
            mind = distance[i]
    flag[node] = 1

    for j in range(1, n):
        if road[j][node] and distance[j] > mind+1:
            distance[j] = mind + 1
            forward[j] = node
# print(distance)

if 0 in flag:
    print('No')
else:
    print("Yes")
    for i in range(1, n):
        print('%d' % (forward[i]+1))

Submission status: Runtime Error

Solution code:
import numpy as np
max_dis = 100001
n, m = map(int, input().split())
road = np.zeros([n, n])
for i in range(m):
    a, b = map(int, input().split())
    road[a-1][b-1] = 1
    road[b-1][a-1] = 1

flag = np.zeros([n])
flag[0] = 1
distance = np.zeros([n])
forward = np.zeros([n])
for i in range(1, n):
    if road[0][i]:
        distance[i] = 1
        forward[i] = 0
    else:
        distance[i] = max_dis+1

while 0 in flag:
    mind = max_dis
    node = 0
    for i in range(1, n):
        if not flag[i] and distance[i] < mind:
            node = i
            mind = distance[i]
    flag[node] = 1

    for j in range(1, n):
        if road[j][node] and distance[j] > mind+1:
            distance[j] = mind + 1
            forward[j] = node
    # print(distance)

i = 1
if 0 in flag:
    print('No')
else:
    print("Yes")
    for i in range(1, n):
        print('%d' % (forward[i]+1))

Submission status: Runtime Error

Solution code:
import numpy as np
max_dis = 100001
n, m = map(int, input().split())
road = np.zeros([n, n])
for i in range(m):
    a, b = map(int, input().split())
    road[a-1][b-1] = 1
    road[b-1][a-1] = 1

flag = np.zeros([n])
flag[0] = 1
distance = np.zeros([n])
forward = np.zeros([n])
for i in range(n):
    if road[0][i]:
        distance[i] = 1
        forward[i] = 1
    else:
        distance[i] = max_dis+1

while 0 in flag:
    mind = max_dis
    node = 0
    for i in range(n):
        if not flag[i] and distance[i] < mind:
            node = i
            mind = distance[i]
    flag[node] = 1
    j = 1
    for j in range(n):
        if road[j][node] and distance[j] > mind+1:
            distance[j] = mind + 1
            forward[j] = node+1
    # print(distance)

i = 1
if 0 in flag:
    print('No')
else:
    print("Yes")
    for i in range(n):
        if i != 0:
            print('%d' % forward[i])

Submission status: Runtime Error

Solution code:
import numpy as np
dmin = 100001
n, m = map(int, input().split())
road = np.zeros([n, n])
for i in range(m):
    a, b = map(int, input().split())
    road[a-1][b-1] = 1
    road[b-1][a-1] = 1
#print(road)
flag = np.zeros([n])
flag[0] = 1
distance = np.zeros([n])
forward = np.zeros([n])
for i in range(n):
    if road[0][i]:
        distance[i] = 1
        forward[i] = 1
    else:
        distance[i] = dmin+1
#print('flag: ', flag)
#print('distance: ', distance)
counter = 0
while (0 in flag) and counter < n+1:
    mind = dmin
    node = 0
    i = 1
    for i in range(n):
        if not flag[i] and distance[i] < mind:
            node = i
            mind = distance[i]
    #print('find ', node)
    flag[node] = 1
    j = 1
    for j in range(n):
        if road[j][node] and distance[j] > mind+1 :
            distance[j] = mind + 1
            forward[j] = node+1
    counter += 1
    #print(distance)
#print("finally ", forward[1:])
i = 1
if 0 in flag:
    print('No')
else:
    print("Yes")
    for i in range(n):
        if i != 0:
            print('%d' % forward[i])

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])


next=[1]
count = 1
while len(next)!=0:
    imaheya = next.pop(0)
    for i in range(len(ikeruheya[imaheya-1])):
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya             
            next.append(ikeruheya[imaheya-1][j])
            #ikeruheya[[imaheya-1][j]-1].remove(imaheya)
        

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

Submission status: Runtime Error

Solution code:
from collections import deque
N,M=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(M)]
l=[0]*N
que=deque([])
que.append(1)
def bfs():
  while que:
     if 0 not in l:
         exit()
     	p=que.popleft
      for i in range(M):
          if ab[i][0]==p:
            que.append(ab[i][1])
            l[ab[i][1]-1]=p
          if ab[i][1]==p:
            que.append(ab[i][0])
            l[ab[i][0]-1]=p
  print("Yes")
  print(*l) 
bfs()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n
check[0]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])
print(ikeruheya)
    
next=[1]
count = 1
while len(next) != 0 and count <= n:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        print(ikeruheya[imaheya-1])
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

print(check)
print(count)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range m]
ikeruheya = [[] for i in range n]
ans = []
check = [0]*n

for i in range(m):
    ikeruheya[a[i][0]].append[a[i][1]]
    ikeruheya[a[i][1]].append[a[i][0]]
    
next=[0]

while len(next) != 0:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya])):
        if check(ikeruheya[imaheya][j])==0:
            ans[ikeruheya[imaheya][j]]= j + 1
            check[ikeruheya[imaheya][j]-1] = 1
            next.append(ikeruheya[imaheya][j]-1)
for i in range(2, n+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())

aisles = [[] for _ in range(n)]
michi = [0] * n
num = [float('INF')] * n
loads = []
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if b < a:
        a, b = b, a
    if a == 0:
        loads.append(b)
    aisles[a].append(b)
    aisles[b].append(a)

if len(loads) == 0:
    print("No")
    exit()


def ap(ll, nm, nu):
    for l in ll:
        if num[l] > nu:
            michi[l] = nm
            num[l] = nu
            ap(aisles[l], l, nu + 1)


ap(loads, 0, 1)

print("Yes")
for mi in michi[1:]:
    print(mi + 1)

Submission status: Runtime Error

Solution code:
import queue
N,M=map(int,input().split())
adj=[[] for s in range(M+1)]
parent=[-1]*(N+1)
for i in range(M):
    A,B=map(int,input().split())
    adj[A].append(B)
    adj[B].append(A)
q=queue.Queue()
q.put((1,0))
while(not q.empty()):
    x,p=q.get()
    if parent[x]!=-1:
        continue
    parent[x]=p
    for next in adj[x]:
        if parent[next]==-1:
            q.put((next,x))
print('Yes')
for k in parent[2:]:
    print(k)

Submission status: Runtime Error

Solution code:
import queue
N,M=map(int,input().split())
adj=[[] for _ in range(M+1)]
parent=[-1]*(N+1)
for i in range(M):
    A,B=map(int,input().split())
    adj[A].append(B)
    adj[B].append(A)
q=queue.Queue()
q.put((1,0))
while(not q.empty()):
    x,p=q.get()
    if parent[x]!=-1:
        continue
    parent[x]=p
    for next in adj[x]:
        if parent[next]==-1:
            q.put((next,x))
print('Yes')
for k in parent[2:]:
    print(k)

Submission status: Runtime Error

Solution code:
import queue
N,M=map(int,input().split())
adj=[[] for _ in range(M+1)]
for i in range(M):
    A,B=map(int,input().split())
    adj[A].append(B)
    adj[B].append(A)
parent=[-1]*(N+1)
q=queue.Queue()
q.put((1,0))
while(not q.empty()):
    x,p=q.get()
    if parent[x]!=-1:
        continue
    parent[x]=p
    for next in adj[x]:
        if parent[next]==-1:
            q.put((next,x))
print('Yes')
for k in parent[2:]:
    print(k)

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
S = []
Room = [MAX for i in range(N)]#2 - N
Room[0] = 0
ACCESS = [0 for i in range(N-1)]
for _ in range(M):
    a,b = list(map(int,input().split()))
    S.append([a,b])
    S.append([b,a])
for i in S:
    if i[0] == 1:
        Room[i[1]-1] = 1
        ACCESS[i[1]-2] = 1
        S.remove(i)
depth = 1
while max(Room) > MAX/2:
    for i in range(len(Room)):
        if Room[i] ==depth:
            k = 0
            for _ in range(len(S)):
                j = S[k]
                if (j[0] == i+1)*(Room[j[1]-1]>MAX/2):
                    Room[j[1]-1] = depth+1
                    ACCESS[j[1]-2] = i+1
                    S.remove(j)
                    S.remove(j[::-1])
                    k -= 1
                k += 1
    depth += 1
print('Yes')
for i in ACCESS:
    print(i)

Submission status: Runtime Error

Solution code:
# from collections import deque

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

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

queue = deque([0])
d = [None] * N

d[0] = 0
while queue:
     v = queue.popleft()
     for i in g[v]:
         if d[i] is None:
             d[i] = v
             queue.append(i)

flag = "Yes"
for i in range(N):
    if d[i] is None:
        flag = "No"

print(flag)

if flag == "Yes":
    for i in range(N-1):
        print(d[i+1]+1)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

queue = deque([0])
d = [None] * N

d[0] = 0
while queue:
     v = queue.popleft()
     for i in g[v]:
         if d[i] is None:
             d[i] = v
             queue.append(i)

flag = "Yes"
for i in range(N):
    if d[i] is None:
        flag = "No"

print(flag)

if flag == "Yes":
    for i in range(N-1):
        print(d[i+1]+1)

Submission status: Runtime Error

Solution code:
import numpy as np

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

mat = np.zeros((N,N))
res = np.zeros((N))
distance = np.full((N), N+1)
distance[0] = 0

for _ in range(M):
    i, j = list(map(int, input().split()))
    mat[i-1][j-1] = 1
    mat[j-1][i-1] = 1

print(mat)

for i in range(N):
    for j in range(N):
        if mat[i][j] == 1:
            print(i,j)
            new_d = distance[i] + 1
            if distance[j] > new_d:
                distance[j] = new_d
                res[j] = i+1
            
print(res[1:])

Submission status: Runtime Error

Solution code:
import numpy as np

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

mat = np.zeros((N,N))
res = np.zeros((N))
distance = np.full((N), N+1)
distance[0] = 0

for _ in range(M):
    i, j = list(map(int, input().split()))
    mat[i-1][j-1] = 1
    mat[j-1][i-1] = 1


for i in range(N):
    for j in range(N):
        if mat[i][j] == 1:
            new_d = distance[i] + 1
            if distance[j] > new_d:
                distance[j] = new_d
                res[j] = i+1
            
print(res[1:])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
ansList = [0] * (N-1)
roadList = [[0] * N for i in range(N)]
for i in range(M):
  road = list(map(int, input().split()))
  roadList[road[0]-1][road[1]-1] = 1
  roadList[road[1]-1][road[0]-1] = 1

def check(idx):
  for i in range(1, len(roadList)):
    if (ansList[i-1] < idx+1 and ansList[i-1] !=1) and roadList[idx][i] == 1:
      ansList[i-1] = idx+1
      check(i)

if roadList[0] != []:
  for i in range(1, len(roadList)):
    if roadList[0][i] == 1:
      ansList[i-1] = 1
      check(i)
    
if 0 in ansList:
  print("No")
else:
  print("Yes")
  for i in range(N-1):
    print(ansList[i])

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
nod=[[]]
check=[[0,0]]
for i in range(n-1):
  nod.append([])
  check.append([10**10,0])
for i in range(m):
  a,b=map(int,input().split())
  nod[a-1].append(b)
  nod[b-1].append(a)


def dfs(k):
  count=check[k-1][0]+1
  uselist=nod[k-1]
  for x in uselist:
    if count<check[x-1][0]:
      check[x-1][0]=count
      check[x-1][1]=k
      dfs(x)
    else:
      continue
  return

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
nod=[[]]
check=[[0,0]]
for i in range(n-1):
  nod.append([])
  check.append([10**10,0])
for i in range(m):
  a,b=map(int,input().split())
  nod[a-1].append(b)
  nod[b-1].append(a)


def dfs(k):
  count=check[k-1][0]+1
  uselist=nod[k-1]
  for x in uselist:
    if count<check[x-1][0]:
      check[x-1][0]=count
      check[x-1][1]=k
      dfs(x)
    else:
      continue

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
nod=[[]]
check=[[0,0]]
for i in range(n-1):
  nod.append([])
  check.append([10**10,0])
for i in range(m):
  a,b=map(int,input().split())
  nod[a-1].append(b)
  nod[b-1].append(a)


def dfs(k):
  count=check[k-1][0]+1
  uselist=nod[k-1]
  for x in uselist:
    if count<check[x-1][0]:
      check[x-1][0]=count
      check[x-1][1]=k
      dfs(x)
    else:
      continue

dfs(1)
for i in range(n-1):
  print(check[i+1][1])

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
ab = [[] for i in range(n+1)]

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

ans = [-1]*(n+1)
que = deque([1])

while que:
  q = que.popleft()
  for i in ab[q]:
    if ans[i] == -1:
      que.append(i)
      ans[i] = q

print("Yes")

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
ab = [[] for i in range(n+1)]

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

ans = [-1]*(n+1)
que = deque([1])

while que:
  q = que.popleft()
  for i in ab[q]:
    if ans[i] == -1:
      que.append(i)
      ans[i] = q

print("Yes")

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

Submission status: Runtime Error

Solution code:
import queue

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

graph = [[] for i 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)

dist = [-1 for i in range(N)]
que = queue.Queue()

dist[0] = 0
que.put(0)


while not que.empty():
    v = que.get()
    for nv in graph[v]:
        if dist[nv] != -1:
            continue

        dist[nv] = dist[v] + 1
        que.push(nv)


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

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
A,B,C = [0 for i in range(M)],[0 for i in range(M)],[0 for i in range(N)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    if A[i]==1:
        C[B[i]]=1
    if B[i]==1:
        C[A[i]]=1

for i in range(N):
    for j in range(M):
        a,b=A[j],B[j]
        if C[a]==i+1 & C[b]== 0:
            C[b]=i+2
        if C[b]==i+1 & C[a]== 0:
            C[a]=i+2

print("Yes")
for i in range(N):
    print(C[i])

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
A,B,C = [0 for i in range(M)],[0 for i in range(M)],[0 for i in range*(N)]
for i in range(M):
    A[i],B[i]=map(int,input().split())
    if A[i]==1:
        C[B[i]]=1
    if B[i]==1:
        C[A[i]]=1

for i in range(N):
    for j in range(M):
        a,b=A[j],B[j]
        if C[a]==i+1 & C[b]== 0:
            C[b]=i+2
        if C[b]==i+1 & C[a]== 0:
            C[a]=i+2

print("Yes")
for i in range(N):
    print(C[i])

Submission status: Runtime Error

Solution code:
N,M = map(int,input.split())
A,B,C = [0 for i in range(M)],[0 for i in range(M)],[0 for i in range*(N)]
for i in range(M):
    A[i],B[i]=map(int,input.split())
    if A[i]==1:
        C[B[i]]=1
    if B[i]==1:
        C[A[i]]=1

for i in range(N):
    for j in range(M):
        a,b=A[j],B[j]
        if C[a]==i+1 & C[b]== 0:
            C[b]=i+2
        if C[b]==i+1 & C[a]== 0:
            C[a]=i+2

print("Yes")
for i in range(N):
    print(C[i])

Submission status: Runtime Error

Solution code:
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = map(int, sys.stdin.readline().strip().split())
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

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

Submission status: Runtime Error

Solution code:

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

print("Yes")

Submission status: Runtime Error

Solution code:
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

print("Yes")