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())
roads = []
for _ in range(N):
    roads.append([])
for _ in range(M):
    a, b = map(int, input().split())
    roads[a - 1].append(b - 1)
    roads[b - 1].append(a - 1)
visited = [float('inf')] * N
q = deque()
q.append(0)
visited[0] = 0
pre = [-1] * N
while q:
    now_room = q.pop()
    for next_room in roads[now_room]:
        if visited[next_room] != float('inf'):
            continue
        visited[next_room] = visited[now_room] + 1
        if pre[next_room] == -1:
            pre[next_room] = now_room + 1
        q.append(next_room)
# print(*roads)
# print(*pre)

print('Yes')
for i in range(1, N):
    print(pre[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
roads = []
for _ in range(N):
    roads.append([])
for _ in range(M):
    a, b = map(int, input().split())
    roads[a - 1].append(b - 1)
    roads[b - 1].append(a - 1)
visited = [float('inf')] * N
q = deque()
q.append(0)
visited[0] = 0
pre = [-1] * N
while q:
    now_room = q.pop()
    for next_room in roads[now_room]:
        if visited[next_room] != float('inf'):
            continue
        visited[next_room] = visited[now_room] + 1
        pre[next_room] = now_room
        q.append(next_room)
# print(*pre)
if not float('inf') in visited:
    print('Yes')
    for i in range(1, N):
        print(visited[i])
else:
    print('No')

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:]
if '-1' in ans:
    print('No')
else:
    print('Yes')
    print(*ans[1:], sep='\n')

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)

#  print(graph)

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

d = deque()
d.append(1)

while d:
    v = d.popleft()
    for i in graph[v]:
        if mark[i] != -1:
            continue
        mark[i] = v
        d.append(i)
if mark.count(-1) > 0:
    print("No")
else:
    print("Yes")
    ans = mark[1:]
    print(*ans, sep="\n")

Submission status: Wrong Answer

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

for i in range(m):
    a, b = map(int, input().split())
    route[a].append(b)
    route[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 route[v]:
        if dist[i] != -1:
            continue
        dist[i] = dist[v]+1
        d.append(i)
        
ans = dist[2:]
if len(ans) > 0:
    print('Yes')
    print(*ans, sep="\n")
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int,input().split())
graph = [[] for i in range(N + 1)]
dist = [-1] * (N + 1)
for _ in range(M):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)

dist[1] = 1
q = deque([1])
while q:
    x = q.popleft()
    for j in graph[x]:
        if dist[j] == -1:
            q.append(j)
            dist[j] = x
ans = dist[2:]
print(*ans, sep = "\n")

Submission status: Wrong Answer

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

#そこから直で行けるところをまとめる
ikeru = [[] for i in range(N)]
for i in range(M):
    ikeru[AB[i][0]-1].append(AB[i][1]-1)
    ikeru[AB[i][1]-1].append(AB[i][0]-1)
# print('ikeru:',ikeru)



que = deque([])
que.append(0)
# print('que ini:',que)
out=[10**6]*N
out[0]=0

while que:
    p = que.popleft()
    # print('p,que:',p,que)

    for next in ikeru[p]:
        # print('next:',next)
        if out[next]==10**6:
            que.append(next)
            out[next]=out[p]+1


# print(out)
print('Yes')
for i in range(1,N):
    print(out[i])

Submission status: Wrong Answer

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

#そこから直で行けるところをまとめる
ikeru = [[] for i in range(N)]
for i in range(M):
    ikeru[AB[i][0]-1].append(AB[i][1]-1)
    ikeru[AB[i][1]-1].append(AB[i][0]-1)
# print('ikeru:',ikeru)



que = deque([])
que.append(0)
# print('que ini:',que)
out=[10**6]*N

num=1
while que:
    p = que.popleft()
    # print('p,que:',p,que)

    for next in ikeru[p]:
        # print('next:',next)
        if out[next]==10**6:
            que.append(next)
            out[next] = num
    num+=1

