Problem:


Score: 400 points

Problem Statement

There is a cave.

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

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

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

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

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

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

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

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

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

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

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

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

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

Thus, the objective is satisfied.

Sample Input 2

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

Sample Output 2

Yes
6
5
5
1
1

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



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


Sample output:
Yes
1
2
2

Submission status: Runtime Error

Solution code:
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

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

Submission status: Runtime Error

Solution code:
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

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

Submission status: Runtime Error

Solution code:

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

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

Submission status: Runtime Error

Solution code:
import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

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

Submission status: Runtime Error

Solution code:

import sys, os, re
from pprint import pprint
from math import sin, cos, sqrt, radians

n, m = list(map(int, sys.stdin.readline().strip().split()))
g = [[] for x in range(n)]

for i in range(n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    x -= 1
    y -= 1
    g[x].append(y)
    g[y].append(x)

ans = {0: -1}

while len(ans) < n:
    flag = False
    for k, v in ans.items():
        for node in g[k]:
            if node not in ans:
                ans[node] = k
                flag = True
    if flag == False:
        break

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, sys.stdin.readline().split())
p = []
for i in range(M):
    p.append(list(map(int, sys.stdin.readline().split())))

graph = [[] for _ in range(N + 1)]
for pi in p:
    graph[pi[0]].append(pi[1])
    graph[pi[1]].append(pi[0])

q = deque([1])
p = [0] * (N + 1)
p[1] = 1
while q:
    u = q.popleft()
    for v in graph[u]:
        if not p[v]:
            p[v] = u
            q.append(v)
            
result = p[2:]
        
if all(result):
    print("Yes")
    [print(r) for r in result] 
else:
    print("No")

Submission status: Runtime Error

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

seen=[False]*n
depth=[-1]*n
todo=deque([])
todo.append(0)
depth[0]=1

    
while len(todo)>0:
    p=todo[0]
    
    seen[p]=True
    to=node[p]
    for i in to:
        if seen[i]:continue
        if depth[i]==-1:
            depth[i]=depth[p]+1
            todo.append(i)
    todo.popleft()
 
 

if min(depth)==-1:
    print('No')
else:
    print("Yes")
    for i in range(1,n):
        for j in node(i):
            if depth[i]>depth[j]:
                print(j+1)
                break

Submission status: Runtime Error

Solution code:

roomNum, aisleNum = map(int, input().split())
aisles = [ list(map(int, input().split())) for _ in range(aisleNum) ]

aisleTree = [ [] for _ in range(aisleNum+1) ]

for roomA, roomB in aisles:
    aisleTree[roomA].append(roomB)
    aisleTree[roomB].append(roomA)

from collections import deque
que = deque()
distanceFromRoom1 = [-1] * (roomNum+1)
distanceFromRoom1[0] = 0
distanceFromRoom1[1] = 0
for room in aisleTree[1]:
    que.append(room)
    distanceFromRoom1[room] = 1

while que:
    room = que.popleft()
    dist = distanceFromRoom1[room]
    for nextRoom in aisleTree[room]:
        if distanceFromRoom1[nextRoom] == -1:
            que.append(nextRoom)
            distanceFromRoom1[nextRoom] = room

if min(distanceFromRoom1) == -1:
    print('No')
else:
    print('Yes')
    for ans in distanceFromRoom1[2:]:
        print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

for _ in range(m):
    a,b=map(lambda x:int(x)-1,input().split())
    node[a].append(b)
    node[b].append(a)
    
#print(node)


seen=[False]*n
depth=[0]*n
todo=deque([])
todo.append(0)
    
    
while len(todo)>0:
    p=todo[0]
    
    seen[p]=True
    to=node[p]
    for i in to:
        if seen[i]:continue
        if not (i in todo):
            depth[i]+=depth[p]+1
            todo.append(i)
    todo.popleft(0)



#print(depth)

sekai=[]

for i in range(1,n):
    for j in node[i]:
        if depth[j]<depth[i]:
            sekai.append(j+1)
            break
#print(sekai)

if len(sekai)!=(n-1):
    print('No')
else:
    print("Yes")
    for a in sekai:
        print(a)

Submission status: Runtime Error

Solution code:
6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
C=[]
s=[0 for i in range(n)]
s[0] = "Yes"
 
for i in range(m):
  c,d=map(int,input().split())
  a.append(c)
  b.append(d)
 
def Next(be):
  C.clear()
  for j in be:
    for i in range(len(a)):
      if a[i] == j:
        if s[b[i]-1]==0 and 0 == be.count(b[i]):
          C.append(b[i])
          s[b[i]-1]=j
        continue

      if b[i] == j: 
        if s[a[i]-1]==0 and 0 == be.count(a[i]):
          C.append(a[i])
          s[a[i]-1]=j
        continue
    Dele(j)
  return C

