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:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 8)

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

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

q = deque([0])

while q:
    v = q.pop()
    for nex in aisles[v]:
        if visited[nex] != 0:
            continue
        visited[nex] = v + 1
        q.append(nex)

print("Yes")
for point in visited[1:]:
    print(point)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
path = [[]for i in range(n)]
for i in range(m):
    a,b = map(lambda x: int(x)-1,input().split())
    path[a] += [b]
    path[b] += [a]
ans = [-1 for i in range(n)]
depth = [[] for i in range(n)]
visited = [False for i in range(n)]
d=0
ans[0] = -2
depth[0]+=[0]
visited[0]=True
while False in visited:
    for i in depth[d]:
        for j in path[i]:
            if not visited[j]:
                ans[j] = i+1
                depth[d+1] += [j]
                visited[j] = True
        d += 1

Submission status: Wrong Answer

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

d = [0 for i in range(n)]
d[0] = -1
used = []
k = 0

while True:
    for i in range(n):
        if (d[i] != 0) & (((i + 1) in used) == False):
            k = i + 1
            break
    if k in used:
        break
    l = []
    for j in range(len(ab)):
        if ab[j][0] == k:
            if d[ab[j][1] - 1] == 0:
                d[ab[j][1] - 1] = k
            l.append(ab[j])
        elif ab[j][1] == k:
            if d[ab[j][0] - 1] == 0:
                d[ab[j][0] - 1] = k
            l.append(ab[j])
    for j in range(len(l)):
        ab.remove(l[j])
    used.append(k)

if 0 in d:
    print('No')
    print(d)
