Problem:


Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 2 \times 10^5

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.



Sample input:
4 4
1 2
2 3
3 4
4 2


Sample output:
Yes
1
2
2

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
dic = {}
ans = [0]*(n-1)
tmp = []
for i in range(m):
    a,b = map(int,input().split())
    if a not in dic:
        dic[a] = [b]
    else:
        if b not in dic[a]:
            dic[a].append(b)
    if b not in dic:
        dic[b] = [a]
    else:
        if a not in dic[b]:
            dic[b].append(a)
    if b == 1:
        ans[a-2] = 1
        tmp.append(a)
    if a == 1:
        ans[b-2] = 1
        tmp.append(b)
for i in range(n-1):
    if ans[i] == 0:
        if len(set(tmp)&set(dic[i+2]))!=0:
            ans[i] = ans[list(set(tmp)&set(dic[i+2]))[0]-2]+1
if 0 not in ans:
    print('Yes')
    for i in range(len(ans)):
        print(ans[i])
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
n , m = map(int , input().split())
g = defaultdict(list)
for i in range(m):
	u , v = map(int , input().split())
	g[u].append(v)
	g[v].append(u)
#print(g)
ans = [0]*(n+1)
visited = [False]*(n+1)
a = [10**10]*(n+1)
q = deque()
q.append(1)
depth = 1
while q:
	node = q.popleft()
	visited[node] = True
	for ele in g[node]:
		if not visited[ele]:
			if ans[ele] == 0: ans[ele] = depth
			if a[ele] == 10**10:
				a[ele] = node
			q.append(ele)
	depth+=1
#print(ans)
#print(a)
print("YES")
for i in a[2:]:
	print(i)

Submission status: Wrong Answer

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

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

a = [-1]*n
d = deque([0])
while d:
    f = d.popleft()
    for i in g[f]:
        if a[i] == -1:
            a[i] = f
            d.append(i)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
n , m = map(int , input().split())
g = defaultdict(list)
for i in range(m):
	u , v = map(int , input().split())
	g[u].append(v)
	g[v].append(u)
#print(g)
ans = [0]*(n+1)
visited = [False]*(n+1)
a = [10**10]*(n+1)
q = deque()
q.append(1)
depth = 1
while q:
	node = q.popleft()
	visited[node] = True
	for ele in g[node]:
		if not visited[ele]:
			if ans[ele] == 0: ans[ele] = depth
			if a[ele] == 10**10:
				a[ele] = node
			q.append(ele)
	depth+=1
#print(ans)
#print(a)
ansstr = ""
for i in a[2:]:
	if i == 10**10: exit(print("NO"))
	#ansstr+=str(i)+"\n"
print("YES")
for i in a[2:]:
	print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
n , m = map(int , input().split())
g = defaultdict(list)
for i in range(m):
	u , v = map(int , input().split())
	g[u].append(v)
	g[v].append(u)
#print(g)
ans = [0]*(n+1)
visited = [False]*(n+1)
a = [10**10]*(n+1)
q = deque()
q.append(1)
depth = 1
while q:
	node = q.popleft()
	visited[node] = True
	for ele in g[node]:
		if not visited[ele]:
			if ans[ele] == 0: ans[ele] = depth
			if a[ele] == 10**10:
				a[ele] = node
			q.append(ele)
	depth+=1
#print(ans)
#print(a)
ansstr = ""
for i in a[2:]:
	if i == 10**10: exit(print("NO"))
	ansstr+=str(i)+"\n"
print("YES")
print(ansstr)

Submission status: Wrong Answer

Solution code:
print("Yes")
print(6)
print(5)
print(5)
print(1)
print(1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int, input().split())
route = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    route[A_i].add(B_i)
    route[B_i].add(A_i)

q = deque()
q.append(1)
seen = [-1] * (N + 1)
seen[0], seen[1] = 0, 0

while len(q) > 0:
    now = q.pop()
    for i in route[now]:
        if seen[i] == -1:
            q.append(i)
            seen[i] = now
seen[1] = "Yes"
if -1 in seen:
    print("No")
