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:
N, M = map(int, input().split())
A = []
B = []
pt = [[] for i in range(M)]
for i in range(M):
	a, b =  map(int, input().split())
	a -=1
	b -=1
	pt[a].append(b)
	pt[b].append(a)
n=M
res_list = [-1] * N
def bfs(v):
    d=[-1]*n
    d[v]=0
    q=[v]
    c=1
    while q:
        q1=[]
        for i in q:
            for j in pt[i]:
                if d[j]==-1:
                    d[j]=c
                    q1.append(j)
                    res_list[j]=i
        q=q1
        c+=1
    return d
res = bfs(0)

no = 'No'
yes ='Yes'
if -1 in res:
	print(no)
else:
	print(yes)
	
for i,x in enumerate(res_list):
	if i==0:
		continue
	print(x+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A = []
B = []
pt = [[] for i in range(M)]
#print(pt)
for i in range(M):
	a, b =  map(int, input().split())
	a -=1
	b -=1
	#A.append(a)
	#B.append(b)
	pt[a].append(b)
	pt[b].append(a)
#	print(pt)
#print(A)
#print(B)
print(pt)
n=M
#n=7
#pt=[[1, 2, 3], [0], [5, 0], [6, 0], [6], [2], [3, 4]]
res_list = [-1] * n
def bfs(v):
    d=[-1]*n
    d[v]=0
    q=[v]
    c=1
    while q:
        q1=[]
        for i in q:
            for j in pt[i]:
                if d[j]==-1:
                    d[j]=c
                    q1.append(j)
                    res_list[j]=i
        q=q1
        c+=1
#        print(d,q)
    return d
res = bfs(0)

no = 'No'
yes ='Yes'
if -1 in res:
	print(no)
else:
	print(yes)
	
for i,x in enumerate(res_list):
	if i==0 or x==0:
		continue
	print(x+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A = []
B = []
pt = [[] for i in range(M)]
#print(pt)
for i in range(M):
	a, b =  map(int, input().split())
	a -=1
	b -=1
	#A.append(a)
	#B.append(b)
	pt[a].append(b)
	pt[b].append(a)
#	print(pt)
#print(A)
#print(B)
print(pt)
n=M
#n=7
#pt=[[1, 2, 3], [0], [5, 0], [6, 0], [6], [2], [3, 4]]
res_list = [-1] * n
def bfs(v):
    d=[-1]*n
    d[v]=0
    q=[v]
    c=1
    while q:
        q1=[]
        for i in q:
            for j in pt[i]:
                if d[j]==-1:
                    d[j]=c
                    q1.append(j)
                    res_list[j]=i
        q=q1
        c+=1
#        print(d,q)
    return d
res = bfs(0)

no = 'No'
yes ='Yes'
if -1 in res:
	print(no)
else:
	print(yes)
	
for i,x in enumerate(res_list):
	if i==0:
		continue
	print(x+1)

Submission status: Runtime Error

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

adj = [[] for _ in range(m)]

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

from collections import deque
q = deque()

dist = [-1] * n
ans = [0] * n

q.append(0)
while len(q) != 0:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1:continue
        dist[to] = dist[now] + 1
        q.append(to)
        ans[to] = now 
    
print("Yes")
for i in range(1, n):
    print(ans[i] + 1)

Submission status: Runtime Error

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

adj = [[] for _ in range(m)]

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

from collections import deque
q = deque()

dist = [-1]*n
ans = [0]*n

q.append(0)
while len(q) != 0:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        dist[to] = dist[now] + 1
        q.append(to)
        ans[to] = now 
    
print("Yes")
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

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

adj = [[] for _ in range(m)]

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

from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while len(q) != 0:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        dist[to] = dist[now] + 1
        q.append(to)
        ans[to] = now 
    
print("Yes")
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)
# print(adj)
from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        dist[to] = dist[now] + 1
        q.append(to)
        ans[to] = now 
    # print(adj[now], dist, ans, q)
    
print("Yes")
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)
# print(adj)
from collections import deque
q = deque()
dist = [-1]*n
ans = [0]*n
q.append(0)
while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        ans[to] = now 
        dist[to] = dist[now] + 1
        q.append(to)
    # print(adj[now], dist, ans, q)
    