else:
    print('Yes')
    for i in range(1, n):
        print(d[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
 
 
def main():
    N, M, *AB = map(int, open(0).read().split())
 
    graph = [[] for _ in range(N + 1)]
    for a, b in zip(*[iter(AB)] * 2):
        graph[a].append(b)
        graph[b].append(a)
 
    # bfs
    queue = deque([1])
    sign = [0] * (N + 1)
    sign[1] = 1
    while queue:
        u = queue.popleft()
        for v in graph[u]:
            if not sign[v]:
                sign[v] = u
                queue.append(v)
    print(sign)
 
    if all(sign[2:]):
        print("Yes")
        print("\n".join(map(str, sign[2:])))
    else:
        print("No")
 
 
if __name__ == "__main__":
    main()

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)

def bfs(u):
  queue=deque([u])
  d=[None]*n
  d[u]=0
  while queue:
    v=queue.popleft()
    for i in g[v]:
      if d[i]==None:
        d[i]=d[v]+1
        queue.append(i)
  return d

d=bfs(0)
if None in d:
  print("No")
else:
  print("Yes")
  for i in range(1,n):
    print(d[i])

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
from collections import deque
dist = [MAX for i in range(N)]
pre = [-1 for i in range(N)]
To = [[] for i in range(N)]#indexに向かっている部屋番号の集合

que = deque()

for _ in range(M):
    a,b = list(map(int,input().split()))
    a -= 1
    b -= 1
    To[a].append(b)
    To[b].append(a)
dist[0] = 0
que.append(0)
while(len(que)>0):
    v = que.pop()
    for u in To[v]:
        if pre[u] != -1: continue
        dist[u] = dist[v] + 1
        que.append(u)
        pre[u] = v
        
        
print('Yes')
for i in pre[1:N]:
    print(i+1)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
from collections import deque
dist = [MAX for i in range(N)]
pre = [-1 for i in range(N)]
To = [[] for i in range(N)]#indexに向かっている部屋番号の集合

que = deque()

for _ in range(M):
    a,b = list(map(int,input().split()))
    a -= 1
    b -= 1
    To[a].append(b)
    To[b].append(a)
dist[0] = 0
que.append(0)
while(len(que)>0):
    v = que.pop()
    for u in To[v]:
        if dist[u] != MAX: continue
        dist[u] = dist[v] + 1
        que.append(u)
        pre[u] = v
        
        
print('Yes')
for i in pre[1:N]:
    print(i+1)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
from collections import deque
dist = [MAX for i in range(N)]
pre = [-1 for i in range(N)]
To = [[] for i in range(N)]#indexに向かっている部屋番号の集合

que = deque()

for _ in range(M):
    a,b = list(map(int,input().split()))
    a -= 1
    b -= 1
    To[a].append(b)
    To[b].append(a)
dist[0] = 0
que.append(0)
while(len(que)>0):
    v = que.pop()
    for u in To[v]:
        if dist[u] != MAX: continue
        dist[u] = dist[v] + 1
        que.append(u)
        pre[u] = v+1
        
        
print('Yes')
for i in pre[1:N]:
    print(i)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
S = []
Room = [MAX for i in range(N)]#2 - N
Room[0] = 0
ACCESS = [0 for i in range(N-1)]
for _ in range(M):
    a,b = list(map(int,input().split()))
    S.append([a,b])
for i in S:
    if i[0] == 1:
        Room[i[1]-1] = 1
        ACCESS[i[1]-2] = 1
    elif i[1] == 1:
        Room[i[0]-1] = 1
        ACCESS[i[0]-2] = 1
depth = 1
while max(Room) > MAX/2:
    for i in range(len(Room)):
        if Room[i] ==depth:
            k = 0
            for _ in range(len(S)):
                j = S[k]
                if (j[0] == i+1)*(Room[j[1]-1]>MAX/2):
                    Room[j[1]-1] = depth+1
                    ACCESS[j[1]-2] = i+1
                    k -= 1
                elif (j[1] == i+1)*(Room[j[0]-1]>MAX/2):
                    Room[j[0]-1] = depth + 1
                    ACCESS[j[0]-2] = i+1
                    k -= 1
                k += 1
    depth += 1
print('Yes')
for i in ACCESS:
    print(i)

Submission status: Wrong Answer

Solution code:
N,M = list(map(int,input().split()))
MAX = 1000000000
S = []
Room = [MAX for i in range(N)]#2 - N
Room[0] = 0
ACCESS = [0 for i in range(N-1)]
for _ in range(M):
    a,b = list(map(int,input().split()))
    S.append([a,b])
for i in S:
    if i[0] == 1:
        Room[i[1]-1] = 1
        ACCESS[i[1]-2] = 1
        S.remove(i)
    elif i[1] == 1:
        Room[i[0]-1] = 1
        ACCESS[i[0]-2] = 1
        S.remove(i)
depth = 1
while max(Room) > MAX/2:
    for i in range(len(Room)):
        if Room[i] ==depth:
            k = 0
            for _ in range(len(S)):
                j = S[k]
                if (j[0] == i+1)*(Room[j[1]-1]>MAX/2):
                    Room[j[1]-1] = depth+1
                    ACCESS[j[1]-2] = i+1
                    S.remove(j)
                    k -= 1
                elif (j[1] == i+1)*(Room[j[0]-1]>MAX/2):
                    Room[j[0]-1] = depth + 1
                    ACCESS[j[0]-2] = i+1
                    S.remove(j)
                    k -= 1
                k += 1
    depth += 1
print('Yes')
for i in ACCESS:
    print(i)

Submission status: Wrong Answer

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

#隣接リスト
road=[[] for i in range(n+1)]
for i in range(m):
    road[road_input[i][0]].append(road_input[i][1])
    road[road_input[i][1]].append(road_input[i][0])

from collections import deque
q=deque([1])
ans=[-1 for i in range(n)]
print(ans)
while len(q)>0:
  p=q.popleft()
  for i in road[p]:
    if ans[i-1]== -1:
      ans[i-1]=p
      q.append(i)

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

Submission status: Wrong Answer

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

def bfs(n):
  q = deque([(0,n)])
  visited = [False]*N

  while q:
    step,cur = q.popleft()
    if visited[cur]: continue
    visited[cur] = True

    for nei in adj[cur]:
      if nei == 0:
        return step + 1
      else:
        q.append((step+1, nei))
    # print(n, q)
  return -1

res = []
for i in range(1, N):
  r = bfs(i)
  if r == -1:
    print("No")
    exit()
  res.append(r)

print("Yes")
for e in res: print(e)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque
q = deque([0])
Dequeue = lambda: q.popleft()
Enqueue = lambda x: q.append(x)
n, m = map(int, input().split())
l = [[] for _ in [0]*n]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    l[a] += [b]
    l[b] += [a]
ans = [-1]*n
while q:
    v = Dequeue()
    for i in l[v]:
        if ans[i] < 1:
            ans[i] += v+1
            Enqueue(i)
if -1 in ans:
    print('No')
else:
    print('Yes')
    for j in ans[1:]:
        print(j)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

from collections import defaultdict
pth = defaultdict(list)

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

sign = [0] * (N+1)

stk = [1]
hist = set()

while len(stk) != 0:
    key = stk.pop()

    for n in pth[key]:
        if sign[n] == 0:
            sign[n] = key
            stk.append(n)
        else:
            continue

if not all([x != 0 for x in sign[2:]]):
    print("No")
else:
    print("Yes")
    for s in sign[2:]:
        print(s)

Submission status: Wrong Answer

Solution code:
# BFSして終わり 必ず存在するので
from collections import deque

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

stamp = [0] * (n + 1)
queue = deque()
for tv in t[1]:
    queue.append((tv, 1))
    stamp[tv] = 1

while len(queue) > 0:
    cur_node, pre_node = queue.popleft()
    for nex_node in t[cur_node]:
        if stamp[nex_node] != 0:
            continue
        stamp[nex_node] = pre_node + 1
        queue.append((nex_node, cur_node))

print("Yes")
for i in stamp[2:]:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
dic={}
n,m=map(int,input().split())
nod=[[]]
keep=[0]*n
for i in range(n-1):
  nod.append([])
for i in range(m):
  a,b=map(int,input().split())
  nod[a-1].append(b)
  nod[b-1].append(a)
use=deque()
use.append(1)
for i in range(0,10**100):
  if bool(use):
    sss=use.popleft()
    for j in nod[sss-1]:
      if not j in dic:
        keep[j-1]=sss
        use.append(j)
        dic[j]=1
  else:
    break

keep.pop(0)
for x in keep:
  print(x)

Submission status: Wrong Answer

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

# from collections import deque

N,M = [int(x) for x in input().split()]
next =[set() for _ in range(N+1)]

for i in range(M):
    A,B = [int(x) for x in input().split()]
    next[A].add(B)
    next[B].add(A)

# opened = deque()
opened = []
opened.append(1)

closed = [False] * (N+1)
prev = [-1] * (N+1)
prevlen = [float('inf')] * (N+1)
prevlen[1] = 0

while len(opened) != 0:
    # target = opened.popleft()
    target = opened.pop()

    closed[target] = True
    for n in next[target]:
        if closed[n] == False:
            opened.append(n)
        if prevlen[n] > prevlen[target]+1:
            prev[n] = target
            prevlen[n] = prevlen[target]+1


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

Submission status: Wrong Answer

Solution code:
from collections import deque

N,M = [int(x) for x in input().split()]
next =[set() for _ in range(N+1)]

for i in range(M):
    A,B = [int(x) for x in input().split()]
    next[A].add(B)
    next[B].add(A)

opened = deque()
opened.append(1)

#closed = set()
closed = [False] * (N+1)
prev = [-1] * (N+1)
prevlen = [float('inf')] * (N+1)
prevlen[1] = 0

while len(opened) != 0:
    target = opened.popleft()
    #closed.add(target)
    closed[target] = True
    for n in next[target]:
        # if n not in closed:
        if closed[N] == False:
            opened.append(n)
        if prevlen[n] > prevlen[target]+1:
            prev[n] = target
            prevlen[n] = prevlen[target]+1


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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
g = [[] for _ in range(n)] # 隣接リスト

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

from collections import deque

def bfs(u):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
    return d

# 0からの各頂点の距離
d = bfs(0)
print('Yes')
for i in range(1, n):
    print(d[i])

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
g = [[] for _ in range(n)] # 隣接リスト

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

from collections import deque

def bfs(u):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
    return d

# 0からの各頂点の距離
d = bfs(0)
print('Yes')
print(d)
for i in range(1, n):
    print(d[i] - 1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
g = [[] for _ in range(n)] # 隣接リスト

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

from collections import deque

def bfs(u):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
    return d

# 0からの各頂点の距離
d = bfs(0)
print('Yes')

for i in range(n - 1):
    print(d[i] + 1)

Submission status: Wrong Answer

Solution code:
import queue

N, M = map(int, input().split())
adj = [[] for i in [0]*N] # 隣接リスト
for i in [0]*M:
    a, b = map(int, input().split())
    adj[a-1].append(b)
    adj[b-1].append(a)

print(adj)
que = queue.Queue()
que.put(1)
guide = [-1]*(N)
 
while not que.empty():
    x = que.get()
    for y in adj[x-1]:
        if guide[y-1] == -1:
            que.put(y)
            guide[y-1] = x


print('Yes')
for i in guide[1:]:
        print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

array = [-1] * (n+1)

d = deque()
d.append(1)

while len(d) != 0:
  tmp = d.popleft()
  arr = dic.get(tmp, [])
  
  for v in arr:
    if array[v] == -1:
      array[v] = tmp
      d.append(v)
      

for i in range(2,n+1):
  print(array[i])

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 for _ in range(N)]
d[0] = 0
ans = [0]*N
while q:
    p = q.popleft()
    for node in G[p]:
        if d[node] == -1:
            d[node] = d[p] + 1
            q.append(node)
        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:
import copy
import collections
import numpy as np

class Graph(object):
    def __init__(self, v, e):
        self.v = [i for i in range(v)]
        self.e = e
    
    def nodes(self):
        return self.v
    
    def edges(self, v):
        return self.e[v]

def bfs(G):
    V = copy.copy(G.nodes())
    deq = collections.deque([V[0]])
    searched = {v:False for v in V}
    searched[V[0]] = True
    Tree = []
    while len(deq) != 0:
        v = deq.popleft()
        edges = [(v,i) for i in G.edges(v) if not searched[i]]
        for edge in edges:
            v,i = edge
            deq.append(i)
            Tree += [(v,i)]
            searched[i] = True
    return Tree

if __name__ == "__main__":
    n,m = map(int,input().split())
    e = [[]*n for _ in range(n)]
    for i in range(m):
        a,b = map(int,input().split())
        e[a-1].append(b-1)
        e[b-1].append(a-1)
    
    G = Graph(n,e)

    T = bfs(G)
    print("Yes")
    a = np.zeros((len(T),2))
    for i in range(len(T)):
        a[i] = np.array([T[i][1],T[i][0]])
    
    idx = np.argsort(a[:,0])
    a = a[idx].astype("i")

    for i in range(0,len(T)):
        print(a[i,1]+1)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m = LI()
    ab = [LI() for _ in range(m)]
    e = collections.defaultdict(set)
    for a,b in ab:
        e[a].add(b)
        e[b].add(a)

    v = set()
    v.add(1)
    r = [0] * (n+1)
    q = [1]
    qi = 0
    while len(q) > qi:
        t = q[qi]
        qi += 1
        for u in e[t]:
            if u not in v:
                v.add(u)
                r[u] = t
                q.append(u)

    return JA(["yes"] + r[2:], "\n")

print(main())

Submission status: Wrong Answer

Solution code:
import queue
N,M=map(int,input().split())
ans=[0] * N
q=queue.Queue()
q.put(1)

import sys
s = sys.stdin.readlines()

while 0 in ans[1:N-1]:
  r=q.get()
  for e in s:
    L, R = map(int, e.split())
    if L==r and ans[R-1]==0:
      ans[R-1]=r
      q.put(R)
    if R==r and ans[L-1]==0:
      ans[L-1]=r
      q.put(L)
    
print("Yes")
for i in range(N-1):
  print(ans[i+1])

Submission status: Wrong Answer

Solution code:

import sys, os, re
from pprint import pprint

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

print("Yes")

Submission status: Wrong Answer

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

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

ans = [-1]*(n+1)
que = deque([1])

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

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())
ab = [[] for i in range(n+1)]

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

