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: Wrong Answer

Solution code:
import math, sys
N , M = list(map(int, input().split()))

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

i = 1
j = 1
dis = 0
ans = [[N + 1 , M+2]for _ in range(N)]
cand = [0]
ans [0] = ["yes",0]
NY = [True for i in range(N)]
while True:
    for each in Es[tar]:
        if NY[each]:
            cand.append(each)
        if ans[each][1]>dis+1:
            ans[each] = [tar, dis+1]
    #print(ans)
    if len(cand)==0:
        break
    else:
        tar = cand.pop(0)
        NY[tar]=False
        dis = ans[tar][1]
    #print(tar, cand)
print("yes")
for i in range(1,N):
    print(ans[i][0]+1)

Submission status: Wrong Answer

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


from collections import deque

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

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

            
print("Yes")
for i in range(2,len(d)):
    print(d[i])

Submission status: Wrong Answer

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


from collections import deque

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

while queue:
    v=queue.popleft()
    for i in g[v]:
        if d[i] is None:
            d[i]=d[v]+1
            queue.append(i)
print(d)
            
print("Yes")
for i in range(2,len(d)):
    print(d[i])

Submission status: Wrong Answer

Solution code:

N, M=map(int, input().split())
xy = [map(int, input().split()) for _ in range(M)]
A, B = [list(i) for i in zip(*xy)]
ans=[-1,0]+[-1]*(N-1)

n=-1
while sum(A) !=0 or sum(B) !=0:
    n+=1
    for i in range(M):
        if A[i]==B[i]:
            A[i]=0
            B[i]=0
        elif ans[A[i]] ==n:
            if ans[B[i]]==-1:
                ans[B[i]]=n+1
            A[i]=0
            B[i]=0
        elif ans[B[i]] ==n:
            if ans[A[i]]==-1:
                ans[A[i]]=n+1
            A[i]=0
            B[i]=0    

print("Yes")
for i in range(2,len(ans)):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

graph = [[] for i in range(n+1)]
dist = [-1] * (n+1)
dist[0] = 0
dist[1] = 0

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

d = deque()
d.append(1)