def Dele(be):
  for j in range(a.count(be)):
    a.remove(be)
  for j in range(b.count(be)):
    b.remove(be)

co=[1]
while True:
  if s.count(0) == 0:
    break
  co = Next(list(co))
 
for i in s:
  print(i)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
C=[]
s=[0 for i in range(n)]
s[0] = "Yes"
 
for i in range(m):
  c,d=map(int,input().split())
  a.append(c)
  b.append(d)
 
def Next(be):
  C.clear()
  for j in be:
    for i in range(len(a)):
      if a[i] == j:
        if s[b[i]-1]==0 and 0 == be.count(b[i]):
          C.append(b[i])
          s[b[i]-1]=j
        continue

      if b[i] == j: 
        if s[a[i]-1]==0 and 0 == be.count(a[i]):
          C.append(a[i])
          s[a[i]-1]=j
        continue
    Dele(j)
  return C

def Dele(be):
  for j in range(a.count(be)):
    a.remove(be)
  for j in range(b.count(be)):
    b.remove(be)

co=[1]
for i in range(5):
  if s.count(0) == 0:
    break
  co = Next(list(co))
 
for i in s:
  print(i)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
C=[]
s=[0 for i in range(n)]
s[0] = "Yes"

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

def Next(be):
  C.clear()
  for j in be:
    for i in range(len(a)):
      if len(a) <= i:
        break
      if a[i] == j:
        
        if s[b[i]-1]==0 and 0 == be.count(b[i]):
          C.append(b[i])
          s[b[i]-1]=j
        continue
      if b[i] == j:
        
        if s[a[i]-1]==0 and 0 == be.count(a[i]):
          C.append(a[i])
          s[a[i]-1]=j
        continue
  return C
co=[1]

while True:
  if s.count(0) == 0:
    break
  co = Next(list(co))

for i in s:
  print(i)

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import deque

ab = np.array([])
n,m = map(int, input().split())

for i in range(m):
    ab = np.append(ab, list(map(int, input().split())), axis=0)

ab = ab.reshape(m,2)

Map = np.zeros((n,n),int)

for i in range(m):
    Map[int(ab[i][0]-1)][int(ab[i][1]-1)] = 1
    Map[int(ab[i][1]-1)][int(ab[i][0]-1)] = 1
    
d = deque([0])
dis = np.zeros(n, int)
appended = np.zeros(n)
appended[0] = 1

while len(d) != 0:
    hop = d.popleft()
    col = Map[hop]
    for i in range(len(col)):
        if col[i] == 1 and appended[i] == 0:
            d.append(i)
            dis[i] = hop+1
            appended[i] = 1
    
    hop += 1
    
for i in range(1,len(dis)):
    print(dis[i])

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
g = [[] for _ in range(n)]
s = [0] + [-1 for _ in range(n-1)]

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

search(0)
print("Yes")
for i in s[1:]: print(i+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
g = [[] for _ in range(n)]
s = [0] + [-1 for _ in range(n-1)]

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

search(0)
print("YES")
for i in s[1:]: print(i+1)

Submission status: Runtime Error

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

G = [[] for _ in range(N + 1)]
C = [0]*(N+1)

for ab in AB:
    a, b = ab
    G[a].append(b)
    G[b].append(a)


def main(keys):
    temp_keys = []
    for key in keys:
        for i in G[key]:
            if C[i] == 0 and i > 1:
                temp_keys.append(i)
                C[i] = C[key] + 1
        G[key] = []
    if temp_keys != []:
        main(temp_keys)


main([1])
print('Yes')
temp = 1

for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

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

G = [[] for _ in range(N + 1)]
C = [0]*(N+1)

for ab in AB:
    a, b = ab
    G[a].append(b)
    G[b].append(a)


def main(keys):
    temp_keys = []
    for key in keys:
        for i in G[key]:
            if C[i] == 0 and i > 1:
                temp_keys.append(i)
                C[i] = C[key] + 1
        G[key] = []
    if temp_keys != []:
        print(C)
        main(temp_keys)


main([1])
print('Yes')
temp = 1

for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

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

G = [[] for _ in range(N + 1)]
C = [0]*(N+1)

for ab in AB:
    a, b = ab
    G[a].append(b)
    G[b].append(a)


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(2, N + 1):
            if C[i] == 0:
                if i in G[key]:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
                    G[key] = []
    if temp_keys != []:
        main(temp_keys)


main([1])
print('Yes')
temp = 1

for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N + 1])

