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:
from collections import deque as dq
# import itertools
# import math
import networkx as nx
import sys
input = sys.stdin.readline


def main():
    n, m = map(int, input().split())
    limit = 10 ** 5
    # lst = [[] for _ in range(n + 1)]
    # ans = [0] * (n + 1)
    que = dq()
    # for _ in range(m):
    #     a, b = map(int, input().split())
    #     lst[a].append(b)
    #     lst[b].append(a)

    g = nx.Graph()
    g.add_edges_from([[i for i in input().split()] for _ in range(m)])
    # [[g.nodes[i][length] = limit ] for i in range(2, n + 1)]
    g.nodes[1]['length'] = 0
    for i in range(2, n + 1):
        g.nodes[i]['length'] = limit
        g.nodes[i]['next'] = 'Non'

    # d = {i: limit + 1 for i in range(1, n + 1)}
    # d[1] = 0
    que.append(1)
    # cnt = 1
    while que:
        visit = que.popleft()
        for next_v in g.succ[visit]:
            if g[next_v]['length'] > g[visit]['length'] + 1:
                g[next_v]['length'] = g[visit]['length'] + 1
                que.append(next_v)
                g[next_v]['next'] = visit

    if all(g.nodes[i]['next'] != 'Non' for i in range(2, n + 1)):
        print('Yes')
        for i in range(2, n + 1):
            print(g.nodes[i]['next'])
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
    def main():
        from collections import deque
        n, m, *ab = map(int, open(0).read().split())
        r = range(n+1)
        l = [[] for _ in r]
        for i, j in zip(*[iter(ab)] * 2):
            l[i].append(j)
            l[j].append(i)
     
        ll = [0 for _ in r]
        vis = [False for _ in r]
        que = deque()
        que.append(1)
        vis[1] = True
        while que:
            now = que.popleft()
            nx = l[now]
            for i in nx:
                if vis[i]:
                    continue
                vis[i] = True
                ll[i] = now
                que.append(i)
        if all(vis[1:]):
            print("Yes")
            s = "\n".join(map(str, ll[2:]))
            print(s)
        else:
            print("No")
     
     
     
    if __name__ == '__main__':
        main()

Submission status: Runtime Error

Solution code:
raw_input = list(map(int, input().strip().split()))
n = raw_input[0]
m = raw_input[1]
lines = {i: [] for i in range(m)}
for i in range(m):
    line = list(map(int, input().strip().split()))
    a = line[0] - 1
    b = line[1] - 1
    lines[a].append(b)
    lines[b].append(a)
is_checked_room = [False for _ in range(n)]
sign = [0 for _ in range(n)]
queue = [0]
is_checked_room[0] = True
while sum(is_checked_room) != n:
    if not queue:
        print('No')
        break
    target = queue.pop(0)
    line = lines[target]
    for l in line:
        if not is_checked_room[l]:
            is_checked_room[l] = True
            sign[l] = target
            queue.append(l)
if sum(is_checked_room) == n:
    print('Yes')
    for i in range(1, n):
        print(sign[i] + 1)

Submission status: Runtime Error

Solution code:
raw_input = list(map(int, input().strip().split()))
n = raw_input[0]
m = raw_input[1]
lines = {i: [] for i in range(m)}
for i in range(m):
    line = list(map(int, input().strip().split()))
    a = line[0] - 1
    b = line[1] - 1
    lines[a].append(b)
    lines[b].append(a)
is_checked_room = [False for _ in range(n)]
sign = [0 for _ in range(n)]
queue = [0]
is_checked_room[0] = True
while sum(is_checked_room) != n:
    target = queue.pop(0)
    line = lines[target]
    for l in line:
        if not is_checked_room[l]:
            is_checked_room[l] = True
            sign[l] = target
            queue.append(l)
    if not queue:
        print('No')
        break
if sum(is_checked_room) == n:
    print('Yes')
    for i in range(1, n):
        print(sign[i] + 1)

Submission status: Runtime Error

Solution code:
raw_input = list(map(int, input().strip().split()))
n = raw_input[0]
m = raw_input[1]
lines = {i: [] for i in range(m)}
for i in range(m):
    line = list(map(int, input().strip().split()))
    a = line[0] - 1
    b = line[1] - 1
    lines[a].append(b)
    lines[b].append(a)