ans = [-1]*(n+1)
que = deque([1])

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

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())
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)

for i in range(n):
    if len(node[i])==0:
        print("No")
        exit()


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()
 
 
print('Yes')
for i in range(1,n):
    for j in node[i]:
        if depth[j]<depth[i]:
            print(j)
            break

Submission status: Wrong Answer

Solution code:
from collections import deque

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,m = nm()

p = [[] for _ in range(n)]

for i in range(m):
  a,b = nm()
  p[a-1].append(b-1)
  p[b-1].append(a-1)

cnt=0
visited = set()

visited.add(0)

q = deque()
q.append(0)

ans = []

while len(q) > 0:
  num = q.popleft()
  for i in p[num]:
    if i in visited:
      continue
    ans.append(num+1)
    cnt+=1
    visited.add(i)
    q.append(i)

if len(visited)==n:
  print('Yes')
  for i in range(len(ans)):
    print(ans[i])
else:
  print('No')

Submission status: Wrong Answer

Solution code:
from collections import deque

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,m = nm()

p = [[] for _ in range(n)]

for i in range(m):
  a,b = nm()
  p[a-1].append(b-1)
  p[b-1].append(a-1)

print(p)

cnt=0
visited = set()

visited.add(0)

q = deque()
q.append(0)

ans = []