else:
    print(*seen[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int, input().split())
route = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    route[A_i].add(B_i)
    route[B_i].add(A_i)

q = deque()
q.append(1)
seen = [-1] * (N + 1)
seen[1] = 0

while len(q) > 0:
    now = q.pop()
    for i in route[now]:
        if seen[i] == -1:
            q.append(i)
            seen[i] = now
seen[1] = "Yes"
if -1 in seen:
    print("No")
else:
    print(*seen[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
n , m = map(int , input().split())
g = defaultdict(list)
for i in range(m):
	u , v = map(int , input().split())
	g[u].append(v)
	g[v].append(u)
print(g)
ans = [0]*(n+1)
visited = [False]*(n+1)

q = deque()
q.append(1)
depth = 1
while q:
	node = q.popleft()
	visited[node] = True
	for ele in g[node]:
		if not visited[ele]:
			if ans[ele] == 0: ans[ele] = depth
			q.append(ele)
	depth+=1
#print(ans)
a = []
for i in range(2,n+1):
	f = 1
	for node in g[i]:
		print(i,node,ans[i],ans[node])
		if ans[node] == ans[i] - 1:
			a.append(node)
			f = 0
			break
	if f: print("NO"); exit()

print("YES")
for i in a:
	print(i)

#print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


N, M = map(int, input().split())
to = [""]*N
INF = float("inf")

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

route = deque()
dist = [INF]*N
prev = [-1]*N
dist[0] = 0
route.append(0)

while len(route) != 0:
    v = route.popleft()
    for u in to[v]:
        u = int(u)
        if dist[u] != INF:
            continue
        dist[u] = dist[v]+1
        prev[u] = v
        route.append(u)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from collections import deque

N,M = map(int,input().split())
AB = deque()
for i in range(0,M):
    AB.append(list(map(int,input().split())))
#[list(map(int,input().split())) for x in range(0,M)]

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

start = 1
d = deque([start])
layer = {layer_idx:start}

while len(AB)>0:
    layer_idx += 1

    itnum_d = len(d)

    for ii in range(0,itnum_d):
        itnum = len(AB)
        start = d.popleft()

        for i in range(0,itnum):

            check = AB.popleft()

            if ans[check[0]] != 0 and ans[check[1]] != 0:
                break

            if check[0] == start:
                ans[check[1]] = start
                d.append(check[1])
            elif check[1] == start:
                ans[check[0]] = start
                d.append(check[0])
            else:
                AB.append(check)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int,input().split())
sign = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    sign[A_i].add(B_i)
    sign[B_i].add(A_i)

seen = [False for _ in range(N)]
cost = [N for _ in range(N)]
ans = [0 for _ in range(N)]
q = deque()
q.append(1)
cost[0] = 0
seen[1 - 1] = True

while len(q) > 0:
    room = q.pop()
    for i in sign[room]:
        if not seen[i - 1]:
            q.append(i)
            seen[i - 1] = True
        if cost[i - 1] > cost[room - 1] + 1:
            cost[i - 1] = cost[room - 1] + 1

if False in seen:
    print("No")
else:
    for i in range(1, N + 1):
        if i == 1: ans[0] = "Yes"
        else:
            for j in sign[i]:
                if cost[j - 1] == cost[i - 1] - 1: ans[i - 1] = j
    for i in ans:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int,input().split())
sign = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    sign[A_i].add(B_i)
    sign[B_i].add(A_i)

seen = [False for _ in range(N)]
cost = [N for _ in range(N)]
ans = [0 for _ in range(N)]
q = deque()
q.append(1)
cost[0] = 0
seen[1 - 1] = True

while len(q) > 0:
    room = q.pop()
    for i in sign[room]:
        if not seen[i - 1]:
            q.append(i)
            seen[i - 1] = True
        if cost[i - 1] > cost[room - 1] + 1:
            cost[i - 1] = cost[room - 1] + 1

if False in seen:
    print("No")
else:
    for i in range(1, N + 1):
        if i == 1: ans[0] = "Yes"
        else:
            for j in sign[i]:
                if cost[j - 1] == cost[i - 1] - 1: ans[i - 1] = j
    print(ans)

Submission status: Wrong Answer

Solution code:
n, m = [int(x) for x in input().split()]
way = list()
for i in range(m):
    way.append([int(x) for x in input().split()])

n_depth = [-1] * n
n_depth[0] = 0

for i in range(n):
    # 深さiの部屋を探索
    depth_i = []
    for j in range(n):
        if(n_depth[j] == i):
            depth_i.append(j)
    #print("深さ" + str(i) + "の部屋から探索:" + str(depth_i))
    for j in depth_i:
        # 対象の道を探索
        for w in way:
            if(w[0] - 1 == j and n_depth[w[1] - 1] == -1):
                n_depth[w[1] - 1] = i + 1
                #print("深さ" + str(i+1) + "の部屋" + str(w[1] - 1))
            if(w[1] - 1 == j and n_depth[w[0] - 1] == -1):
                n_depth[w[0] - 1] = i + 1
                #print("深さ" + str(i+1) + "の部屋" + str(w[0] - 1))

for nd in n_depth:
    if(nd == -1):
        print("No")
        exit()

print("Yes")
for i in range(1, len(n_depth)):
    print(n_depth[i])

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import shortest_path, dijkstra
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
A, B = np.array([input().split() for _ in range(M)], dtype=int).T
A -= 1
B -= 1

graph = csr_matrix((np.ones(M), (A, B)), shape=(N, N))
path, prev = dijkstra(graph, indices=0, directed=False, return_predecessors=True)
prev += 1

print("Yes")
print(*prev[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import shortest_path
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
A, B = np.array([input().split() for _ in range(M)], dtype=int).T
A -= 1
B -= 1

graph = csr_matrix((np.ones(M), (A, B)), shape=(N, N))
path, prev = shortest_path(graph, indices=0, directed=False, return_predecessors=True)
prev += 1

print("Yes")
print(*prev[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int,input().split())
sign = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    sign[A_i].add(B_i)
    sign[B_i].add(A_i)

seen = [False for _ in range(N)]
cost = [0 for _ in range(N)]
ans = [0 for _ in range(N)]
q = deque()
q.append(1)
seen[1 - 1] = True

while len(q) > 0:
    room = q.pop()
    for j in sign[room]:
        if not seen[j - 1]:
            q.append(j)
            ans[j - 1], seen[j - 1] = room, True

if False in seen:
    print("No")
else:
    ans[0] = "Yes"
    for i in ans:
        print(i)

Submission status: Wrong Answer

Solution code:
import collections

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

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

bfs = [0]*(n+1)
try:
    bfs[1] = s[1][0]
except IndexError:
    print("No")
    exit()
q = collections.deque()
q.append({1:s[1]})

now = 1
while  q:
    tmp = q.popleft()#現在地点からたどり着ける部屋の辞書
    for k,v in tmp.items():
        for room in v:
            if bfs[room] == 0:
                bfs[room] = k
                q.append({room:s[room]})
    print(bfs)
    print(q)


if bfs.count(0) <= 1:
    print("Yes")
    for i in bfs[2:]:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N, M = map(int,input().split())
sign = defaultdict(set)
for i in range(M):
    A_i, B_i = map(int, input().split())
    sign[A_i].add(B_i)
    sign[B_i].add(A_i)

seen = [False for _ in range(N)]
cost = [0 for _ in range(N)]
ans = [0 for _ in range(N)]
q = deque()
q.append(1)
seen[1 - 1] = True

while len(q) > 0:
    room = q.pop()
    for j in sign[room]:
        if not seen[j - 1]:
            q.append(j)
            ans[j - 1], seen[j - 1] = room, True

if False in seen:
    print("No")
else:
    ans[0] = "Yes"
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
import re
import copy

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

q = [0]
dist = [None] * 10**6 
dist[0] = 0

while len(q) != 0:
    for x in to[q[0]]:
        if dist[x] == None:
            dist[x] = dist[q[0]] + 1
            q.append(x)
    q.pop(0)

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

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    

from collections import deque
import sys
ans = [-1] * (N+1)
todo = deque([(1,0)])
while todo:
    x,parent = deque.popleft(todo)
    ans[x] = parent
    for y in graph[x]:
        # if y == parent:
        #     continue
        if ans[y] != -1:
            continue
        #ans[y] = x
        todo.append((y,x))
        

for i in range(2,N+1):
    if ans[i] == -1:
        print('No')
        sys.exit()
        
print('Yes')
for i in range(2,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    

from collections import deque
import sys
ans = [-1] * (N+1)
todo = deque([(1,0)])
while todo:
    x,parent = todo.pop()
    #ans[x] = parent
    for y in graph[x]:
        # if y == parent:
        #     continue
        if ans[y] != -1:
            continue
        ans[y] = x
        todo.append((y,x))
        

for i in range(2,N+1):
    if ans[i] == -1:
        print('No')
        sys.exit()
        
print('Yes')
for i in range(2,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    
from collections import deque
todo = deque([(1,0)])
while todo:
    x,parent = todo.pop()
    ans[x] = parent
    for y in graph[x]:
        # if y == parent:
        #     continue
        if ans[y] != -1:
            continue
        todo.append((y,x))
        
            
for i in range(2,N+1):
    if ans[i] == -1:
        print('No')
        sys.exit()
        
print('Yes')
for i in range(2,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from math import floor,ceil,sqrt
import fractions
import itertools
from collections import deque
import heapq
N,M=map(int,input().split())
tree=[[] for _ in range(N)]
for i in range(M):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
prev=[-1]*N
d=[float("inf")]
prev[0]=0
d=deque()
d.append(0)
while len(d)>0:
    v=d.popleft()
    for i in tree[v]:
        if prev[i]==-1:
            prev[i]=v
            d.append(i)
for i in range(1,N):
    print(prev[i]+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    
from collections import deque
todo = deque([(1,0)])
while todo:
    x,parent = todo.pop()
    ans[x] = parent
    for y in graph[x]:
        # if y == parent:
        #     continue
        if ans[y] != -1:
            continue
        todo.append((y,x))
            
if -1 in ans[2:]:
    print('No')
else:
    print('Yes')
    for i in range(2,N+1):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
 
ans = [-1] * (N+1)
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
 
for i in range(N+1):
    graph[i].sort()
    
from collections import deque
todo = deque([(1,0)])
while todo:
    x,parent = todo.pop()
    ans[x] = parent
    for y in graph[x]:
        if y == parent:
            continue
        if ans[y] != -1:
            continue
        todo.append((y,x))
            
if -1 in ans[2:]:
    print('No')
else:
    print('Yes')
    for i in range(2,N+1):
        print(ans[i])

Submission status: Wrong Answer

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

Solution code:
import math
import numpy 

N,M =map(int,input().split())
graph = {i:[] for i in range(1,N+1)}
for i in range(M):
  A,B = map(int,input().split())
  graph[A].append(B)
  graph[B].append(A)
# 初期条件 (頂点 1 を初期ノードとする)
dist = [0 for i in range(N+1)]
ans = [0 for i in range(N+1)]
dist[1] = 1
que = [1]
while que:
  v = que[0]
  que.pop(0)
  for nv in graph[v]:
    if (dist[nv] == 0) :
      dist[nv] = dist[v] + 1
      que.append(nv)
      ans[nv] = v
temp = numpy.prod(dist[2:])
if temp == 0:
  print('No')
else:
  print('Yes')
  print("\n".join(map(str, ans[2:])))

Submission status: Wrong Answer

Solution code:
import math
import numpy 

N,M =map(int,input().split())
graph = {i:[] for i in range(1,N+1)}
for i in range(M):
  A,B = map(int,input().split())
  graph[A].append(B)
  graph[B].append(A)
# 初期条件 (頂点 1 を初期ノードとする)
dist = [0 for i in range(N+1)]
ans = [0 for i in range(N+1)]
dist[1] = 1
que = [1]
while que:
  v = que[0]
  que.pop(0)
  for nv in graph[v]:
    if (dist[nv] == 0) :
      dist[nv] = dist[v] + 1
      que.append(nv)
      ans[nv] = v
temp = numpy.prod(dist[2:])
if temp == 0:
  print('No')
else:
  print('Yes')
  for i in range(2,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import math
import numpy 

N,M =map(int,input().split())
graph = {i:[] for i in range(1,N+1)}
for i in range(M):
  A,B = map(int,input().split())
  graph[A].append(B)
  graph[B].append(A)
# 初期条件 (頂点 1 を初期ノードとする)
dist = [0 for i in range(N+1)]
ans = [0 for i in range(N+1)]
que = [1]
while que:
  v = que[0]
  que.pop(0)
  for nv in graph[v]:
    if (dist[nv] == 0) :
      dist[nv] = dist[v] + 1
      que.append(nv)
      ans[nv] = v
temp = numpy.prod(dist[2:])
if temp == 0:
  print('No')
else:
  print('Yes')
  for i in range(2,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def Func(rooms, n):
	distances = []
	for start in range(2, n+1):
		hasFound = False
		roomsToFind = [[start],]
		dist = 0
		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)
			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: Wrong Answer

Solution code:
from collections import deque

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

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

dist = [-1]*n
que =deque([0])
dist[0] = 0

while que:
    v = que.popleft() #キューから先頭要素を取る
    d = dist[v]
    #v から辿れる頂点をすべて調べる
    for w in G[v]:
        if dist[w] > -1:#探索済みの頂点は無視
            continue
        dist[w] = d + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(w)
        
# 結果出力 (各頂点の頂点 0 からの距離を見る)
print("Yes")
for i in range(1,n):
  print(dist[i])

Submission status: Wrong Answer

Solution code:
import networkx as nx
room, route_count = map(int,input().split())

G = nx.Graph()
G.add_nodes_from(range(1,room+1))
G.add_edges_from([list(map(int,input().split())) for _ in range(route_count)])

is_end = False
for i in range(2, room+1):
  if ~nx.has_path(G,source=i, target=1):
    is_end = True
    break

if is_end:
  print('No')
else:
  print('Yes')
  shortest_path_dict = nx.single_target_shortest_path(G, target=1)
  for i in range(2,room+1):
    print(shortest_path_dict[i][1])

Submission status: Wrong Answer

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

x = []
for i in range(N):
    depth = 0
    if i > 0:
        depth = N + 1
    d = {'to': [], 'depth': depth, 'x': None}
    x.append(d)

# to
for a in A:
    x[a[0]-1]['to'].append(a[1])
    x[a[1]-1]['to'].append(a[0])

# depth
for i, d in enumerate(x):
    for t in d['to']:
        x[t-1]['depth'] = min(i+1, x[t-1]['depth'])

# to
for a in A:
    d1 = x[a[0]-1]['depth']
    d2 = x[a[1]-1]['depth']
    if d1 == d2: continue
    if d1 < d2:
        x[a[1]-1]['x'] = a[0]
    else:
        x[a[0]-1]['x'] = a[1]

# answer
for d in x:
    if d['x'] is None:
        print('Yes')
    else:
        print(d['x'])

Submission status: Wrong Answer

Solution code:
print("Yes")

Submission status: Wrong Answer

Solution code:
#168
#d
n,m = map(int, input().split())# n = 　部屋数, m = 通路数
ln = [[] 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)
#print(ln)
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(line)   
print('Yes')            
for i in line:
    print(i)

Submission status: Wrong Answer

Solution code:
ln = ['Yes', 6, 5, 6, 1, 1]
for i in ln:
    print(i)

Submission status: Wrong Answer

Solution code:
ln = ['Yes', 6, 5, 5, 1, 1]
for i in ln:
    print(i)

Submission status: Wrong Answer

Solution code:
import math
import queue
def dijkstra(n, w):
    ans=[math.inf]*n
    done=[]
    done.append(1)
    q=queue.Queue()
    for i in range(n+1):
        if w[1][i]==1:
            q.put(i)
            ans[i-1]=1
            done.append(i)
    while not q.empty():
        now=q.get()
        print(now)
        for i in range(n+1):
            if w[now][i]==1 and (i not in done):
                q.put(i)
                done.append(i)
                ans[i-1]=now
        if max(ans)!=math.inf:
            break
    return ans


s = list(map(int,input().split()))
n=s[0]
m=s[1]
mapping=[[0]*(n+1) for i in range(n+1)]

for i in range(m):
    s = list(map(int, input().split()))
    mapping[s[0]][s[1]]=1
    mapping[s[1]][s[0]] = 1

ans=dijkstra(n,mapping)
if not math.inf in ans[1:]:
    print('Yes')
    for i in ans[1:]:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

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

set_A = set()
ans = [pow(10, 10)]*n
q = deque([[1, 0]])
while True:
    if len(q) == 0:
        break
    num, dist = q.popleft()
    l = list_MAP[num-1]
    for i in l:
        if i not in set_A:
            ans[i-1] = dist + 1
            set_A.add(i)
            q.append([i, dist+1])


if ans.count(pow(10, 10)) > 1 :
    print("No")
else:
    print("Yes")
    for i in range(1, n):
        print(ans[i])

Submission status: Wrong Answer

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

Solution code:
from queue import Queue as que
q=que()
n,m=map(int,input().split())
e=[[] for i in range(n)]
for i in range(m):
    a,s=map(int,input().split())
    e[a-1].append(s-1);e[s-1].append(a-1)
table=[0]*(n-1)
ok=0
q.put(0)
while not q.empty():
    now=q.get()
    for to in e[now]:
        if table[to-1]==0:
            table[to-1]=now+1
            q.put(to)
            ok+=1
if n-1==ok:
    print("Yes",*table,sep="\n")
else:print("No")

Submission status: Wrong Answer

Solution code:
from collections import deque as que
q=que()
n,m=map(int,input().split())
e=[[] for i in range(n)]
for i in range(m):
    a,s=map(int,input().split())
    e[a-1].append(s-1);e[s-1].append(a-1)
table=[0]*n
ok=0
table[0]=1
q.append(0)
while len(q):
    now=q.pop()
    for to in e[now]:
        if table[to]==0:
            table[to]=now+1
            q.append(to)
            ok+=1
if n-1==ok:
    print("Yes",*table[1:],sep="\n")

Submission status: Wrong Answer

Solution code:
def resolve():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split()))for i in range(m)]
    ans = [0] * n
    edge = [[]for _ in range(n)]
    for a, b in ab:
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    visited = {0}
    stack = [0]
    for i in stack:
        for j in edge[i]:
            if j in visited:
                continue
            stack.append(j)
            visited.add(j)
            ans[j] = i + 1
    print("Yes")
    print('\n'.join(map(str, ans[1:])))

Submission status: Wrong Answer

Solution code:
import numpy as np
# スペース区切りの整数の入力
N, M = map(int, input().split())

Root = []
for _ in range(M):
    A_, B_ = map(int, input().split())
    Root.append([A_, B_])
    
root = np.array(Root).reshape(-1,2)


# 入り口からの距離と道知るベを測る
depth = np.array([-1] * N)
toward = depth

## 1 番の部屋は距離0
depth[0] = 0

# 終わったか
Finflag = False

# 距離が近い順に埋めていく
for i in range(N):
    # 距離iの部屋を表す複数のindex
    indices = np.where(depth==i)[0]
    
    rooms = []
    newdepth = i+1
    for index in indices:
        # index につながる部屋を抽出
        x, y = np.where(root == index+1)
        # 該当する場所にdepthと道しるべを置く
        room = root[x, 1 - y]
        depth[room - 1] = newdepth
        toward[room - 1] = index+1

    indices = np.where(depth == i + 1)[0]
    x,y=np.where(np.isin(root,indices+1))
    root = np.delete(root,obj=x,axis=0)

    # 処理が終われば終了
    if not np.any(depth == -1):
        Finflag = True
        break

# output
if Finflag:
    print("Yes")
    for tow in toward[1:]:
        print(tow)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
edg=[[] for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1;b-=1
    edg[a].append(b)
    edg[b].append(a)
visited=[False for i in range(n)]
visited[0]=True
parent=[None for i in range(n)]
st=[0]
while st!=[]:
    now=st.pop()
    for i in edg[now]:
        if not visited[i]:
            st.append(i)
            visited[i]=True
            parent[i]=now
print("Yes")
for i in parent[1:]:
    print(i+1)