is_checked_room = [False for _ in range(n)]
sign = [0 for _ in range(n)]
queue = [0]
is_checked_room[0] = True
while sum(is_checked_room) != n:
    target = queue[0]
    queue.pop(0)
    line = lines[target]
    for l in line:
        if not is_checked_room[l]:
            is_checked_room[l] = True
            sign[l] = target
            queue.append(l)
    if not queue:
        print('No')
        break
if sum(is_checked_room) == n:
    print('Yes')
    for i in range(1, n):
        print(sign[i] + 1)

Submission status: Runtime Error

Solution code:
def Func(rooms, n):
	distances = []
	for start in range(2, n+1):
		hasFound = False
		roomsToFind = [[start],]
		dist = 0
		print(start)
		while not hasFound:
			dist += 1
			for room in rooms[roomsToFind[0][-1]-1].connected:
				if room.ID in roomsToFind[0]:
					continue
				if room.ID == 1:
					if dist == 1:
						distances.append(1)
					else:
						distances.append(roomsToFind[0][1])
					hasFound = True
					break
				a = roomsToFind[0].copy()
				a.append(room.ID)
				roomsToFind.append(a)
			roomsToFind.pop(0)
			print(roomsToFind)
			if dist > n and hasFound == False:
				print("No")
				return
	print("Yes")
	for num in distances:
		print(num)

class Room:
	def __init__(self, ID):
		self.ID = ID
		self.connected = []

	def add(self, room):
		self.connected.append(room)


n, m = list(map(int, input().split()))
rooms = [Room(i) for i in range(1, n+1)]

for i in range(m):
	beg, end = list(map(int, input().split()))

	rooms[beg-1].add(rooms[end-1])
	rooms[end-1].add(rooms[beg-1])