# print(out)
if 10**6 in out:
    print('No')
else:
    print('Yes')
    for i in range(1,N):
        print(out[i])

Submission status: Wrong Answer

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

#そこから直で行けるところをまとめる
ikeru = [[] for i in range(N)]
for i in range(M):
    ikeru[AB[i][0]-1].append(AB[i][1]-1)
    ikeru[AB[i][1]-1].append(AB[i][0]-1)
# print('ikeru:',ikeru)



que = deque([])
que.append(0)
# print('que ini:',que)
out=[10**6]*N

num=1
while que:
    p = que.popleft()
    # print('p,que:',p,que)

    for next in ikeru[p]:
        # print('next:',next)
        if out[next]==10**6:
            que.append(next)
            out[next] = num
    num+=1

# print(out)
for i in range(1,N):
    print(out[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra
import numpy as np

V, E = map(int, input().split())                              # V:頂点数 E:辺の数 r:始点
r = 0
data = []
for _ in range(E):
    A, B = map(int, input().split())
    data.append((A, B, 1))

edge = np.array([data[i] for i in range(E)], dtype = np.int64).T    # scr_matrix に乗せるために転置している

graph = csr_matrix((edge[2], (edge[:2] - 1)), (V, V))           # [[辺の左端],[辺の右端],[辺の距離]] の形で渡す
# graph = csr_matrix((edge[2], (edge[:2] )), (V, V))           # 番号のデクリメントが不要な場合

d = dijkstra(graph, directed = False , indices = r, return_predecessors = True)          # 有効グラフの場合は directed = True とする。
                                                            # 経路が欲しい場合は return_predecessors = True を入れるとよい。
if sum(d[0]) == float("inf"):
    print("No")
else:
    print("Yes")
    print(*(d[1][1:]+1) , sep="\n")

Submission status: Wrong Answer

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

ans=[0]*(N+1)
checked=[False]*(N+1)
d=[1]
while d:
  c=d.pop()
  for g in G[c]:
    if not checked[g]:
      d.append(g)
      checked[g]=True
      ans[g]=c

print('Yes')
for i in range(2, len(ans)):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A=[]
B=[]
for m in range(M):
  s = [int(x) for x in input().split()]
  A.append(s[0])
  B.append(s[1])

ans={}

def Search(s):
  res=[]
  for (a, b) in zip(A, B):
    if a==s:
      res.append(b)
    elif b==s:
      res.append(a)
  return res

cnt=0
while cnt<M:
  cnt+=1
  if cnt==1:
    num=Search(cnt)
    if len(num)==0:
      print('No')
      break
    for n in num:
      ans[n] = cnt
  else:
    num={}
    for k, i in ans.items():
      if i==cnt-1:
        num[k]=Search(k)
    for k, n in num.items():
      for n2 in n:
        if n2 not in ans.keys():
          ans[n2] = k
else:
  print('Yes')
  for k, a in sorted(ans.items()):
    if k != 1:
      print(a)

Submission status: Wrong Answer

Solution code:
class priority_queue:
    def __init__(self):
        self.q = []
        self.n = 0
    
    def push(self, x):
        self.q.append(x)
        self.n += 1
        i = self.n - 1
        while i > 0:
            p = (i-1)//2
            if x[0] < self.q[p][0]:
                self.q[i] = self.q[p]
                i = p
            else:
                break
        self.q[i] = x
    
    def pop(self):
        res = self.q[0]
        x = self.q[self.n - 1]
        del self.q[self.n - 1]
        self.n -= 1
        if self.n != 0:
            self.q[0] = x
        i = 0
        while i*2+1 < self.n:
            p1, p2 = i*2+1, i*2+2
            if p2 < self.n and self.q[p2][0] < self.q[p1][0]:
                p1 = p2
            if self.q[p1][0] < x[0]:
                self.q[i] = self.q[p1]
                i = p1
            else:
                break
        
        return res
    
    def notempty(self):
        return self.n > 0
        

def dijkstra():
    landmark = [0] + [float('inf')] * N
    used = [False] * (N+1)
    q = priority_queue()
    q.push((0, 1, 0))
    while q.notempty():
        k, v, l = q.pop()
        if used[v]:
            continue
        
        landmark[v] = l
        used[v] = True
        for e in E[v]:
            if not used[e]:
                q.push((k+1, e, v))
    
    return landmark
    
        
        

N, M = map(int, input().split())
E = [[] for _ in range(N+1)]
for i in range(M):
    A, B = map(int, input().split())
    E[A].append(B)
    E[B].append(A)
    
landmark = dijkstra()
if float('inf') in landmark:
    print('No')
else:
    print('Yes')
    for x in landmark[2:]:
        print(x)

Submission status: Wrong Answer

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

q = deque([0])
d = [-1]*n
d[0] = 0
ans = [0]*n
print("Yes")

while q:
    p = q.popleft() # 先に入れた要素を取り出す
    for node in g[p]:
        if d[node] == -1:
            d[node] = d[p] + 1
            q.append(node) # リストへの追加
            ans[node] = d[p]+1
        # else:
        #     if d[node] + 1 == d[p]:
        #         ans[p] = node + 1

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from collections import deque

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

way=[[] for i in range(N)]

for _ in range(M):
    A,B=map(int,input().split())
    way[A-1].append(B-1)
    way[B-1].append(A-1)

visited=[-1 for i in range(N)]
visited[0]=0
dq=deque([0]) #loc,value

while dq:
    temp=dq.popleft()
    for item in way[temp]:
        if visited[item]==-1:
            visited[item]=temp
            dq.appendleft(item)

if -1 in visited:
    print("No")
else:
    print("Yes")
    for item in visited[1:]:
        print(item+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
def bfs():
    visited=[-1]*n
    q=deque()
    start=1
    visited[start-1]=0
    q.append(start)
    while q:
        now=q.popleft()
        for next in graph[now-1]:
            if visited[next-1]==-1:
                q.append(next)
                visited[next-1]=visited[now-1]+1
    print('Yes')
    for i in visited[1:]:
        print(i)

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

graph = [[] for _ in range(n)]
for v in p:
    graph[v[0]-1].append(v[1])
    graph[v[1]-1].append(v[0])  # 有向グラフなら消す

#print(graph)  # [[2, 3, 5], ..., [1, 3, 4]]

bfs()

Submission status: Wrong Answer

Solution code:
from collections import deque

N,M=map(int,input().split())
graph=[[] for i 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)
print("Yes")
for i in range(2,N+1):
    print(dist[i])

Submission status: Wrong Answer

Solution code:
def nearlist(N, LIST):
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック
    dist = [n for _ in range(N)]  # 前処理
    PATH = [-1 for _ in range(N)]
    dist[S], PATH[S] = 0, 's'
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i in NEAR[q]:  # 移動先の候補
            if (i != 0) and (i in frag):
                dist[i] = dist[PATH[i]] + 1
            if dist[i] > dist[q] + 1:
                dist[i] = dist[q] + 1
                PATH[i] = q
            if i in frag:  # 処理済みか否か
                continue
            stack.append(i), frag.add(i)
    return PATH


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

near = nearlist(n, ab)
path = dfs(near, 0, n)

print('Yes')
for i in range(1, n):
    print(path[i] + 1)

Submission status: Wrong Answer

Solution code:
def nearlist(N, LIST):
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def dfs(NEAR, S, N):  # 深優先探索  # スタック

    dist = [n for _ in range(N)]  # 前処理
    PATH = [-1 for _ in range(N)]
    dist[S], PATH[S] = 0, 's'
    stack, frag = [S], set([S])

    while len(stack) > 0:
        q = stack.pop()
        for i in NEAR[q]:  # 移動先の候補
            if dist[i] > dist[q] + 1:
                dist[i] = dist[q] + 1
                PATH[i] = q
            if i in frag:  # 処理済みか否か
                continue
            stack.append(i), frag.add(i)
    return PATH


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

near = nearlist(n, ab)
path = dfs(near, 0, n)

print('Yes')
for i in range(1, n):
    print(path[i] + 1)

Submission status: Wrong Answer

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

for i in range(M):
	j,k=map(int,input().split())
	graph[j-1].append(k-1)
	graph[k-1].append(j-1)

queue=deque()
dist=[None]*N
dist[0]=0
queue.append(0)
while queue:
	v=queue.popleft()
	for i in graph[v]:
		if dist[i] is None:
			dist[i]=dist[v]+1
			queue.append(i)
print("Yes")
for i in range(1,len(dist)):
	print(dist[i])

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import deque
from copy import deepcopy
def main():
    n, m = map(int, input().split())
    edges = {e:[] for e in range(n + 1)}
    for _ in range(m):
        a, b = map(int, input().split())
        edges[a].append(b)
        edges[b].append(a)

    res = [n + 1] * (n + 1)
    res[1] = 0
    q1 = deque()
    q2 = deque()
    q1.append(1)
    while q1:
        for q1e in q1:
            for adj in edges[q1e]:
                if res[adj] > q1e:
                    res[adj] = q1e
                    q2.append(adj)
        q1 = deepcopy(q2)
        q2 = deque()
    print('Yes')
    print(*res[2:],sep='\n')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print('No')

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

queue = deque(aisle[0])
visited = [-1] * n
parent = [0] * n
now = 1

visited[0]=0

while len(queue) > 0:
    for i in aisle[now-1]:
        if visited[i-1]==-1:
            queue.append(i)
            visited[i-1]=visited[now-1]+1
            parent[i-1] = now
    now = queue.popleft()

print('Yes')
for i in range(2,n+1):
    print(parent[i-1])

Submission status: Wrong Answer

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

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

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

while stack:
    v = stack.popleft()
    for nv in g[v]:
        if dist[nv] == -1:
            dist[nv] = v+1
            stack.append(nv)
print("YES")
print(*dist[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,m=map(int,input().split())
s=[[]for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    s[a-1].append(b-1)
    s[b-1].append(a-1)
t=[-1]*n
q=[1]
while q:
    p=[]
    for j in q:
        for i in s[j]:
            if i!=j and t[i]==-1:
                p.append(i)
                t[i]=j
    q=p[::]
print("Yes")
for i in t[1:]:
    print(i+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
ab=[[] for _ in range(n+1)]
for _ in range(m):
    a,b=map(int,input().split())
    ab[a].append(b) 
    ab[b].append(a)
ans=[0]*(n+1)
ans[1]=1
que=deque()
que.append(1)
while que:
    x=que.popleft()
    for i in ab[x]:
        if ans[i]==0:
            ans[i]=x
            que.append(i)
print("Yse")
for j in range(2,n+1):
  print(ans[j])

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
lst=[[] for i in range(n)]
visited=[True]+[False]*(n-1)
ans=[0]*n

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

Q=[0]

while Q:
  v=Q.pop(0)
  for i in lst[v]:
        if visited[i]==False:
              visited[i]=True
              ans[i]=v
              Q.append(v)

print('Yes')

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
root = [[] for _ in range(n)]
adj = [-1 for _ in range(n)]
for _ in range(m):
  r,c = map(int, input().split())
  root[r-1].append(c-1)
  root[c-1].append(r-1)
  
visited = {0}
explor = [0]

while len(visited)!=n:
  tmp_explor = set()
  while explor:
    tmp = explor.pop()
    tmp_explor = tmp_explor.union(set(root[tmp]))
    visited = visited.union(set(root[tmp]))
    for i in root[tmp]:
      if adj[i]==-1:
        adj[i] = tmp
  explor = tmp_explor.copy()

print('Yes')
for i in adj:
  print(i+1)

Submission status: Wrong Answer

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

matrix = [[0]*n for _ in range(n)]
for _ in range(m):
  r,c = map(int, input().split())
  matrix[r-1][c-1] = 1
  matrix[c-1][r-1] = 1
  
visited = {0}
costs = [n]*n
path = [0]

def extract_adj(x):
  res_set = set()
  for i, flg in enumerate(x):
    if flg:
      res_set.add(i)
  return res_set

turn = 0
while len(visited)!=n:
  turn+=1
  tmp_visited = set()
  for i in visited:
    tmp = set(extract_adj(matrix[i]))
    tmp_visited = tmp_visited.union(tmp)
  for i in tmp_visited:
    costs[i] = min(costs[i], turn)
  visited = tmp_visited.copy()
print('Yes')
for i in costs[1:]:
  print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m = map(int,input().split())
turo = [[] for i in range(n+1)]
sirube = [-1 for i in range(n+1)]

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

q = deque()
sirube[1] = 0

for i in turo[1]:
    sirube[i] = 1
    q.append(i)

while len(q) != 0:
    x = q.popleft()
    for i in turo[x]:
        if sirube[i] == -1:
            sirube[i] = x
            for j in turo[i]:
                q.append(j)

print('Yes')
for i in range(len(sirube)):
    if i != 0 and i != 1:
        print(sirube[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

n, m = map(int,input().split())
turo = [[] for i in range(n+1)]
sirube = [-1 for i in range(n+1)]

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

q = deque()
sirube[1] = 0

for i in turo[1]:
    sirube[i] = 1
    q.append(i)

while len(q) != 0:
    x = q.popleft()
    for i in turo[x]:
        if sirube[i] == -1:
            sirube[i] = x
            for j in turo[i]:
                if sirube[j] == -1:
                    q.append(j)

print('Yes')
for i in range(2,len(sirube)):
    print(sirube[i])

Submission status: Wrong Answer

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

for _ in range(m):
    a,b = map(int, input().split())
    ikisaki[a].append(b)
    ikisaki[b].append(a)
ans=[0]+[1]+[0]*(n-1)
tansakuheya=deque([1])
tmp=deque()

while tansakuheya:

    for bangou in tansakuheya:
        tmp=deque()

        for heya in ikisaki[bangou]:
            if ans[heya]==0:
                ans[heya]=bangou
                tmp.append(heya)

    tansakuheya=tmp.copy()

    if not tansakuheya:
        break

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

Submission status: Wrong Answer

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

for _ in range(m):
    a,b = map(int, input().split())
    ikisaki[a].append(b)
    ikisaki[b].append(a)
ans=[0]+[1]+[0]*(n-1)
tansakuheya=deque([1])
tmp=deque()

for _ in range(1000009):

    for bangou in tansakuheya:
        tmp=deque()

        for heya in ikisaki[bangou]:
            if ans[heya]==0:
                ans[heya]=bangou
                tmp.append(heya)

    tansakuheya=tmp.copy()


    if not tansakuheya:
        break

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

Submission status: Wrong Answer

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

for _ in range(m):
    a,b = map(int, input().split())
    ikisaki[a].append(b)
    ikisaki[b].append(a)
ans=[0]+[1]+[0]*(n-1)
tansakuheya=deque([1])
tmp=deque()

for _ in range(100009):

    for bangou in tansakuheya:
        tmp=deque()

        for heya in ikisaki[bangou]:
            if ans[heya]==0:
                ans[heya]=bangou
                tmp.append(heya)

    tansakuheya=tmp.copy()


    if not tansakuheya:
        break

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 _ in range(M)]

g = [set() for _ in range(N)]
for a, b in AB:
    a, b = a-1, b-1
    g[b].add(a)
    g[a].add(b)

mark = [-1]*N
mark[0] = 0
q = [0]
while q:
    t = q.pop()

    for c in g[t]:
        if mark[c] == -1:
            mark[c] = t
            q.append(c)

print("Yes")
for m in mark[1:]:
    print(m+1)

Submission status: Wrong Answer

Solution code:
from collections import deque


def solve():
    N, M = map(int, input().split())
    to = [[] for _ in range(N)]

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

    que = deque()
    hint = [0] * N
    seen = [False] * N

    seen[0] = True
    que.appendleft(0)
    
    # BFS
    while que:
        v = que.popleft()
        for nv in to[v]:
            if seen[nv]:
                continue
            que.appendleft(nv)
            hint[nv] = v + 1
            seen[nv] = True

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

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

neighbors = {}
for _ in range(m):
  a, b = map(int, input().split())
  if a not in neighbors.keys():
    neighbors.update({a:[]})
  if b not in neighbors[a]:
    neighbors[a].append(b)
  if b not in neighbors.keys():
    neighbors.update({b:[]})
  if a not in neighbors[b]:
    neighbors[b].append(a)
#print(neighbors)
lst = [0] * (n - 1)
visited = set()
queue = [[1]]
count = 0
while queue:
  rooms = []
  next_rooms = queue.pop(0)
  #print(next_rooms)
  for next_room in next_rooms:
    if next_room in visited:
      continue
    if lst[next_room - 2] == 0 and next_room != 1:
      lst[next_room - 2] = count
    visited.add(next_room)
    for nei in neighbors[next_room]:
      rooms.append(nei)
  if rooms:
    queue.append(rooms)
    count += 1
if 0 in lst:
  print('No')
else:
  print('Yes')
  for i in range(n - 1):
    print(lst[i])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

deq=deque()
dic=defaultdict(list)

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

dist=[-1]*(n+1)
dist[1]=0
for i in dic[1]:
    deq.append(i)
    dist[i]=1

while deq:
    p = deq.popleft()
    for i in dic[p]:
        if dist[i]!=-1:
            continue
        else:
            dist[i]=dist[p]+1
            deq.append(i)

bl = True
for i in range(2, n+1):
    if dist[i]<0:
        bl = False

if bl:
    print("Yes")
    for i in range(2, n+1):
        print(dist[i])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import deque

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

N,M = MI()
links = [[] for _ in range(N)]

for _ in range(M):
    a, b = MI()
    a -= 1
    b -= 1
    links[a].append(b)
    links[b].append(a)

ans = [-1] * N
ans[0] = 0
q = deque([(0, 0)])
while q:
  room, prev = q.popleft()
  for next_ in links[room]:
    if ans[next_] < 0:
      ans[next_] = ans[room]+1
      q.append((next_, room))
print("Yes")
for i in range(1,N):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

N,M = MI()
links = [[] for _ in range(N)]

for _ in range(M):
    a, b = MI()
    a -= 1
    b -= 1
    links[a].append(b)
    links[b].append(a)

ans = [-1] * N
ans[0] = 0
q = deque([(0, 0)])
while q:
  room, prev = q.popleft()
  for next_ in links[room]:
    if ans[next_] < 0:
      ans[next_] = ans[room]+1
      q.append((next_, room))
for i in range(1,N):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

N,M = MI()
links = [[] for _ in range(N)]

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

ans = [-1] * N
ans[0] = 0
q = deque([(0, 0)])
while q:
    room, prev = q.popleft()
    for next_ in links[room]:
        if ans[next_] < 0:
            ans[next_] = ans[room]+1
            q.append((next_, room))
print("Yes")
for i in range(1,N):
  print(ans[i])

Submission status: Wrong Answer

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

N,M = map(int,input().split())
l = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    l[A-1][B-1] = 1
    l[B-1][A-1] = 1
a = np.array(l)
n, labels = connected_components(a)
if n == 1:
    print('Yes')
    s = [0]
    t = [i for i in range(1,N)]
    ans = [0]*N
    x = 1
    #print(l)
    while True:
        S = s.copy()
        for i in S:
            for j in t:
                if l[i][j] == 1 :
                    s.append(j)
                    t.remove(j)
                    ans[j] = i+1
                    #print(i,j)
        if t == []:
            break
    for i in range(1,N):
        print(ans[i])
else:
    print('No')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

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)