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:
import sys
import heapq
from typing import Callable, List, NoReturn, Set


def dijkstra(n, path) -> List[int]:
    total_cost: List[int] = [10**5] * (n + 1)
    total_cost[1] = 0

    not_checked: Set[int] = set(range(1, n + 1))
    checked: Set[int] = set()
    cur_node: int = 1
    h: List = []

    while not_checked:
        checked.add(cur_node)
        not_checked -= {cur_node}

        for next_node in path[cur_node]:
            if next_node in not_checked:
                heapq.heappush(h, (total_cost[cur_node] + 1, next_node))

        if not_checked:
            cost, next_node = heapq.heappop(h)  # type: int, int

            while next_node in checked:
                cost, next_node = heapq.heappop(h)

            cur_node = next_node
            total_cost[cur_node] = cost

    return total_cost


def main() -> NoReturn:
    readline: Callable[[], str] = sys.stdin.readline

    n, m = map(int, readline().rstrip().split())  # type: int, int
    path: List[List[int]] = []
    for _ in range(n + 1):
        path.append([])
    for _ in range(n):
        a, b = map(int, readline().rstrip().split())  # type: int, int
        (path[a]).append(b)
        (path[b]).append(a)

    print("Yes")
    total_costs: List[int] = dijkstra(n, path)
    for i in range(2, n + 1):
        print(total_costs[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy
n,m = map(int,input().split())
graph = [[] for _ in range(m)]
for i in range(m):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
memo = [0]*n
now = [[0,0]]
nxt = []
done = [False]*n
done[0] = True


while now:
    for l in now:
        for j in graph[l[1]]:
            if not done[j]:                
                done[j] = True
                nxt.append([l[1],j])
                memo[j] = l[1]+1
    now = copy.copy(nxt)
    nxt = []
for l in now:
    done[l[1]] = True
if False in done:
    print("No")
else:
    print("Yes")
    for i in range(1,n):
        print(memo[i])

Submission status: Runtime Error

Solution code:
from collections import deque

N, M = map(int, input().split())
con = [[] for _ in range(M)]
for i in range(M):
    A, B = map(int, input().split())
    con[A-1].append(B-1)
    con[B-1].append(A-1)
    
d = deque()
houmon = [False] * N
ans = [0] * (N-1)

houmon[0] = True
d.append(0)
while d:
#     print(d, houmon, ans)
    room = d.popleft()
#     houmon[room] = True
#     print(room)
    
    for next_room in con[room]:
        if not houmon[next_room]:
            ans[next_room-1] = room+1
            d.append(next_room)
            houmon[next_room] = True
    
print('Yes\n' + '\n'.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


is_visited = [False for _ in range(N)]
ans = [0 for _ in range(N)]
def bfs():
    queue = deque()
    queue.append(0)
    is_visited[0] = True
    while queue:
        pos = queue.popleft()
        for next_pos in A[pos]:
            if is_visited[next_pos] == False:
                is_visited[next_pos] = True
                queue.append(next_pos)
                ans[next_pos] = pos

bfs()

if False in is_visited:
    print("No")
else:
    print("Yes")
    for i in range(N-1):
        print(ans[i+1]+1)

Submission status: Runtime Error

Solution code:
import copy
n,m = map(int,input().split())
graph = [[] for _ in range(m)]
for i in range(m):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
memo = [0]*n
now = [[0,0]]
nxt = []
done = [False]*n
done[0] = True


while now:
    for l in now:
        for j in graph[l[1]]:
            if not done[j]:                
                done[j] = True
                nxt.append([l[1],j])
                memo[j] = l[1]+1
    now = copy.copy(nxt)
    nxt = []
    print(memo)
for l in now:
    done[l[1]] = True
if False in done:
    print("No")
else:
    print("Yes")
    for i in range(1,n):
        print(memo[i])

Submission status: Runtime Error

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

n, m = map(int, input().split())
paths = []

while True:
    try:
        paths.append(list(map(int, input().split())))
    except EOFError:
        break


aj_matrix = np.zeros((n, n), dtype=int)
for path in paths:
    aj_matrix[path[0] - 1][path[1] - 1] = 1
    aj_matrix[path[1] - 1][path[0] - 1] = 1
result = shortest_path(aj_matrix, indices=0)

if np.isinf(result).sum() == 0:
    result = result.astype(int).tolist()
    print('Yes')
    print(*result[1:], sep='\n')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(3000)
n,m =map(int,input().split())
pas = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    pas[a].append(b)
    pas[b].append(a)

seen = [False for i in range(n)]
que = deque()
def bfn(i,j):
    if seen[i-1]==False:
        seen[i-1]=j
    for x in pas[i]:
        if seen[x-1]!=False:
            continue
        que.append([x,i])
    if que:
        now,pre =que.popleft()
        bfn(now,pre)
    else:
        return

bfn(1,1)

if False in seen:
    print("No")
else:
    print("Yes")
    for i in range(n-1):
        print(seen[i+1])

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
from heapq import heappop,heappush 

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

mark=[-1]*(N-1)

inf=float("inf")

di=[inf]*N
di[0]=0
hq=[(0,0)]

while hq:
    t,r=heappop(hq) #time room
    if t>di[r]:
        continue
    for x in graph[r]:
        t_new=t+1
        r_new=x
        if t_new<di[r_new]:
            di[r_new]=t_new
            heappush(hq,(t_new,r_new))
            if mark[r_new-1]==-1:
                mark[r_new-1]=r
            else:
                print("No")
                exit()
    
print("Yes")
for i in mark:
    print(i+1)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
ans=[0]*m
route={}
for i in range(m):
  a,b=map(int,input().split())
  if a in route:
    li=route[a]
    li.append(b)
    route[a]=li
  else:
    route[a]=[b]
  if b in route:
    li=route[b]
    li.append(a)
    route[b]=li
  else:
    route[b]=[a]
que=[1]
while sorted(ans)[0]==0:
  x=que.pop(0)
  que.extend(route[x])
  for k in route[x]:
    if ans[k-1]==0:
      ans[k-1]=x
ans.pop(0)
print("Yes")
for k in ans:
  print(k)

Submission status: Runtime Error

Solution code:
import bisect, collections, copy, heapq, itertools, math, numpy, string
import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def MS(): return map(str, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
# N = I()
# A = [LI() for _ in range(N)]


n,m = map (int,input().split())
a = [0] * n
b = [0] * n
box = [[] for i in range(n)]
s = deque()
ans = [-1] * (n-1)
for i in range(n):
    a[i],b[i] = map (int,input().split())
    box[a[i]-1].append (b[i])
    box[b[i] - 1].append (a[i])
for i in range(len(box[0])):
    s.append([1,box[0][i]])

while len (s) > 0:
    k = s.popleft()

    if ans[k[1] - 2] == -1:
        ans[k[1] - 2] = k[0]

        for i in range(len(box[k[1] - 1])):
            s.append([k[1], box[k[1] - 1][i]])

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

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
from heapq import heappop,heappush 

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

mark=[-1]*(N-1)

inf=float("inf")

di=[inf]*N
di[0]=0
hq=[(0,0)]

while hq:
    t,r=heappop(hq) #time room
    if t>di[r]:
        continue
    for x in graph[r]:
        t_new=t+1
        r_new=x
        if t_new<di[r_new]:
            di[r_new]=t_new
            heappush(hq,(t_new,r_new))
            if mark[r_new-1]==-1:
                mark[r_new-1]=r
            else:
                print("No")
    
print("Yes")
for i in mark:
    print(i+1)

Submission status: Runtime Error

Solution code:
n,m = map (int,input().split())
a = [0] * n
b = [0] * n
box = [[] for i in range(n)]
s = deque()
ans = [-1] * (n-1)
for i in range(n):
    a[i],b[i] = map (int,input().split())
    box[a[i]-1].append (b[i])
    box[b[i] - 1].append (a[i])
for i in range(len(box[0])):
    s.append([1,box[0][i]])

while len (s) > 0:
    k = s.popleft()

    if ans[k[1] - 2] == -1:
        ans[k[1] - 2] = k[0]

        for i in range(len(box[k[1] - 1])):
            s.append([k[1], box[k[1] - 1][i]])

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

Submission status: Runtime Error

Solution code:
from collections import deque
sys.setrecursionlimit(3000)
n,m =map(int,input().split())
pas = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    pas[a].append(b)
    pas[b].append(a)

seen = [False for i in range(n)]
que = deque()
def bfn(i,j):
    if seen[i-1]==False:
        seen[i-1]=j
    for x in pas[i]:
        if seen[x-1]!=False:
            continue
        que.append([x,i])
    if que:
        now,pre =que.popleft()
        bfn(now,pre)
    else:
        return

bfn(1,1)

if False in seen:
    print("No")
else:
    print("Yes")
    for i in range(n-1):
        print(seen[i+1])

Submission status: Runtime Error

Solution code:
from collections import deque
N, M = map(int, input().split())
graph = [[] for _ in range(M)]
for i in range(M):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)
#print(graph)

queue = deque()
seen = [-1] * N
steps = [-1] * N
steps[0] = 0
parents = [-1] * N
queue.append(0)
while queue:
    node = queue.popleft()

    seen[node] = 1
    #print("now: "+str(node))
    for next in graph[node]:
        if seen[next] > -1:
            continue
        #print("next: " + str(next))
        queue.append(next)
        seen[next] = 1
        steps[next] = steps[node] + 1
        parents[next] = node

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

Submission status: Runtime Error

Solution code:
from collections import deque
sys.setrecursionlimit(100000)
n,m =map(int,input().split())
pas = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    pas[a].append(b)
    pas[b].append(a)

seen = [False for i in range(n)]
que = deque()
def bfn(i,j):
    if seen[i-1]==False:
        seen[i-1]=j
    for x in pas[i]:
        if seen[x-1]!=False:
            continue
        que.append([x,i])
    if que:
        now,pre =que.popleft()
        bfn(now,pre)
    else:
        return

bfn(1,1)

if False in seen:
    print("No")
else:
    print("Yes")
    for i in range(n-1):
        print(seen[i+1])

Submission status: Runtime Error

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *

import numpy as np

import math

def rootCheck(checkList):
    nextList=[]
    for i in checkList:
        for j in root[i]:
            if hint[j] == -1:
                hint[j] = i
                nextList.append(j)
    if len(nextList) >0:
        rootCheck(nextList)



if __name__ == '__main__':
    n, m = map(int, input().split())
    root = {}
    for i in range(m):
        b, c = map(int, input().split())
        if b-1 in root:
            root[b-1].append(c-1)
        else:
            root[b-1]=[c-1]
        if c-1 in root:
            root[c-1].append(b-1)
        else:
            root[c-1]=[b-1]
 
    hint =[-1]*n

    rootCheck(root[0])

    if -1 in hint:
        print("No")
    else:
        print("Yes")
        for i in hint[1:]:
            print(i+1)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = map(int,input().split())
visited = [False for _ in range(n)]
label = [-1 for _ in range(n)]
g = [[] for _ in range(m)]
for _ in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

pos = 0
que = deque([])
for nx in g[0]:
    que.append((nx, pos))
while que:
    to, now = que.popleft()
    if visited[to] == True:
        continue
    else:
        visited[to] = True
        label[to] = now
        for nx in g[to]:
            que.append((nx, to))
if -1 in label[1:]:
    print('No')
else:
    print('Yes')
    for l in label[1:]:
        print(l+1)

Submission status: Runtime Error

Solution code:
from _collections import deque
n,m=map(int,input().split())
X=[[] for _ in range(n)]
visited=[0]*(n+1)
ANS=[0]*n
ANS[0]=1
for i in range(m):
    a,b=map(int,input().split())
    X[a-1].append(b-1)
    X[b-1].append(a-1)

q=deque([0])
visited[0]=1
while len(q)>0:
    s=q.popleft()
    for x in X[s]:
        if visited[x]:
            continue
        q.append(x)
        ANS[x]=s
        visited[x]=1
    print(ANS)
print('Yes')
for ans in ANS[1:]:
    print(ans+1)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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


is_visited = [False for _ in range(N)]
ans = [0 for _ in range(N)]
def bfs():
    queue = deque()
    queue.append(0)
    is_visited[0] = True
    while queue:
        pos = queue.popleft()
        for next_pos in A[pos]:
            if is_visited[next_pos] == False:
                is_visited[next_pos] = True
                queue.append(next_pos)
                ans[next_pos] = pos

bfs()
#print(is_visited)                                                                                     

if False in is_visited:
    print("No")
else:
    for i in range(N-1):
        print(ans[i+1]+1)

Submission status: Runtime Error

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


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


graph = csr_matrix(graph)

dist_matrix, predecessors = dijkstra(csgraph=graph, directed=False, indices=0, return_predecessors=True)

if -9999 in predecessors[1:]:
    print("No")
else:
    ans=predecessors[1:]+1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Runtime Error

Solution code:
from _collections import deque
N, M = map(int, input().split())
graph = [[] for _ in range(M)]
for i in range(M):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)
#print(graph)

queue = deque()
seen = [-1] * N
steps = [-1] * N
steps[0] = 0
parents = [-1] * N
queue.append(0)
while queue:
    node = queue.popleft()

    seen[node] = 1
    #print("now: "+str(node))
    for next in graph[node]:
        if seen[next] > -1:
            continue
        #print("next: " + str(next))
        queue.append(next)
        seen[next] = 1
        steps[next] = steps[node] + 1
        parents[next] = node

for a in parents[1:]:
    print(a+1)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10**5+2)
    
    N,M=list(map(int,sys.stdin.readline().split()))
    dic=defaultdict(list)
    for i in range(M):
        A,B=list(map(int,sys.stdin.readline().split()))
        dic[A].append(B)
        dic[B].append(A)

    L=[-1]*(N+1)
    R=[-1]*(N+1)

    def dfs(now,count):
        for goto in dic[now]:
            if L[goto]==-1 or count<L[goto]:
                L[goto]=count
                R[goto]=now
                dfs(goto,count+1)

    dfs(1,1)
    if any(x+1 for x in R[2:]):
        print('Yes')
        for x in R[2:]:
            print(x)
    else:
        print('No')
    


main()

Submission status: Runtime Error

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

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

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


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

D = Dijkstra(N)

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

L = D.exe(0)

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

R = {}

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

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

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

Submission status: Runtime Error

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

seen = [False for i in range(n)]
que = deque()
def bfn(i,j):
    if seen[i-1]==False:
        seen[i-1]=j
    for x in pas[i]:
        if seen[x-1]!=False:
            continue
        que.append([x,i])
    if que:
        now,pre =que.popleft()
        bfn(now,pre)
    else:
        return

bfn(1,1)

if False in seen:
    print("No")
else:
    print("Yes")
    for i in range(n-1):
        print(seen[i+1])

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = list(map(int, input().split()))
al = [0] * n
bl = [0] * n
G = [[] for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
N = n
# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

dist = [-1]*N
tag = [-1]*N
que = deque([0])
dist[0] = 0
pre = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        tag[w] = v + 1
        que.append(w)

print('Yes')
for t in tag[1:]:
    print(t)

Submission status: Runtime Error

Solution code:
from sys import stdin


class DFS():
    def __init__(self,A_B_arr,N,M):
        self.N = N
        self.M = M
        self.route = {i:[] for i in range(N+1)}
        for A, B in A_B_arr:
            self.route[A].append(B)
            self.route[B].append(A)

        self.dp = [[None,float('inf')] for i in range(N+1)]

    def dfs(self,i,rank):
        for r in self.route[i]:
            if rank<self.dp[r][1]:
                self.dp[r] = [i,rank]
                self.dfs(r,rank+1)
        return


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, M = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_B_arr = []
    for i in range(M):
        _ = list(map(int,_in[i+1].split(' ')))  # type:list(int)
        A_B_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    dfs = DFS(A_B_arr,N,M)
    dfs.dfs(1,1)

    ans = []
    for (i,_) in enumerate(dfs.dp):
        if i>1 and _[0] == None:
            TF = 'No'
            break
        else:
            ans.append(_[0])
    else:
        TF = 'Yes'
        ans = ans[2:]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    if TF == 'Yes':
        print(TF)
        for _ in ans:
            print(_)
    else:
        print(TF)

Submission status: Runtime Error

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

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

S = [-1] * N
S[0] = 0
NXT = [[] for _ in range(N)]  # 最大ステップ
NXT[0] = [0]

for s in range(N):
    for k in NXT[s]:  # sステップ目で到達
        for i in R[k]:
            if S[i] == -1:  # 未更新
                S[i] = k
                NXT[s+1].append(i)

    if S.count(-1) == 0:
        break

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

Submission status: Runtime Error

Solution code:
import sys
import math
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

a,b,h,m = map(int,readline().split())

degree = abs(h*30+30*(m/60) - m*6)
degree = min(degree,180-degree)
if degree == 0 or degree == 180:
    print(0)
    exit()
print(math.sqrt(a**2+b**2-2*a*b*math.cos(math.radians(degree))))

Submission status: Runtime Error

Solution code:
from sys import stdin


class DFS():
    def __init__(self,A_B_arr,N,M):
        self.N = N
        self.M = M
        self.route = {i:[] for i in range(N+1)}
        for A, B in A_B_arr:
            self.route[A].append(B)
            self.route[B].append(A)

        self.dp = [[None,float('inf')] for i in range(N+1)]

    def dfs(self,i,rank):
        for r in self.route[i]:
            if rank<self.dp[r][1]:
                self.dp[r] = [i,rank]
                self.dfs(r,rank+1)
        return


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, M = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_B_arr = []
    for i in range(M):
        _ = list(map(int,_in[i+1].split(' ')))  # type:list(int)
        A_B_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    dfs = DFS(A_B_arr,N,M)
    dfs.dfs(1,1)


    print(dfs.dp)
    ans = []
    for (i,_) in enumerate(dfs.dp):
        if i>1 and _[0] == None:
            TF = 'No'
            break
        else:
            ans.append(_[0])
    else:
        TF = 'Yes'
        ans = ans[2:]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    if TF == 'Yes':
        print(TF)
        for _ in ans:
            print(_)
    else:
        print(TF)

Submission status: Runtime Error

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

n, m = map(int, input().split())
e = np.zeros((n, n))
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    e[a][b] = 1
    e[b][a] = 1
dist, predecessor = dijkstra(e, directed=False, return_predecessors=True)
for v in predecessor[0][1:]:
    print(v+1)

Submission status: Runtime Error

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


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


graph = csr_matrix(graph)

dist_matrix, predecessors = dijkstra(csgraph=graph, directed=False, indices=0, return_predecessors=True)

if -9999 in predecessors[1:]:
    print("No")
else:
    ans=predecessors[1:]+1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Runtime Error

Solution code:
from collections import deque


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

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

dist = [-1] * n
prev = [-1] * n
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in g[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)
        prev[w] = v

print("Yes")
for i in prev[1:]:
    print(i + 1)

Submission status: Runtime Error

Solution code:
#coding: utf-8
from collections import deque
N, M = map(int, input().split())
dd = {i:[] for i in range(N)}
for i in range(N):
    d[i][i] = 0
for i in range(M):
    a, b = map(int, input().split())
    dd[a-1].append(b-1)
    dd[b-1].append(a-1)
ans = [0 for _ in range(N)]
visited = [False for _ in range(N)]
queue = []
for i in range(len(dd[0])):
    queue.append([0, dd[0][i]])
queue = deque(queue)
while queue:
    q = queue.popleft()
    if visited[q[1]]:
        continue
    visited[q[1]] = True
    ans[q[1]] = q[0] + 1
    for v in dd[q[1]]:
        if not visited[v]:
            queue.append([q[1], v])
print("Yes")
print(*ans[1:], sep="\n")

Submission status: Runtime Error

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

def dfs(i, ib, l):
    global ans
    if ans[i][0] == -1 or l < ans[i][0]:
        ans[i][0] = l
        ans[i][1] = ib
        for next in root[i]:
            dfs(next, i, l+1)

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

Submission status: Runtime Error

Solution code:
import queue

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

adj = [[False] * M for i in range(M)]

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

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

Q = queue.Queue()
Q.put(0)

while not Q.empty():
    i = Q.get()
    for j in range(N):
        if adj[i][j] == 0:
            continue
        if node[j] != -1:
            continue
        node[j] = i
        Q.put(j)

if any([i == -1 for i in node]):
    print('No')
else:
    print('Yes')
    for i in node[1:]:
        print(i + 1)

Submission status: Runtime Error

Solution code:
N, M = list(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)
    
ans = [-1 for _ in range(N)]
ans[0] = 1

stack = [0]

while stack:
    j = stack.pop(0)
    for i in G[j]:
        if ans[i] == -1:
            ans[i] = j
            stack.append(i)
            
for i in range(1, N):
    if ans[i] == -1:
        print("No")
        break
        
else:
    print("Yes")            
    for i in range(1, N):
        print(ans[i] + 1)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import numpy

rl = sys.stdin.readline

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

marks = [-1]*n
stack = [0]
while stack:
    for next_room in li[stack[0]]:
        if marks[next_room] == -1:
            stack.append(next_room)
            marks[next_room] = stack[0]
    del stack[0]
if -1 in stack:
  print('No')
else:
  print('Yes')
  for i in marks[1:]:
    print(i+1)

Submission status: Runtime Error

Solution code:
import itertools
from collections import defaultdict
import collections
import math
import sys
import queue
sys.setrecursionlimit(200000)
mod = 1000000007

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

graph = {i: [] for i in range(1, m + 1)}
for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

tf = [False] * (n + 1)
sirube = [0] * (n + 1)
def bfs(v):
    q = queue.Queue()
    q.put([v,0])
    tf[v] = True
    while not q.empty():
        sub = q.get()
        now = sub[0]
        before = sub[1]
        for next in graph[now]:
            if tf[next]:
                continue
            tf[next] = True
            if sirube[next] == 0:
                sirube[next] = now
            q.put([next, now])

bfs(1)
for i in range(2, n + 1):
    if sirube[i] == 0:
        print("No")
        exit()

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

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10**7)
N,M=map(int,input().split())
A=[]
B=[]
road=[[] for i in range(N)]
for i in range(N):
  a,b=map(int,input().split())
  road[a-1].append(b-1)
  road[b-1].append(a-1)
visit=[0]*N

q=deque([0])
ans=[0]*N
visit[0]=1
def bfs(q):
  while(q):
    v=q.popleft()
    #print(v)
    for x in road[v]:
      if(visit[x]==0):
        q.append(x)
        #print(q)
        visit[x]=1
        ans[x]=v+1
        #print(x)
bfs(q)
print("Yes")
for i in range(N-1):
  print(ans[i+1])