Func(rooms, n)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m=map(int,input().split())
path=[[] for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    path[a-1].append(b-1)
    path[b-1].append(a-1)
nan=deque([0])
check=[1 if i==0 else -1 for i in range(n)]
def bfs():
    global n,m,path,nan
    l=len(nan)
    if not l:
        return
    for _ in range(l):
        x=nan.popleft()
        for j in path[x]:
            if check[j]==-1:
                check[j]=x
                nan.append(j)
    bfs()
bfs()
if any([i==-1 for i in check]):
    print("No")
else:
    print("Yes")
    for i in range(1,n):
        print(check[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
l = list(list(map(int, input().split())) for _ in range(m))
sign = list(list() for _ in range(m+1))
c = [0]*(m+1)
for i in l:
    sign[i[0]].append(i[1])
    sign[i[1]].append(i[0])
cnt = 0
x = [1]
k = []
while cnt != m-1:
    for i in x:
        a = i
        x = []
        for j in sign[a]:
            if c[j] == 0:
                c[j] = a
                cnt += 1
            x.append(j)
if cnt >= m-1:
    print('Yes')
    for i in c[2:]:
        print(i)
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
n, m = map(int, input().split())
route =[[] for i in range(m+1)]
for i in range(m):
    a, b = map(int, input().split())
    route[a].append(b)
    route[b].append(a)
visited = [0 for i in range(n+1)]
visited[1] = 1
ans = [0 for i in range(n+1)]
D = deque()
D.append(1)
while D:
    visit = D.popleft()
    for i in route[visit]:
        if visited[i] == 0:
            D.append(i)
            visited[i] = 1
            ans[i] = visit
print('Yes')
for i in range(2, n+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"

for i in list(connect_list[0]):
    root_list[i-1] = 1
now_set = connect_list[0]

def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for m in list(connect_list[j-1]):
            if root_list[m-1] == 0:
                root_list[m-1] = j
                # next_set.add(i)
                now_set.add(m)
        now_set.remove(j)
    do_ranking(now_set)


do_ranking(now_set)   #道しるべ作成

for k in root_list:
    print(k)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for m in list(connect_list[j-1]):
            if root_list[m-1] == 0:
                root_list[m-1] = j
                # next_set.add(i)
                now_set.add(m)
        now_set.remove(j)
    do_ranking(now_set)


for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for k in root_list:
    print(k)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                # next_set.add(i)
                now_set.add(i)
        now_set.remove(j)
    do_ranking(now_set)


# for A, B in AB:
#     connect_list[A-1].add(B)
#     connect_list[B-1].add(A)

for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                # next_set.add(i)
                now_set.add(i)
        now_set.remove(j)
    do_ranking(now_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
ways = [tuple(map(int, input().split())) for _ in range(m)]

table = [list() for _ in range(n+1)]
hush = {}
hush2 = {}
for i in range(1, n+1):
    hush[i] = False
    hush2[i] = False
ans = [0 for _ in range(n+1)]

for a, b in ways:
    table[a].append(b)
    table[b].append(a)

hush[1] = True

def bfs(i):
    if hush2[i] == True: return
    else: hush2[i] = True
    for room in table[i]:
        if hush[room] == False:
            ans[room] = i
            hush[room] = True
    for room in table[i]:
        bfs(room)

bfs(1)

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())
map_li = [[] for i in range(n)]
for i in range(m):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    map_li[a].append(b)
    map_li[b].append(a)

ans_li = [-1]*n
queue=deque()
queue.append(0)

while queue:
    x = queue.pop(0)
    for g in map_li[x]:
        if ans_li[g]==-1:
            ans_li[g]=x
if -1 in ans_li[1:]:
    print('No')
else:
    print('Yes')
    for ans in ans_li[1:]:
        print(ans+1)

Submission status: Runtime Error

Solution code:
def dfs(v,cnt):
    dist[v]=cnt
    for nv in graph[v]:
        if dist[nv]<=cnt+1:
            continue 
        guide[nv]=v
        dfs(nv,cnt+1)

n,m=map(int,input().split())
graph=[[] for _ in range(n+1)]
dist=[float("inf") for _ in range(n+1)]
guide=[0 for _ in range(n+1)]

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

dfs(1,0)

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

Submission status: Runtime Error

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

G = [[] for _ in range(m)]
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

ans = [-1] * n
# ans[0] = 'Yes'

from collections import deque

q = deque([0])

while q:
    _q = q.popleft()
    for x in G[_q]:
        if ans[x] == -1:
            ans[x] = _q + 1
            q.append(x)

if -1 in ans:
    print('No')
else:
    ans[0] = 'Yes'
    for _ans in ans:
        print(_ans)

Submission status: Runtime Error

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

G = [[] for _ in range(m)]
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

ans = [-1] * n
ans[0] = 'Yes'

from collections import deque

q = deque([0])

while q:
    _q = q.popleft()
    for x in G[_q]:
        if ans[x] == -1:
            ans[x] = _q + 1
            q.append(x)

# if -1 in ans:
#     print('No')
# else:
for _ans in ans:
    print(_ans)

Submission status: Runtime Error

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

G = [[] for _ in range(m)]
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

ans = [-1] * n
ans[0] = 'Yes'

from collections import deque

q = deque([0])

while q:
    _q = q.popleft()
    for x in G[_q]:
        if ans[x] == -1:
            ans[x] = _q
            q.append(x)

if -1 in ans:
    print('No')
else:
    print('Yes')
    for _ans in ans[1:]:
        print(_ans+1)

Submission status: Runtime Error

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

G = [[] for _ in range(m)]
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

ans = [-1] * n
ans[0] = 'Yes'

from collections import deque

q = deque([0])

pos = 0
while q:
    _q = q.popleft()
    for x in G[_q]:
        if ans[x-1] == -1:
            ans[x-1] = _q
            q.append(x-1)

if -1 in ans:
    print('No')
else:
    print('Yes')
    for _ans in ans[1:]:
        print(_ans+1)

Submission status: Runtime Error

Solution code:
#168
#d
n,m = map(int, input().split())# n = 　部屋数, m = 通路数
ln = [[0] for i in range(n+1)] #  部屋i が繋がる部屋はln[i] に示される
line = [0 for i in range(n-1)] # 1~nの印 部屋i の印はline[i-2]
for k in range(m):
    i,j  = map(int, input().split())
    ln[i].append(j)
    ln[j].append(i)

hy = [1] # 
while min(line) == 0 :
    new_hy = []
    for i in hy:
        for j in ln[i]:
            if line[j-2] == 0:
                line[j-2] = i
            new_hy.append(j)
            
    hy = set(new_hy)
    hy = list(new_hy)
    
print('Yes')            
for i in line:
    print(i)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
    
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    if i - 1 < N:
        root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

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

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

next_queue = deque()
sign = [-1] * n
sign[0] = 999

next_queue.append(0)

while next_queue:
    current_number = next_queue.popleft()
    for room in route[current_number]:
        if sign[room] == -1:
            sign[room] = current_number
            next_queue.append(room)
print("Yes")
for i in range(1, n):
    print(sign[i] + 1)

Submission status: Runtime Error

Solution code:
#1<=A     A sench (hour)
#B<=1000  B sench (min)
#0<=H<=11 hour
#0<=M<59  minute

N,M = map(int,input().split())
AB = []
while True:
    try:
        AB.append(list(map(int,input().split())))
    except:
        break

#N個 1~N
#M本 1~M

#print(AB)
class Room:
  def __init__(self, num, connected):
    #print('[Room.__init__]')
    self.num = num
    self.connected = connected
    self.first = 10**5
    self.dire = 0
  def add(self, con):
    self.connected.append(con)
  def showinfo(self):
    #print('[Room.showinfo]')
    print(str(self.num)+' '+str(self.connected)+' : '+str(self.first)) 

Rl = [Room(i,[]) for i in range(1,N+2)]
#Rl[0].showinfo()
#Rl[0].add(2)
#Rl[0].showinfo()
for i in range(M):
  Rl[AB[i][0]-1].add(AB[i][1])
  Rl[AB[i][1]-1].add(AB[i][0])

#for i in range(N):
#  Rl[i].showinfo()

  
Rl[0].first = 0

ma = [0]*N

def dijkstra(s):
  
  if(ma[s-1]==1):
    return
  ma[s-1]+=1
  
  if(Rl[s-1].num == 1):
    Rl[0].dire = 0
    
  leng = len(Rl[s-1].connected)
  for i in range(leng):
    Rl[ Rl[s-1].connected[i]-1 ].first = min(Rl[ Rl[s-1].connected[i]-1 ].first, Rl[s-1].first+1)
    if(Rl[ Rl[s-1].connected[i]-1 ].first == Rl[s-1].first+1): #update
      Rl[ Rl[s-1].connected[i]-1 ].dire = Rl[s-1].num
  for i in range(leng):
    dijkstra(Rl[s-1].connected[i])

dijkstra(1)
if(sum(ma) < N):
  print('No')#+str(sum(ma)))
  #for i in range(N):
  #  Rl[i].showinfo()
else:
  print('Yes')
  for i in range(1,N):
    print(Rl[i].dire)
  #for i in range(N):
  #  Rl[i].showinfo()

Submission status: Runtime Error

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

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

next_queue = deque()
sign = [-1] * n
sign[0] = 999

next_queue.append(1)

while next_queue:
    current_number = next_queue.popleft()
    for room in route[current_number - 1]:
        if sign[room - 1] == -1:
            sign[room - 1] = current_number
            next_queue.append(room)
print("Yes")
for i in range(1, n):
    print(sign[i])

Submission status: Runtime Error

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

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

next_queue = deque()
sign = [-1] * n

next_queue.append(1)

while next_queue:
    current_number = next_queue.popleft()
    for room in route[current_number - 1]:
        if sign[room - 1] == -1:
            sign[room - 1] = current_number
            next_queue.append(room)
print("Yes")
for i in range(1, n):
    print(sign[i])

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Submission status: Runtime Error

Solution code:
#1<=A     A sench (hour)
#B<=1000  B sench (min)
#0<=H<=11 hour
#0<=M<59  minute

N,M = map(int,input().split())
AB = []
while True:
    try:
        AB.append(list(map(int,input().split())))
    except:
        break

#N個 1~N
#M本 1~M

#print(AB)
class Room:
  def __init__(self, num, connected):
    #print('[Room.__init__]')
    self.num = num
    self.connected = connected
    self.first = 10**5
    self.dire = 0
  def add(self, con):
    self.connected.append(con)
  def showinfo(self):
    #print('[Room.showinfo]')
    print(str(self.num)+' '+str(self.connected)+' : '+str(self.first)) 

Rl = [Room(i,[]) for i in range(1,N+1)]
#Rl[0].showinfo()
#Rl[0].add(2)
#Rl[0].showinfo()
for i in range(M):
  Rl[AB[i][0]-1].add(AB[i][1])
  Rl[AB[i][1]-1].add(AB[i][0])

#for i in range(N):
#  Rl[i].showinfo()

  
Rl[0].first = 0

ma = [0]*N

def dijkstra(s):
  
  if(ma[s-1]==1):
    return
  ma[s-1]+=1
  
  if(Rl[s-1].num == 1):
    Rl[0].dire = 0
    
  leng = len(Rl[s-1].connected)
  for i in range(leng):
    Rl[ Rl[s-1].connected[i]-1 ].first = min(Rl[ Rl[s-1].connected[i]-1 ].first, Rl[s-1].first+1)
    if(Rl[ Rl[s-1].connected[i]-1 ].first == Rl[s-1].first+1): #update
      Rl[ Rl[s-1].connected[i]-1 ].dire = Rl[s-1].num
  for i in range(leng):
    dijkstra(Rl[s-1].connected[i])

dijkstra(1)
#if(sum(ma) < N):
#  print('No '+str(sum(ma)))
#  for i in range(N):
#    Rl[i].showinfo()
#else:
print('Yes')
for i in range(1,N):
  print(Rl[i].dire)
  #for i in range(N):
  #  Rl[i].showinfo()

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [0] * N
queue = deque()


def bfs():
    queue.append(0)
    while queue:
        now = queue.popleft()

        for c in nei_L[now]:
            if ans[c] == -1:
                ans[c] = now
                queue.append(c)


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

Submission status: Runtime Error

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

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 = shortest_path(
    e, directed=False, return_predecessors=True, unweighted=True, indices=0)

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [-1] * N
queue = deque()


def bfs():
    queue.append(0)
    while queue:
        now = queue.popleft()

        for c in nei_L[now]:
            if ans[c] == -1:
                ans[c] = now
                queue.append(c)


bfs()

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

Submission status: Runtime Error

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

lists = [False for i in range(N)]
lists[0] = True
a = N - 1
dicts = {i:[] for i in range(N)}
d = deque([0])

for i in range(M):
    tmpa, tmpb = map(lambda x: int(x) - 1, input().split()) 
    dicts[tmpa] += [tmpb]
    dicts[tmpb] += [tmpa]

print('Yes')
while a > 0:
    tmp = d.popleft()
    for i in dicts[tmp]:
        if not lists[i]:
            if tmp != 0:
                lists[i] = tmp
            else:
                lists[i] = -1
            d.append(i)
            a -= 1
    
for i in range(1, N):
    print(lists[i] + 1 if lists[i] != -1 else 1)

Submission status: Runtime Error

Solution code:
import queue

n,m = map(int,input().split())
hen = [[] for i in range(m)]
ans = [0 for i in range(n)]
a,b = 0,0

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

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

while q.empty() == False:
    head = q.get()
    for h in hen[head-1]:
        if h != 1 and ans[h-1] == 0:
            ans[h-1] = head
            q.put(h)

flag = 0

for i in range(n):
    if i != 0 and ans[i] == 0:
        flag = 1

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

Submission status: Runtime Error

Solution code:
import queue

n,m = map(int,input().split())
hen = [[] for i in range(m)]
ans = [0 for i in range(n)]
a,b = 0,0
max = 0

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

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

while q.empty() == False:
    head = q.get()
    for h in hen[head-1]:
        if h != 1 and ans[h-1] == 0:
            ans[h-1] = head
            q.put(h)

flag = 0

for i in range(n):
    if i != 0 and ans[i] == 0:
        flag = 1

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [-1] * N
queue = deque()


def bfs():
    queue.append(0)
    while len(queue) > 0:
        now = queue.popleft()

        for c in nei_L[now]:
            if ans[c] == -1:
                ans[c] = now
                queue.append(c)


bfs()

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [-1] * N
queue = deque()


def bfs():
    queue.append(0)
    while len(queue) > 0:
        now = queue.popleft()

        for c in nei_L[now]:
            if not ans[c] == -1:
                ans[c] = now
                queue.append(c)


bfs()

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

ans = [-1] * N
visited = [0] * N
queue = deque()

def bfs():
    queue.append(0)
    while len(queue) > 0:
        #if all(visited):
            #break
        now = queue.popleft()

        for c in nei_L[now]:

            if not visited[c]:
                ans[c] = now
                queue.append(c)
                visited[c] = 1


bfs()

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