for ab in AB:
    a, b = ab
    G[a][b] = 1
    G[b][a] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(2, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
    if temp_keys != []:
        main(temp_keys)


main([1])

print('Yes')
temp = 1

for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N+1])

for ab in AB:
    a, b = ab
    G[a][b] = 1
    G[b][a] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(2, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
    if temp_keys != []:
        main(temp_keys)


main([1])

print('Yes')
temp = 1

for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N+1])

for ab in AB:
    a, b = ab
    G[a][b] = 1
    G[b][a] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(2, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
    if temp_keys != []:
        main(temp_keys)


main([1])

print('Yes')
temp = 1
for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N+1])

for ab in AB:
    a, b = ab
    G[a][b] = 1
    G[b][a] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(2, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
    if temp_keys != []:
        print(temp_keys)
        main(temp_keys)


main([1])

print('Yes')
temp = 1
for i in range(2, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N+1])

for ab in AB:
    a, b = ab
    if a < b:
        G[a][b] = 1
    else:
        G[b][a] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(1, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key] + 1
    if temp_keys != []:
        main(temp_keys)


main([1])

print('Yes')
temp = 1
for i in range(1, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

G = np.zeros([N + 1, N + 1])
C = np.zeros([N+1])

for ab in AB:
    a, b = ab
    G[a][b] = 1


def main(keys):
    temp_keys = []
    for key in keys:
        for i in range(1, N + 1):
            if G[key][i] >= 1:
                if C[i] == 0:
                    temp_keys.append(i)
                    C[i] = C[key]+1
    if temp_keys != []:
        main(temp_keys)


main([1])

print('Yes')

temp = 1
for i in range(1, N + 1):
    for j in range(1, N + 1):
        if C[i] - 1 == C[j]:
            print(j)
            temp = C[j]
            break

Submission status: Runtime Error

Solution code:
package abc;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class D168 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), M = sc.nextInt();
        int[][] route = new int[M][2];
        for(int i=0;i<M;i++){
            route[i][0] = sc.nextInt()-1;   //最後に1を足せ
            route[i][1] = sc.nextInt()-1;
        }

        int[] clear = new int[N];
        for(int i=0; i<clear.length; i++) {
            if(i!=0) {
                clear[i]=-1;
            }
        }

        List<Integer> queue = new ArrayList<Integer>();
        queue.add(0);

        for(int i=0; i<queue.size(); i++) {
            for(int j=0; j<M; j++) {
                if(route[j][0]==queue.get(i) && clear[route[j][1]]==-1) {
                    queue.add(route[j][1]);
                    clear[route[j][1]]=route[j][0];
                }
                if(route[j][1]==queue.get(i) && clear[route[j][0]]==-1) {
                    queue.add(route[j][0]);
                    clear[route[j][0]]=route[j][1];
                }
            }
        }
        if(queue.size()==N) {
            System.out.println("Yes");
        }
        for(int i=1; i<clear.length; i++) {
            System.out.println(clear[i]+1);
        }
    }

}

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
def input():
    return list(map(int, sys.stdin.readline().strip().split()))

n, m = input()
mem = [0 for _ in range(n + 1)] # n -> 1 거리
adj = [set() for _ in range(n + 1)]
q = []

for i in range(m):
    a, b = input()
    adj[b].append(a)
    adj[a].append(b)
    if b == 1: 
        q.append(a)
        mem[a] = 1
    if a == 1: 
        q.append(b)
        mem[b] = 1

visited = []
while q:
    v = q.pop(0)
    if v not in visited:
        visited.append(v)
        for node in adj[v]:
            if node not in visited:
                mem[node] = mem[v] + 1
                q.append(n)

if 0 in mem: print('No')
else: print('Yes', *mem[2:], sep='\n')

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = map(int,input().split())

b = [[] for i in range(n)]
for i in range(m):
    a=list(map(int,input().split()))
    b[a[1]-1].append(a[0])
    b[a[0]-1].append(a[1])
    
c = [-1 for i in range(n)]
c[0] = 0
q = deque([1])
now = 0
count = 0
while q:
    print(c,q, now)
    count = count + 1
    if count > n:
        print("No")
        exit()
    now = q.popleft() - 1
    for i in range(len(b[now])):
        #count = count + 1
        #print(now+1, "=>", b[now][i])
        if c[b[now][i] - 1] == -1:
            q.append(b[now][i])
            c[q[-1] - 1] = now + 1
            #print(c)
            
#print(count)
print("Yes")
for i in range(1,len(c)):
    print(c[i])