while len(q) > 0:
  num = q.popleft()
  for i in p[num]:
    if i in visited:
      continue
    ans.append(num+1)
    cnt+=1
    visited.add(i)
    q.append(i)

if len(visited)==n:
  print('Yes')
  for i in range(len(ans)):
    print(ans[i])
else:
  print('No')

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=[]
b=[]
C=[1]
D=[1]
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):
  A=[a[i] for i in range(len(a)) if a[i] == be or b[i] == be]
  B=[b[i] for i in range(len(b)) if b[i] == be or a[i] == be]
  A=[i for i in A if i != be]
  B=[i for i in B if i != be]
  A.extend(B)
  for i in A:
    if s[i-1] == 0:
      s[i-1] = be
  return A

for j in range(5):
  if s.count(0)==0:
    break
  for i in C:
    D=Next(i)
    C = D
for i in s:
  print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
v = [[] for i in range(n)]
f = [False]*n
step = [-1]*n
for i in range(m):
    a,b = map(int,input().split())
    v[a-1].append(b-1)
    v[b-1].append(a-1)
q = deque([(0,0)])
while(q):
    vi,s = q.popleft()
    #print(vi,s)
    if not(f[vi]):
        f[vi]=True
        step[vi]=s
        s+=1
        for vij in v[vi]:
            q.append((vij,s))