while d:
    v = d.popleft()
    for i in graph[v]:
        if dist[i] != -1:
            continue
        dist[i] = dist[v]+1
        d.append(i)

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
t = [[i+1] for i in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    t[A-1].append(B)
    t[B-1].append(A)
from collections import deque
d = deque()
ans = [-1]*(N-1)
d.append(t[0])
while len(d) > 0:
    z = d.popleft()
    x = z[0]
    for i in range(1,len(z)):
        if ans[z[i]-2] == -1:
            ans[z[i]-2] = x
            d.append(t[z[i]-1])
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
t = [[i+1] for i in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    t[A-1].append(B)
    t[B-1].append(A)
from collections import deque
d = deque()
ans = [-1]*(N-1)
d.append(t[0])
while len(d) > 0:
    z = d.popleft()
    x = z.pop(0)
    for i in z:
        if ans[i-2] == -1:
            ans[i-2] = x
            d.append(t[i-1])
if -1 in ans:
    print("No")
else:
    print("Yes")
    for i in ans:
        print(i)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
t = [[i+1] for i in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    t[A-1].append(B)
    t[B-1].append(A)
from collections import deque
d = deque()
ans = [-1]*(N-1)
d.append(t[0])
while len(d) > 0:
    z = d.popleft()
    x = z.pop(0)
    for i in z:
        if ans[i-2] == -1:
            ans[i-2] = x
            d.append(t[i-1])
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
INF = 1_000_000_000
def main():
    n, m = map(int, input().split(" "))
    to = [[] for _ in range(n)]
    for _ in range(m):
        a, b = map(lambda i:int(i)-1, input().split(" "))
        to[a].append(b)
        to[b].append(a)
    print(to)
    que = deque()
    d = {i:INF for i in range(n)}
    prev = {i:-1 for i in range(n)}
    d[0] = 0
    que.append(0)
    while que:
        v = que.popleft()
        for i in to[v]:
            if d[i] != INF: continue
            d[i] = d[v] + 1
            prev[i] = v
            que.append(i)
    print("Yes")
    for i in range(n):
        if i == 0: continue
        print(prev[i]+1)
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
INF = 1_000_000_000
def main():
    n, m = map(int, input().split(" "))
    to = [[] for _ in range(n)]
    for _ in range(m):
        a, b = map(lambda i:int(i)-1, input().split(" "))
        to[a].append(b)
        to[b].append(a)
    que = deque()
    d = {i:INF for i in range(n)}
    d2 = {i:-1 for i in range(n)}
    d[0] = 0
    que.append(0)
    while que:
        v = que.pop()
        for i in to[v]:
            if d[i] != INF: continue
            d[i] = d[v]+1
            d2[i] = v
            que.append(i)
    print("Yes")
    for i in range(n):
        if i == 0: continue
        print(d2[i]+1)
        


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
INF = 1_000_000_000
def main():
    n, m = map(int, input().split(" "))
    to = [[] for _ in range(n)]
    for _ in range(m):
        a, b = map(lambda i:int(i)-1, input().split(" "))
        to[a].append(b)
        to[b].append(a)
    que = deque()
    d = {i:INF for i in range(n)}
    d2 = {i:-1 for i in range(n)}
    d[0] = 0
    que.append(0)
    while que:
        v = que.pop()
        for i in to[v]:
            if d[i] != INF: continue
            que.append(i)
            d[i] = d[v]+1
            d2[i] = v
            que.append(i)
    print("Yes")
    for i in range(n):
        if i == 0: continue
        print(d2[i]+1)
        


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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)

dist = [-1] * (n+1)
dist[0] = 0
dist[1] = 0

d = deque()
d.append(1)

while d:
 v = d.popleft()
 for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for i in range(m):
    x, y = map(int,input().split())
    to[x-1].append(y-1)
    to[y-1].append(x-1)

INF = float('inf')
dist = [INF]*n
dist[0] = 0

pre = [-1]*n

que = deque([])
que.append(0)

while que:
    v = que.popleft()
    for i in to[v]:
        if dist[i] != INF:continue
        dist[i] = dist[v] + 1
        pre[i] = v
        que.append(i)
print("Yes")
for i in range(1,n):
    print(dist[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
l = deque([list(map(int,input().split())) for i in range(m)])
d = [-1] * n
d[0] = 0
ans = [-1] * n
ans[0] = 0
while l:
    z = l.popleft()
    x = z[0] -1
    y = z[1] -1
    if d[x] == -1 and d[y] == -1:
        l.append([x+1,y+1])
    elif d[x] == -1:
        d[x] = d[y] + 1
        ans[x] = y + 1
    elif d[y] == -1:
        d[y] = d[x] + 1
        ans[y] = x + 1
    else:
        if d[x] > d[y]:
            d[x] = d[y] + 1
            ans[x] = y + 1
        elif d[y] > d[x]:
            d[y] = d[x] + 1
            ans[y] = x + 1
        else:
            pass
if -1 in ans:
    print('No')
else:
    print('Yes')
    for i in ans[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
a = deque()
b = deque()
for i in range(m):
    x,y = map(int,input().split())
    x,y = x-1,y-1
    if x > y:
        x,y = y,x
    a.append(x)
    b.append(y)
d = [-1] * n
d[0] = 0
ans = [-1] * n
ans[0] = 0
while a:
    x = a.popleft()
    y = b.popleft()
    if d[x] == -1 and d[y] == -1:
        a.append(x)
        b.append(y)
    elif d[x] == -1:
        d[x] = d[y] + 1
        ans[x] = y + 1
    elif d[y] == -1:
        d[y] = d[x] + 1
        ans[y] = x + 1
    else:
        if d[x] > d[y]:
            d[x] = d[y] + 1
            ans[x] = y + 1
        elif d[y] > d[x]:
            d[y] = d[x] + 1
            ans[y] = x + 1
        else:
            pass
if -1 in ans:
    print('No')
else:
    print('Yes')
    for i in ans[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
a = deque()
b = deque()
for i in range(m):
    x,y = map(int,input().split())
    x,y = x-1,y-1
    if x > y:
        x,y = y,x
    a.append(x)
    b.append(y)
d = [-1] * n
d[0] = 0
ans = [-1] * n
ans[0] = 0
while a:
    x = a.popleft()
    y = b.popleft()
    if d[x] == -1 and d[y] == -1:
        a.append(x)
        b.append(y)
    elif d[x] == -1:
        d[x] = d[y] + 1
        ans[x] = y + 1
    elif d[y] == -1:
        d[y] = d[x] + 1
        ans[y] = x + 1
    else:
        if d[x] > d[y] + 1:
            d[x] = d[y] + 1
            ans[x] = y + 1
        if d[y] > d[x] + 1:
            d[y] = d[x] + 1
            ans[y] = x + 1
if -1 in ans:
    print('No')
else:
    print('Yes')
    for i in ans[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
l = [list(map(int,input().split())) for i in range(m)]
for i in range(m):
    x,y = l[i][0],l[i][1]
    x,y = x-1,y-1
    if x > y:
        x,y = y,x
    l[i] = [x,y]
l.sort()
a,b = deque(),deque()
for i in range(m):
    a.append(l[i][0])
    b.append(l[i][1])
ans = [-1] * n
ans[0] = 0
while a:
    x = a.popleft()
    y = b.popleft()
    if ans[x] == -1 and ans[y] == -1:
        a.append(x)
        b.append(y)
    elif ans[x] == -1:
        ans[x] = ans[y] + 1
    elif ans[y] == -1:
        ans[y] = ans[x] + 1
    else:
        ans[x] = min(ans[x],ans[y]+1)
        ans[y] = min(ans[y],ans[x]+1)
if -1 in ans:
    print('No')
else:
    print('Yes')
    for i in ans[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
a,b = deque(),deque()
for i in range(m):
    x,y = map(int,input().split())
    x,y = x-1,y-1
    if x > y:
        x,y = y,x
    a.append(x)
    b.append(y)
ans = [-1] * n
ans[0] = 0
queue = deque()
for i in range(n):
    queue.append(i)
while queue:
    h = queue.popleft()
    flg = False
    p = 0
    q = len(a)
    while p < q:
        x = a.popleft()
        y = b.popleft()
        if h == x or h == y:
            if ans[x] == -1 and ans[y] == -1:
                a.append(x)
                b.append(y)
                flg = True
            elif ans[x] == -1:
                ans[x] = ans[y] + 1
            elif ans[y] == -1:
                ans[y] = ans[x] + 1
            else:
                ans[x] = min(ans[x],ans[y]+1)
                ans[y] = min(ans[y],ans[x]+1)
        else:
            a.append(x)
            b.append(y)
        p += 1
    if flg:
        queue.append(h)
if -1 in ans:
    print('No')
else:
    print('Yes')
    for i in ans[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M = map(int, input().split())
net = [[] for _ in range(N+1)]
vect = [0]*(N+1)

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

q = deque()
q.append(1)

while len(q)>0:
	t = q.popleft()
	for i in net[t]:
		if t != i and vect[i] == 0:
			vect[i] = t
			q.append(i)

for i in range(2, N+1):
	print(vect[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

que = deque()
que.append(0)

visited = [0 for _ in range(n)]
visited[0] = 1
ans = [None for _ in range(n)]

while len(que) > 0:
    c = que.popleft()
    for ne in graph[c]:
        if visited[ne] == 1:
            continue
        que.append(ne)
        ans[ne] = c
        # print(f'ans[{ne}] = {c}')
        visited[ne] = 1

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

g = [[] for i in range(n+1)]

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

def bfs(u):
  queue = deque([u])
  d = [None] * (n+1)
  d[u] = 0
  while queue:
    v = queue.popleft()
    for i in g[v]:
      if d[i] is None:
        d[i] = d[v] + 1
        queue.append(i)
  return d

ans = bfs(1)[2:]
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M = map(int, input().split())
routes = [[] for _ in range(N+1)]
for _ in range(M):
  a, b = map(int, input().split())
  routes[a].append(b)
  routes[b].append(a)
checked = [0 for _ in range(N+1)]
route = deque(routes[1])
for r in routes[1]:
  checked[r] = 1
while(len(route) > 0):
  r = route.popleft()
  for rr in routes[r]:
    if checked[rr] == 0:
      checked[rr] = r
      route.append(rr)
for i in range(1, N+1):
  if checked[i] == 0:
    print('No')
    exit()
print('Yes')
for j in range(1, N+1):
  print(checked[j])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

d=deque()
d.append(0)

while d:
    v=d.popleft()
    for i in graph[v]:
        if dist[i] != -1:
            continue
        else:
            dist[i]=dist[v]+1
            d.append(i)

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

Submission status: Wrong Answer

Solution code:
def main():
	N, M = [int(x) for x in input().split(" ")]
	route = [[] for i in range(N)]
	for i in range(M):
		A, B = [int(y) for y in input().split(" ")]
		route[A - 1] += [B]
		route[B - 1] += [A]

	checked = [1] + [0] * (N - 1)
	to_check = [{"src" : s, "dst" : 1} for s in route[0]]
	signal = [-1] + [0] * (N - 1)
	signal_cnt = 0

	for r in route[0]:
		checked[r - 1] = 1

	while len(to_check) > 0:
		checking = to_check.pop(0)
		src = checking["src"]
		signal[src - 1] = str(checking["dst"])
		signal_cnt += 1
		rooms = route[src - 1]
		for r in rooms:
			if checked[r - 1] == 0:
				to_check.append({"src": r, "dst": src})
				checked[r - 1] = 1
	if signal_cnt != N - 1:
		print("No")
	else:
		print("Yes")
		print("\n".join(signal[1:]))

main()

Submission status: Wrong Answer

Solution code:
import queue
import sys
imput = sys.stdin.readline

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

conne = [[] for _ in range(n)]
dist = [99999]*(n)
par = [0] * (n)

for i in range(m):
    a,b = map(int,input().split()) 
    conne[a-1].append(b-1)
    conne[b-1].append(a-1)
    
q=queue.Queue()
dist[0] = 0
q.put((0,0))

while not q.empty():
    
    x,d = q.get()
    
    for i in conne[x]:
        if dist[i]==99999:
            q.put((i,d+1))
            par[i] = x
            dist[i] = d+1

print("Yes")    

for i in par[1:]:
    print(i)

Submission status: Wrong Answer

Solution code:
import queue
import sys
imput = sys.stdin.readline

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

conne = [[] for _ in range(n+1)]
dist = [99999]*(n+1)
par = [0] * (n+1)

for i in range(m):
    a,b = map(int,input().split()) 
    conne[a].append(b)
    conne[b].append(a)
    
q=queue.Queue()
dist[1] = 0
q.put((1,0))

while not q.empty():
    
    x,d = q.get()
    
    for i in conne[x]:
        if dist[i]==99999:
            q.put((i,d+1))
            par[i] = x
            dist[i] = d+1

if max(dist[1:]) == 99999:
    print("No")
    
else:
    print("Yes")    
    
    for i in par[2:]:
        print(i)

Submission status: Wrong Answer

Solution code:
import queue
import sys
imput = sys.stdin.readline

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

conne = [[] for _ in range(n+1)]
dist = [99999]*(n+1)
par = [0] * (n+1)

for i in range(m):
    a,b = map(int,input().split()) 
    conne[a].append(b)
    conne[b].append(a)
    
q=queue.Queue()
dist[1] = 0
q.put((1,0))

while not q.empty():
    
    x,d = q.get()
    
    for i in conne[x]:
        if dist[i]>=d+1:
            q.put((i,d+1))
            par[i] = x
            dist[i] = d+1

if max(dist[1:]) == 99999:
    print("No")
    
else:
    print("Yes")    
    
    for i in par[2:]:
        print(i)

Submission status: Wrong Answer

Solution code:
import queue
import sys
imput = sys.stdin.readline

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

conne = [[] for _ in range(n+1)]
dist = [99999]*(n+1)
par = [0] * (n+1)

for i in range(m):
    a,b = map(int,input().split()) 
    conne[a].append(b)
    conne[b].append(a)
    
q=queue.Queue()
dist[1] = 0
q.put((1,0))

while not q.empty():
    
    x,d = q.get()
    
    for i in conne[x]:
        if dist[i]>d+1:
            q.put((i,d+1))
            par[i] = x
            dist[i] = d+1

if max(dist[1:]) == 99999:
    print("No")
    
else:
    print("Yes")    
    
    for i in par[2:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
r=[[] for i in range(n+1)]
for i in range(m):
    a,b=map(int,input().split())
    r[a].append(b)
    r[b].append(a)
dep=[-1]*(n+1)
dep[0]=0
dep[1]=0
data=deque([1])
d=0
while len(data)>0:
    p=data.popleft()
    for i in r[p]:
        if dep[i]==-1:
            dep[i]=dep[p]+1
            data.append(i)

if not all(dep[i+1]>=0 for i in range(n)):
    print("No")
else:
    print("Yes")
    for i in range(2,n+1):
        for j in r[i]:
            if dep[j]==dep[i]-1:
                print(j)
                continue

Submission status: Wrong Answer

Solution code:
def resolve():
    N, M = list(map(int, input().split()))
    AB = {}
    for _ in range(M):
        a, b = list(map(int, input().split()))
        a, b = a-1, b-1
        if a not in AB:
            AB[a] = []
        if b not in AB:
            AB[b] = []
        AB[a].append(b)
        AB[b].append(a)

    import collections
    q = collections.deque([0])
    metrics = [-1 for i in range(N)]
    metrics[0] = 0
    while q:
        node = q.pop()
        metr = metrics[node]
        for nxt in AB[node]:
            if metrics[nxt] == -1:
                metrics[nxt] = metr + 1
                q.appendleft(nxt)
    #print(metrics)
    for m in metrics:
        if m == -1:
            print("No")
            return
    print("Yes")
    for i in range(1, N):
        print(metrics[i])


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

N, M = map(int, input().split())
edge = np.array([input().split() for _ in range(M)], dtype = np.int64).T
ones = np.ones(M)
graph = csr_matrix((ones, (edge[:2] - 1)), (N, N))


distance_mat, processors = dijkstra(graph, directed = False, indices = 0, return_predecessors = True)

processors = processors + 1

'''
print ('点0からの最短距離')
print (distance_mat)


print ('各点の直前の点')
print (processors)
'''

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

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

N, M = map(int, input().split())
edge = np.array([input().split() for _ in range(M)], dtype = np.int64).T
ones = np.ones(M)
graph = csr_matrix((ones, (edge[:2] - 1)), (N, N))


distance_mat, processors = dijkstra(graph, directed = False, indices = 0, return_predecessors = True)

'''
print ('点0からの最短距離')
print (distance_mat)

processors = processors + 1
print ('各点の直前の点')
print (processors)
'''

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

Submission status: Wrong Answer

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

dist = [0] + [N + 10] * N
dist[1] = 0

ans = [0] + [-1] * N

def memo_index(x):
    return [i for i, _x in enumerate(ans) if _x == x]

for _ in range(M):
    a, b = map(int, input().split())

    if dist[a] > dist[b]:
        dist[a] = dist[b] + 1
        ans[a] = b
        for item in memo_index(a):
            dist[item] = dist[a] + 1

    elif dist[b] > dist[a]:
        dist[b] = dist[a] + 1
        ans[b] = a
        for item in memo_index(b):
            dist[item] = dist[b] + 1

    elif dist[a] == dist[b] and dist[a] > N:
        ans[a] = b
        ans[b] = a

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

Submission status: Wrong Answer

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

dist = [0] + [N + 10] * N
dist[1] = 0

ans = [0] + [-1] * N
ans[1] = "Yes"

def memo_index(x):
    return [i for i, _x in enumerate(ans) if _x == x]

for _ in range(M):
    a, b = map(int, input().split())

    if dist[a] > dist[b]:
        dist[a] = dist[b] + 1
        ans[a] = b
        for item in memo_index(a):
            dist[item] = dist[a] + 1

    elif dist[b] > dist[a]:
        dist[b] = dist[a] + 1
        ans[b] = a
        for item in memo_index(b):
            dist[item] = dist[b] + 1

    elif dist[a] == dist[b] and dist[a] > N:
        ans[a] = b
        ans[b] = a

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

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M=map(int,input().split())
haba=[[] for i in range(N+1)]
for i in range(M):
    A,B=map(int,input().split())
    haba[A].append(B)
    haba[B].append(A)
kyori=[-1]*(N+1)
kyori[0]=0
kyori[1]=0

que=deque()
que.append(1)

while que:
    kari=que.popleft()
    for i in haba[kari]:
        if kyori[i]!=-1:
            continue
        kyori[i]=kyori[kari]+1
        que.append(i)

ans=kyori[1:]
print("Yes")
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M=map(int,input().split())
haba=[[] for i in range(N+1)]
for i in range(M):
    A,B=map(int,input().split())
    haba[A].append(B)
    haba[B].append(A)
kyori=[-1]*(N+1)
kyori[0]=0
kyori[1]=0

que=deque()
que.append(1)

while que:
    kari=que.popleft()
    for i in haba[kari]:
        if kyori[i]!=-1:
            continue
        kyori[i]=kyori[kari]+1
        que.append(i)

ans=kyori[2:]
print("Yes")
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
sys.setrecursionlimit(500*500)
# import numpy as np
from collections import deque

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# A_B = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# edges = [list(map(int,input().split())) for _ in range(M)]
# tree = [[] for _ in range(N + 1)]

# for edge in edges:
#     tree[edge[0]].append([edge[1], edge[2]])
#     tree[edge[1]].append([edge[0], edge[2]])

# depth = [-1] * (N + 1)
# depth[1] = 0

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])
#     if temp!=1:
#         arr.append([temp, 1])
#     if arr==[]:
#         arr.append([n, 1])
#     return arr

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

for edge in edges:
    tree[edge[0]].append(edge[1])
    tree[edge[1]].append(edge[0])

depth = [-1] * (N + 1)
depth[1] = 0

d = deque()
d.append(1)

while d:
 v = d.popleft()
 for i in tree[v]:
   if depth[i] != -1:
     continue
   depth[i] = depth[v] + 1
   d.append(i)

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

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
import sys
sys.setrecursionlimit(500*500)
# import numpy as np
from collections import deque

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# A_B = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# edges = [list(map(int,input().split())) for _ in range(M)]
# tree = [[] for _ in range(N + 1)]

# for edge in edges:
#     tree[edge[0]].append([edge[1], edge[2]])
#     tree[edge[1]].append([edge[0], edge[2]])

# depth = [-1] * (N + 1)
# depth[1] = 0

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])
#     if temp!=1:
#         arr.append([temp, 1])
#     if arr==[]:
#         arr.append([n, 1])
#     return arr

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

for edge in edges:
    tree[edge[0]].append(edge[1])
    tree[edge[1]].append(edge[0])

depth = [-1] * (N + 1)
depth[1] = 0

d = deque()
d.append(1)

while d:
 v = d.popleft()
 for i in tree[v]:
   if depth[i] != -1:
     continue
   depth[i] = depth[v] + 1
   d.append(i)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque

YES = "Yes"  # type: str


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    dq = deque([])
    traversed = [False for _ in range(N+1)]
    ans = [None for _ in range(N+1)]

    G = {n : set([]) for n in range(1, N+1)}
    for a, b in zip(A,B):
        G[a].add(b)
        G[b].add(a)

    dq.append(A[0])
    traversed[A[0]] = True

    while dq:
        s = dq.popleft()

        for node in G[s]:
            if not traversed[node]:
                dq.append(node)
                traversed[node] = True
                

                ans[node] = s

    print(YES)
    for s in ans:
        if s != None:
            print(s)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
roads = []
for _ in range(N):
    roads.append([])
for _ in range(M):
    a, b = map(int, input().split())
    roads[a - 1].append(b - 1)
    roads[b - 1].append(a - 1)
visited = [float('inf')] * N
q = deque()
q.append(0)
visited[0] = 0
pre = [-1] * N
while q:
    now_room = q.popleft()
    if now_room == 5:
        print(1)
    for next_room in roads[now_room]:
        if visited[next_room] != float('inf'):
            continue
        visited[next_room] = visited[now_room] + 1
        if pre[next_room] == -1:
            pre[next_room] = now_room + 1
        q.append(next_room)
# print(*roads)
# print(*pre)

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