print("Yes")
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)

from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        ans[to] = now 
        dist[to] = dist[now] + 1
        q.append(to)

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

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)
# print(adj)
from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        ans[to] = now 
        dist[to] = dist[now] + 1
        q.append(to)
    # print(adj[now], dist, ans, q)
    
print('Yes')
for i in range(1,n):
    print(ans[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)
# print(adj)
from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        # ans[to] = now 
        dist[to] = dist[now] + 1
        q.append(to)
    # print(adj[now], dist, ans, q)
    
print('Yes')
for i in range(1,n):
    print(dist[i]+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
adj = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    adj[a].append(b)
    adj[b].append(a)
# print(adj)
from collections import deque
q = deque([0])
dist = [-1]*n
ans = [0]*n

while q:
    now = q.popleft()
    for to in adj[now]:
        if dist[to] != -1: continue
        # ans[to] = now 
        dist[to] = dist[now] + 1
        q.append(to)
    # print(adj[now], dist, ans, q)
    
print('Yes')
for i in range(1,n):
    print(dist[i]+1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A, B = [], []
dic = {i:[] for i in range(1, N+1)}
for _ in range(M):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)

node = [float('inf')] * N
node[0] = 0 #1までの距離
route = [0]*N #ランプ置く部屋
rooms = [1] #次調べる部屋
st = (1)

for r1 in rooms:
    for r2 in dic[r1]:
        if node[r1-1] + 1 <= node[r2-1]:
            node[r2-1] = node[r1-1] + 1
            route[r2-1] = r1
            if not r2 in st:
                rooms.append(r2)
                st.add(r2)
            try:
                dic[r2].remove(r1)
            except:
                continue

if float('inf') in node:
    print('No')
else:
    print('Yes')
    for i in route[1:]:
        print(i)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A, B = [], []
dic = {i:[] for i in range(1, N+1)}
for _ in range(M):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)

node = [float('inf')] * N
node[0] = 0 #1までの距離
route = [0]*N #ランプ置く部屋
rooms = [1] #次調べる部屋

for r1 in rooms:
    for r2 in dic[r1]:
        if node[r1-1] + 1 <= node[r2-1]:
            node[r2-1] = node[r1-1] + 1
            route[r2-1] = r1
            rooms.append(r2)
            dic[r2].remove(r1)

if float('inf') in node:
    print('No')
else:
    print('Yes')
    for i in route[1:]:
        print(i)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M = map(int,input().split())
vector = np.zeros((N, N))

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

depth = np.zeros((1, N))
que = [0]

while not que == []:
	room = que[0]
	que.pop(0)
	search = np.where(vector[room:room+1]==1)[1]
	for next_room in search:
		if not depth[:,next_room:next_room+1] == 0 or next_room == 0:
			continue
		depth[:,next_room:next_room+1] = depth[:,room:room+1]+1
		que.append(next_room)

if 0 in depth[:,1:][0]:
	print("No")
else:
	print("Yes")
	for dep in depth[:,1:][0]:
		print(np.where(depth==dep-1)[1][0]+1)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M = map(int,input().split())
inputs.pop(0)
vector = np.zeros((N, N))

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

depth = np.zeros((1, N))
que = [0]

while not que == []:
	room = que[0]
	que.pop(0)
	search = np.where(vector[room:room+1]==1)[1]
	for next_room in search:
		if not depth[:,next_room:next_room+1] == 0 or next_room == 0:
			continue
		depth[:,next_room:next_room+1] = depth[:,room:room+1]+1
		que.append(next_room)

if 0 in depth[:,1:][0]:
	print("No")
else:
	print("Yes")
	for dep in depth[:,1:][0]:
		print(np.where(depth==dep-1)[1][0]+1)

Submission status: Runtime Error

Solution code:
from collections import deque

n,m = map(int, input().split())
dic = {}

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

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

for a,b in AB:
    a -= 1
    b -= 1

    dic[a].append(b)
    dic[b].append(a)

#dist = {}
queue = deque([0])
visited = []
pre = {}

dist[0] = 0 
#queue.append(0)

while queue:
    v = queue.popleft()
    
    for i in dic[v]:
        if i in visited:
            continue
        
        visited.append(i)
        #dist[i] = dist[v] + 1
        pre[i]= v
        queue.append(i)

print("Yes")
for i in range(n):
    ans = 0
    
    if(i == 0):
        continue
    
    ans = pre[i]
    ans += 1
    print(ans)

Submission status: Runtime Error

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

for i in range(M):
    x, y = map(int, input().split())
    G[x].append(y)
    G[y].append(x)

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

Q = deque()
Q.append(1)

while Q:
    x = Q.pop()
    for t in G[x]:
        if USED[t] == -1:
            Q.appendleft(t)
            USED[t] = x

if -1 in USED:
    print("No")
else:
    print("Yes")
    print(*USE[2:])

Submission status: Runtime Error

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

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

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

for a,b in AB:
    a -= 1
    b -= 1

    links[a].append(b)
    links[b].append(a)

dist = {}
queue = []
visited = []
pre = {}

dist[0] = 0 
queue.append(0)

while len(queue) > 0:
    v = queue.pop(0)
    
    for i in dic[v]:
        if i in visited:
            continue
        
        visited.append(i)
        dist[i] = dist[v] + 1
        pre[i]= v
        queue.append(i)


print("Yes")
for i in range(n):
    ans = 0
    
    if(i == 0):
        continue
    
    ans = pre[i]
    ans += 1
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


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

dct = [0]*(N+1)
for _ in range(M):
    a, b = map(int,input().split())
    dct[a].add(b)
    dct[b].add(a)

#print(dct)
answers = deque([0]*(N+1))

stack = deque([1])
lst = set()
while len(stack):
    s = stack.popleft()
    lst.add(s)
    #print(dct[s])
    for i in dct[s]:
        if i in lst:
            continue
        if i in stack:
            continue
        stack.append(i)
        answers[i] = s

print("Yes")
answers.popleft()
answers.popleft()
for ans in answers:
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


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

dct = [0]*(N+1)
for _ in range(M):
    a, b = map(int,input().split())
    dct[a].add(b)
    dct[b].add(a)

#print(dct)
answers = deque([0]*(N+1))

stack = deque([1])
lst = []
while len(stack):
    s = stack.popleft()
    lst.append(s)
    #print(dct[s])
    for i in dct[s]:
        if i in lst:
            continue
        if i in stack:
            continue
        stack.append(i)
        answers[i] = s

print("Yes")
answers.popleft()
answers.popleft()
for ans in answers:
    print(ans)

Submission status: Runtime Error

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

head = input().split()

N = int(head[0])
M = int(head[1])
inputs = [input() for i in range(M)]

vector = np.zeros((N, N))
for i in range(0, M):
	param = inputs[i].split()
	A = int(param[0])
	B = int(param[1])
	vector[A-1:A,B-1:B] = 1
	vector[B-1:B,A-1:A] = 1

depth = np.zeros((1, N))
que = [0]

try:
  while not que == []:
	room = que[0]
	que.pop(0)
	search = np.where(vector[room:room+1]==1)[1]
	for next_room in search:
		if not depth[:,next_room:next_room+1] == 0 or next_room == 0:
			continue
		depth[:,next_room:next_room+1] = depth[:,room:room+1]+1
		que.append(next_room)
except Exception as e:
  print("No")
  return 0

if 0 in depth[:,1:][0]:
	print("No")
else:
	print("Yes")
	for dep in depth[:,1:][0]:
		print(np.where(depth==dep-1)[1][0]+1)

Submission status: Runtime Error

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

head = input().split()

N = int(head[0])
M = int(head[1])
inputs = [input() for i in range(M)]

vector = np.zeros((N, N))
for i in range(0, M):
	param = inputs[i].split()
	A = int(param[0])
	B = int(param[1])
	vector[A-1:A,B-1:B] = 1
	vector[B-1:B,A-1:A] = 1

depth = np.zeros((1, N))
que = [0]

while not que == []:
	room = que[0]
	que.pop(0)
	search = np.where(vector[room:room+1]==1)[1]
	for next_room in search:
		if not depth[:,next_room:next_room+1] == 0 or next_room == 0:
			continue
		depth[:,next_room:next_room+1] = depth[:,room:room+1]+1
		que.append(next_room)

if 0 in depth[:,1:][0]:
	print("No")
else:
	print("Yes")
	for dep in depth[:,1:][0]:
		print(np.where(depth==dep-1)[1][0]+1)

Submission status: Runtime Error

Solution code:
from collections import deque

d = deque()

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

dct = {}
for _ in range(M):
    a, b = map(int,input().split())
    if not a in dct:
        dct[a] = set()
    if not b in dct:
        dct[b] = set()
    dct[a].add(b)
    dct[b].add(a)

#print(dct)
answers = [0]*(N+1)

stack = deque(1)
lst = []
while len(stack):
    s = stack.popleft()
    lst.append(s)
    #print(dct[s])
    for i in dct[s]:
        if i in lst:
            continue
        if i in stack:
            continue
        stack.append(i)
        answers[i] = s

if answers.count(0) != 2:
    print("No")
else:
    print("Yes")
    for ans in answers[2:]:
        print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

INF = 2 * m

dists = [INF for i in range(n)]

def bfs(cur, pre, dist):
    global dists
    stack = deque([[cur, pre, dist]])
    while stack:
        cur, pre, dist = stack.popleft()
        if dist < dists[cur]:
            dists[cur] = dist
            for e in E[cur]:
                if e != pre:
                    stack.append([e, cur, dist + 1])
bfs(0, -1, 0)

print('Yes')

for i in range(1,n):
    di = dists[i]
    for e in E[i]:
        if di - 1 == dists[e]:
            print(e+1)
            break

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

INF = 2 * m

dists = [INF for i in range(n)]

def bfs(cur, pre, dist):
    stack = deque([[cur, pre, dist]])
    while stack:
        cur, pre, dist = stack.popleft()
        if dist < dists[cur]:
            dists[cur] = dist
            for e in E[cur]:
                if e != pre:
                    stack.append([e, cur, dist + 1])
bfs(0, -1, 0)

print('Yes')

# for i in range(1,n):
#     di = dists[i]
#     for e in E[i]:
#         if di - 1 == dists[e]:
#             print(e+1)
#             break

Submission status: Runtime Error

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

head = input().split()

N = int(head[0])
M = int(head[1])
inputs = [input() for i in range(M)]

vector = np.zeros((N, N))
for i in range(0, M):
	param = inputs[i].split()
	A = int(param[0])
	B = int(param[1])
	vector[A-1:A,B-1:B] = 1
	vector[B-1:B,A-1:A] = 1

depth = np.zeros((1, N))
que = [0]

while not que == []:
	room = que[0]
	que.pop(0)
	search = np.where(vector[room:room+1]==1)[1]
	for next_room in search:
		if not depth[:,next_room:next_room+1] == 0 or next_room == 0:
			continue
		depth[:,next_room:next_room+1] = depth[:,room:room+1]+1
		que.append(next_room)

print("Yes")
for dep in depth[:,1:][0]:
	print(np.where(depth==dep-1)[1][0]+1)

Submission status: Runtime Error

Solution code:
import numpy as np

N,M = map(int, input().split())
root = []
for i in range(M):
  tmp = list(map(int, input().split()))
  root.append(tmp)

visited = np.zeros(N)
Q = []
visited[0] = 1		# 訪問済み
Q.append(1)			# 探索する要素を追加
while 1:
  q = Q.pop(0)
  for i in range(N):
    if q in root[i] and visited[root[i][0]-1] == 0 or visited[root[i][1]-1] == 0:
      if root[i][0] == q:	
        visited[root[i][1]-1] = q
        Q.append(root[i][1])
      elif root[i][1] == q:	
        visited[root[i][0]-1] = q
        Q.append(root[i][0])
  #print(Q)
  if len(Q) == 0:
    break
if sum(visited) != 1:
  print('Yes')
  for i in range(1,N,1):
    print(int(visited[i]))
else:
  print('No')

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

INF = 2 * m

dists = [INF for i in range(n)]

def bfs(cur, pre, dist):
    stack = deque([[cur, pre, dist]])
    while stack:
        cur, pre, dist = stack.popleft()
        if dist < dists[cur]:
            dists[cur] = dist
            for e in E[cur]:
                if e != pre:
                    stack.append([e, cur, dist + 1])
bfs(0, -1, 0)

print('Yes')

for i in range(1,n):
    di = dists[i]
    for e in E[i]:
        if di - 1 == dists[e]:
            print(e+1)
            break

Submission status: Runtime Error

Solution code:
from collections import deque


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

q = deque([1])
prev=[1]
siru = [0] * (n-1)
while q:

    now = q.popleft()
    
    next = graph[now-1]

    
    for n in next:
        if not n in prev: 
            q.append(n)
            prev.append(n)
            siru[n-2] = now
    if not 0 in siru:
        break



print("Yes")
print("\n".join(map(str, signposts[2:])))

Submission status: Runtime Error

Solution code:
n,m = map(int, input().split())
dic = {}
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    
    if a in dic.keys():
        tmp = dic[a]
        tmp.append(b)
        dic[a] = tmp
    else:
        tmp = [b]
        dic[a] = tmp
    
    if b in dic:
        tmp = dic[b]
        tmp.append(a)
        dic[b] = tmp
    else:
        tmp = [a]
        dic[b] = tmp

dist = {}
queue = []
visited = []
pre = {}

dist[0] = 0 
queue.append(0)

while len(queue) > 0:
    v = queue.pop(0)
    
    for i in dic[v]:
        if i in visited:
            continue
        
        visited.append(i)
        dist[i] = dist[v] + 1
        pre[i]= v
        queue.append(i)


print("Yes")
for i in range(m):
    ans = 0
    
    if(i == 0):
        continue
    
    ans = pre[i]
    ans += 1
    print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import math
import fractions
import functools
import copy
from collections import deque

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

ans = [-1] * n
d = deque([0])

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

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

Submission status: Runtime Error

Solution code:
# from collections import deque
# from collections import defaultdict
import itertools
# import math
# import numpy as np
# from scipy.special import comb
# import heapq


def main():
    N, W = map(int, input().split())
    v = [0] * (N + 1)
    w = [0] * (N + 1)

    for i in range(1, N + 1):
        v[i], w[i] = map(int, input().split())

    values = [[0 for _ in range(W + 1)] for _ in range(N + 1)]

    for i in range(0, N + 1):
        for j in range(0, W + 1):
            non_choice = values[i - 1][j]
            if j >= w[i]:
                choice = values[i - 1][j - w[i]] + v[i]
            else:
                choice = 0
            values[i][j] = max(choice, non_choice)

    print(max(list(itertools.chain(*values))))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# from collections import deque
# from collections import defaultdict
# import itertools
# import math
import numpy as np
# from scipy.special import comb
# import heapq


def main():
    N, W = map(int, input().split())
    v = [0] * (N + 1)
    w = [0] * (N + 1)

    for i in range(1, N + 1):
        v[i], w[i] = map(int, input().split())

    values = [[0 for _ in range(W + 1)] for _ in range(N + 1)]

    for i in range(0, N + 1):
        for j in range(0, W + 1):
            non_choice = values[i - 1][j]
            if j >= w[i]:
                choice = values[i - 1][j - w[i]] + v[i]
            else:
                choice = 0
            values[i][j] = max(choice, non_choice)

    print(np.max(np.array(values)))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
#AB=[list(map(int,input().split()) for _ n range(N))]
p=[[] for _ in range(N+1)]
for _ in range(N):
    a,b=map(int,input().split())
    p[a].append(b)
    p[b].append(a)

sirube=[-1]*N
visited=[False]*N
def bfs():
    global visited,sirube
    queue=[1]
    while queue:
        now=queue.pop(0)
        for n in p[now]:
            if not visited[n-1]:
                queue.append(n)
                visited[n-1]=True
                sirube[n-1]=now
    return
bfs()
if -1 in sirube[1:]:
    print("No")
    exit()

print("Yes")
for sir in sirube[1:]:
    print(sir)

Submission status: Runtime Error

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

if __name__ == '__main__':
    function()

Submission status: Runtime Error

Solution code:
import math
import collections
from collections import defaultdict

N = int(input())
iwa = []
mod = 10**9+7
cnt = 0
for i in range(N):
    a, b = map(int,input().split())
    if a == 0 and b == 0:
        cnt = 1
        continue
    elif b == 0:
        iwa.append((1,0))
    elif a == 0:
        iwa.append((0,1))
    elif b > 0:
        c = math.gcd(a,b)
        iwa.append((a/c, b/c))#bは正
    else:
        c = math.gcd(a,b)
        iwa.append((-a/c, -b/c))#bは正
        
S = set(iwa)
I = collections.Counter(iwa)
#print(I)
jdg = defaultdict(int)

ans = 1
for i in S:
    if jdg[i] == 1:
        continue
    elif i == (1,0) or i == (0,1):
        k = I[(1,0)]
        l = I[(0,1)]
        if min(k,l) == 0:
            ans *= pow(2,max(l,k),mod)
        else:
            ans *= (pow(2,l,mod)-1) + (pow(2,k,mod)-1)+1
        jdg[(1,0)] = 1
        jdg[(0,1)] = 1
    else:
        k = I[i]
        a, b = i
        if a < 0:
            l = I[(b,-a)]
            jdg[(b, -a)] = 1
        else:
            l = I[(-b,a)]
            jdg[(-b,a)] = 1
        if min(l,k) == 0:
            ans *= pow(2,max(l,k),mod)
        else:
            ans *= (pow(2,l,mod)-1) + (pow(2,k,mod)-1)+1
        jdg[i] = 1
print(ans+cnt-1)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

q = deque([0])
ans = [-1] * n
count = 0

while len(q):
    p = q.popleft()
    for x in u[p]:
        if ans[x] == -1:
            ans[x] = p+1
            q.append(x)
    print(q)

ans[0] = 'Yes'

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

Submission status: Runtime Error

Solution code:
from collections import deque

N, M = map(int, input().split()) # ノード数, エッジ数
edges = [[] for i in range(M)] # エッジ
for i in range(M):
    A, B = map(int, input().split())
    edges[A - 1] += [B - 1]
    edges[B - 1] += [A - 1]

que = deque([0]) # 探索用キュー
ans = [0] + [-1 for i in range(N - 1)] # 未探索ノード 兼 道しるべ

while -1 in ans: # 未探索ノードがある間
    node = que.popleft() # キューから取り出し
    for neighbor in edges[node]: # 全ての隣接ノードに対して
        if ans[neighbor] == -1: # 未探索なら
            ans[neighbor] = node + 1 # 道しるべを設定
            que.append(neighbor) # キューに入れる

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

Submission status: Runtime Error

Solution code:
from collections import deque
N,Q = (int(x) for x in input().split())
connection ={i:[] for i in range(1,N+1)}
#木の構造
for i in range(1,N):
    a,b = (int(x) for x in input().split())
    connection[a].append(b)
    connection[b].append(a)

#各頂点のポイント入力
point ={i:0 for i in range(1,N+1)}
for i in range(1,Q+1):
    p,x = (int(x) for x in input().split())
    point[p] =x

#深さ優先探索
dq =deque()
dq.append(1)
passed ={i:0 for i in range(1,N+1)} #各頂点を通過チェック、０は未通過
while dq:
    now =dq.pop()
    passed[now] =1
    for next in connection[now]:
        if passed[next] ==1:
            continue
        point[next] +=point[now]
        dq.append(next)
        #print(next,end=" ")
print(*point.values())

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
d = {}
ans = [0] * (n)
for _ in range(m):
    a, b = map(int, input().split())
    if not a in d.keys():
        d[a] = [b]
    elif not b in d[a]:
        d[a].append(b)
    if not b in d.keys():
        d[b] = [a]
    elif not a in d[b]:
        d[b].append(a)

q = deque([1])
while not len(q) == 0:
    x = q.popleft()
    for i in d[x]:
        if ans[i-1] == 0:
            ans[i-1] = x
            q.append(i)

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

Submission status: Runtime Error

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

if __name__ == '__main__':
    function()