print("Yes")
for i in range(1,n):
    si = step[i]
    for vij in v[i]:
        if step[vij]==si-1:
            print(vij+1)

Submission status: Wrong Answer

Solution code:
import heapq

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

node = [[] for i in range(N+1)]
for i in range(M):
  a, b = map(int, input().split())
  node[a].append(b)
  node[b].append(a)
#

gw = [0]*(N+1)
gw[1] = 1
ng=1
hq = []
heapq.heapify(hq) 
heapq.heappush(hq,1)
while hq:
  n = heapq.heappop(hq)
  for i in node[n]:
    if gw[i] == 0:
      gw[i] = n
      ng += 1
      heapq.heappush(hq,i)
#
if ng < N:
  print("No")
else:
  print("Yes")
  for i in range(2,N+1):
    print(gw[i])
#

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)
    

queue = deque([0])
d = [None]*n
d[0] = 0
while queue:
    v = queue.popleft()
    for i in g[v]:
        if d[i] == None:
            d[i] = d[v] + 1
            queue.append(i)
            
if all(type(i) is int for i in d):
    print('Yes')
    for i in d[1:]:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

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())
  if d>c:
    a.append(c)
    b.append(d)
  else:
    a.append(d)
    b.append(c)
def syo(I):
  a.pop(I)
  b.pop(I)
def Next(be):
  C.clear()
  for j in range(len(be)):
    for i in range(len(a)):
      if len(a) <= i:
        break
      if (0 == be.count(b[i]) or be[j]==1) and a[i] == be[j]:#
        C.append(b[i])
        if s[b[i]-1]==0:
          s[b[i]-1]=be[j]
        syo(i)
        continue
      if (0 == be.count(a[i]) or be[j]==1) and b[i] == be[j]:# 
        C.append(b[i])
        if s[a[i]-1]==0:
          s[a[i]-1]=be[j]
        syo(i)
        continue
  return C
