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:
from collections import deque
N, M = map(int, input().split())
route = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  route[A-1].append(B-1)
  route[B-1].append(A-1)

tmp = 0
prv = 0
visited = [False]*N
kouho = deque()
ans = [-1]*(N)

for room in route[0]:
  kouho.append([room,0])
while kouho:
  visited[tmp] = True
  ans[tmp] = prv
  for room in route[tmp]:
    if not visited[room]:
      kouho.append([room,tmp])
  while kouho:
    tmp_ = kouho.popleft()
    tmp = tmp_[0]
    prv = tmp_[1]
    if not visited[tmp]:
      break
if -1 not in ans:
  print("Yes")
  for n in range(1,N):
    print(ans[n]+1)
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
route = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  route[A-1].append(B-1)
  route[B-1].append(A-1)

tmp = 0
prv = 0
visited = [False]*N
kouho = deque()
ans = [-1]*(N)

for room in route[0]:
  kouho.append([room,0])
while kouho:
  visited[tmp] = True
  ans[tmp] = prv
  for room in route[tmp]:
    if not visited[room]:
      kouho.append([room,tmp])
  while kouho:
    tmp_ = kouho.popleft()
    tmp = tmp_[0]
    prv = tmp_[1]
    if not visited[tmp]:
      break
print("Yes")
for n in range(1,N):
  print(ans[n]+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
route = [[] for _ in range(N)]
for _ in range(M):
  A, B = map(int, input().split())
  route[A-1].append(B-1)
  route[B-1].append(A-1)

tmp = 0
prv = 0
visited = [False]*N
kouho = deque()
ans = [-1]*(N)

for room in route[0]:
  kouho.append([room,0])
while kouho:
  visited[tmp] = True
  ans[tmp] = prv
  for room in route[tmp]:
    if not visited[room]:
      kouho.append([room,tmp])
  while kouho:
    tmp_ = kouho.popleft()
    tmp = tmp_[0]
    prv = tmp_[1]
    if not visited[tmp]:
      break
print("Yes")
for n in range(1,N):
  print(ans[n])

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M = na()
edge = np.array([na() + [1] for _ in range(M)], dtype=np.int64).T
g = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
d, root = dijkstra(g, directed=False, return_predecessors=True, indices=[0])
if -9999 not in d[0][1:]:
    print('Yes')
    for i in range(1, N):
        print(root[0][i] + 1)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M = na()
edge = np.array([na() + [1] for _ in range(M)], dtype=np.int64).T
g = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
d, root = dijkstra(g, directed=False, return_predecessors=True, indices=[0])
print(root)
if -9999 not in d[0][1:]:
    print('Yes')
    for i in range(1, N):
        print(root[0][i] + 1)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M = na()
edge = np.array([na() + [1] for _ in range(M)], dtype=np.int64).T
g = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
d, root = dijkstra(g, directed=False, return_predecessors=True, indices=[0])
if 0 not in d[0][1:]:
    print('Yes')
    for i in range(1, N):
        print(root[0][i] + 1)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M = na()
edge = np.array([na() + [1] for _ in range(M)], dtype=np.int64).T
g = csr_matrix((edge[2], (edge[:2] - 1)), (N, N))
d, root = shortest_path(g, directed=False, return_predecessors=True, indices=[0])
if 0 not in d[0][1:]:
    print('Yes')
    for i in range(1, N):
        print(root[0][i] + 1)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

n, m = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()]for j in range(m)]    # nは行数

tmp = [inf for i in range(n + 1)]
res = "No"

tmp[1] = 0
cnt = 0
A = deque(A)
while A:
    a, b = A.popleft()
    if tmp[a] == inf and tmp[b] == inf:
        A.append([a, b])
        if cnt > len(A):
            break
        cnt += 1
    elif tmp[a] == inf:
        tmp[a] = tmp[b] + 1
        cnt = 0
    elif tmp[b] == inf:
        tmp[b] = tmp[a] + 1
        cnt = 0
    elif tmp[b] <= tmp[a]:
        tmp[a] = min(tmp[a], tmp[b] + 1)
        cnt = 0
    elif tmp[a] <= tmp[b]:
        tmp[b] = min(tmp[b], tmp[a] + 1)
        cnt = 0
    print(tmp)
if sum(tmp[2:]) < inf:
    res = "\n".join(["Yes"] + [str(i) for i in tmp[2:]])

print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M=[int(s) for s in input().split()]
edge=[set() for s in range(N)]
for _ in range(M):
  a,b=[int(s) for s in input().split()]
  edge[a-1].add(b-1)
  edge[b-1].add(a-1)

INF=10*9
dist=[INF for _ in range(N)]
dist[0]=0
ans=[0 for _ in range(N)]
def BFS(n):
  reach=deque([n])
  while reach:
    _from=reach.popleft()
    for _to in edge[_from]:
      if dist[_to]==INF:
        reach.append(_to)
        dist[_to]=dist[_from]+1
        ans[_to]=_from+1
    #print(ans,reach)

BFS(0)
if max(dist)==INF:
  print('No')
else:
  print('Yes')
  for i in range(1,N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M=[int(s) for s in input().split()]
edge=[set() for s in range(N)]
for _ in range(M):
  a,b=[int(s) for s in input().split()]
  edge[a-1].add(b-1)
  edge[b-1].add(a-1)

INF=10*9
dist=[INF for _ in range(N)]
dist[0]=0
ans=[0 for _ in range(N)]
def DFS(n):
  reach=[n]
  while reach:
    _from=reach.pop()
    for _to in edge[_from]:
      if dist[_to]==INF:
        reach.append(_to)
        dist[_to]=dist[_from]+1
        ans[_to]=_from+1

DFS(0)
if max(dist)==INF:
  print('No')
else:
  print('Yes')
  for i in range(1,N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N,M=[int(s) for s in input().split()]
edge=[set() for s in range(N)]
for _ in range(M):
  a,b=[int(s) for s in input().split()]
  edge[a-1].add(b-1)
  edge[b-1].add(a-1)

INF=10*9
dist=[INF for _ in range(N)]
dist[0]=0
ans=[0 for _ in range(N)]
def DFS(n):
  reach=[n]
  while reach:
    _from=reach.pop()
    for _to in edge[_from]:
      if dist[_to]==INF:
        reach.append(_to)
        dist[_to]=dist[_from]+1
        ans[_to]=_from+1

DFS(0)
if max(dist)==INF:
  print('No')
else:
  for i in range(1,N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

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

d = deque()
d.append(1)

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

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

Submission status: Wrong Answer

Solution code:
import sys
from sys import stdin
def I():
    return stdin.readline().rstrip()
def MI():
    return map(int,stdin.readline().rstrip().split())
def LI():
    return list(map(int,stdin.readline().rstrip().split()))
#main part
from collections import deque

n, m = MI()

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

for i in range(m):
 a, b = MI()
 graph[a].append(b)
 graph[b].append(a)

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

d = deque()
d.append(1)

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

ans = dist[2:]
print('Yes')
for i in ans:
    print(i)