co=[1]

for i in range(m):
  if len(a) == 0:
    break
  co = Next(list(co))
for i in s:
  print(i)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
#n,m=6,9
a = []
b = []
C=[]
s=[0 for i in range(n)]
s[0] = "Yes"
for i in range(m):
  c,d=map(int,input().split())
  if d>c:
    a.append(c)
    b.append(d)
  else:
    a.append(d)
    b.append(c)
def syo(I):
  a.pop(I)
  b.pop(I)

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

for i in range(m):
  if len(a) == 0:
    break
  co = Next(list(co))
#  print(type(co))
#  print(co)
#  print(b)
for i in s:
  print(i)

Submission status: Wrong Answer

Solution code:
n,m=map(int, input().split())
a=[]
ans=[]
ok=[1]
nnn=0
for x in range(n):
    ans.append(0)
for x in range(m):
    b=list(map(int, input().split()))
    a.append(b)
for c in range(n):
    for t in range(m):
        for s in range(2):
            if a[t][s] in ok:
                ss=abs(s-1)
                nn=a[t][ss]
                my=a[t][s]
                if ans[nn-1]==0:
                    ans[nn-1]=my                    
    for x in range(n):
        if ans[x]!=0:
            ok.append(x+1)
for x in ans:
    if x==0:
        t=0
if t!=0:
    ans[0]="Yes"
    for x in ans:
        print(x)
if t==0:
    print('No')

Submission status: Wrong Answer

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

for _ in range(M):
  x, y = map(int, input().split())
  node[x-1].append(y-1)
  node[y-1].append(x-1)

ct = [-1]*N

i = 0
stack = []
ct[0] = 0
stack.append(0)
while len(stack) != 0:
  v = stack.pop()
  for e in node[v]:
    if ct[e] == -1:
      ct[e] = v
      stack.append(e)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    N, M = map(int, input().split(' '))
    edge = [[] for i in range(N+1)] #各node(index)から繋がるnode(value)
    for _ in range(M):
        a, b = map(int, input().split(' '))
        edge[a].append(b)
        edge[b].append(a)
    signpost = [0 for i in range(N+1)]
    q = deque([1])
    visited = [False for i in range(N+1)]
    while q:
        node = q.popleft()
        visited[node] = True
        for e in edge[node]:
            if visited[e]:
                continue
            if signpost[e] == 0:
                signpost[e] = node
                q.append(e)
    if 0 in signpost[2:]:
        print('No')
    else:
        print('Yes')
        for i in signpost[2:]:
            print('{:.1g}'.format(i))        

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    N, M = map(int, input().split(' '))
    edge = [[] for i in range(N+1)] #各node(index)から繋がるnode(value)
    for _ in range(M):
        a, b = map(int, input().split(' '))
        edge[a].append(b)
        edge[b].append(a)
    signpost = [0 for i in range(N+1)]
    q = deque([1])
    visited = []
    while q:
        node = q.popleft()
        visited.append(node)
        for e in edge[node]:
            if signpost[e] == 0:
                signpost[e] = node
                q.append(e)
    if 0 in signpost[2:]:
        print('No')
    else:
        print('Yes')
        for i in signpost[2:]:
            print('{:.1g}'.format(i))        

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    N, M = map(int, input().split(' '))
    edge = [[] for i in range(N+1)] #各node(index)から繋がるnode(value)
    for _ in range(M):
        a, b = map(int, input().split(' '))
        edge[a].append(b)
        edge[b].append(a)
    signpost = [0 for i in range(N+1)]
    q = deque([1])
    visited = []
    while q:
        node = q.popleft()
        visited.append(node)
        for e in edge[node]:
            if signpost[e] == 0:
                signpost[e] = node
                q.append(e)
    print('Yes')
    for i in signpost[2:]:
        print('{:.1g}'.format(i))        

if __name__ == "__main__":
    main()