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: Time Limit Exceeded

Solution code:
#inport
import sys
import numpy as np

#input
N,M = map(int, sys.stdin.readline().split())
P = [list(map(int, sys.stdin.readline().split())) for i in range(M)]

#shori
##初期化
P = np.array(P)     
ans = np.zeros((N,2))
tmp_latest = np.zeros(1)
tmp_latest_tmp = np.zeros(1)
tmp_latest[0] = 1
pos_latest = 0
num_latest = 1
ans[0][1] = 1 #部屋1をてがかりありに
flg_pos = 1
flg_fale = 0
num_crew = 1
num_left_room = N-1
left_room = np.zeros(N)

for i in range(N):
    ans[i][0] = i + 1

while num_crew < N:
    if flg_pos == 0:
        flg_fale = 1
        break

    flg_pos = 0
    for i in range(num_latest):
        tmp_room_num = tmp_latest[i]
        tmp_root = P[np.any( P == int(tmp_room_num), axis = 1)]
        root_loop_n = int(np.size(tmp_root)/2)
        for j in range(root_loop_n):
            if tmp_root[j][0]==tmp_latest[i]:
                if(ans[tmp_root[j][1]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][1])
                    ans[tmp_root[j][1]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1
            elif tmp_root[j][1]==tmp_latest[i]:
                if(ans[tmp_root[j][0]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][0])
                    ans[tmp_root[j][0]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1               
        P = np.delete(P, np.where(P == int(tmp_room_num))[0], axis = 0)

    #Nループ終了時にtmp_latestを更新
    tmp_latest = np.insert(tmp_latest, num_latest, tmp_latest_tmp)
    for j in range(num_latest):
        tmp_latest = np.delete(tmp_latest,0)
    num_latest = np.size(tmp_latest)
    tmp_latest = np.delete(tmp_latest,num_latest-1)
    num_latest = np.size(tmp_latest)
    tmp_latest_tmp = np.zeros(1)

    
if flg_fale == 0:
    print('Yes')
    for i in range(N-1):
        print(int(ans[i+1][1]))
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
#inport
import sys
import numpy as np

#input
N,M = map(int, sys.stdin.readline().split())
P = [list(map(int, sys.stdin.readline().split())) for i in range(M)]

#shori
##初期化
P = np.array(P)     
ans = np.zeros((N,2))
tmp_latest = np.zeros(1)
tmp_latest_tmp = np.zeros(1)
tmp_latest[0] = 1
pos_latest = 0
num_latest = 1
ans[0][1] = 1 #部屋1をてがかりありに
flg_pos = 1
flg_fale = 0
num_crew = 1
num_left_room = N-1
left_room = np.zeros(N)

for i in range(N):
    ans[i][0] = i + 1

while num_crew < N:
    if flg_pos == 0:
        flg_fale = 1
        break

    flg_pos = 0
    for i in range(num_latest):
        tmp_room_num = tmp_latest[i]
        tmp_root = P[np.any( P == int(tmp_room_num), axis = 1)]
        root_loop_n = int(np.size(tmp_root)/2)
        for j in range(root_loop_n):
            if tmp_root[j][0]==tmp_latest[i]:
                if(ans[tmp_root[j][1]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][1])
                    ans[tmp_root[j][1]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1
            elif tmp_root[j][1]==tmp_latest[i]:
                if(ans[tmp_root[j][0]-1][1]==0):
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_root[j][0])
                    ans[tmp_root[j][0]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1               
        P = np.delete(P, np.where(P == int(tmp_room_num))[0], axis = 0)

    #Nループ終了時にtmp_latestを更新
    tmp_latest = np.insert(tmp_latest, num_latest, tmp_latest_tmp)
    for j in range(num_latest):
        tmp_latest = np.delete(tmp_latest,0)
    num_latest = np.size(tmp_latest)
    tmp_latest_tmp = np.zeros(1)

    
if flg_fale == 0:
    print('Yes')
    for i in range(N-1):
        print(int(ans[i+1][1]))
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
def mapinput():
    return map(int,input().split())

n,m = mapinput()
rootlist = set()
delist = []
flag = [False for i in range(n)]
for i in range(m):
    rootlist.add(tuple(mapinput()))

que = [1]
while len(que) != 0:
    for root in rootlist:
        temp = list(root)
        if que[0] in temp:
            for i in [j for j in root if j != que[0]]:
                if flag[i-1]:
                    break
                else:
                    flag[i-1] = que[0]
                    que.append(i)
                    delist.append(root)

    for root in delist:
        rootlist.remove(root)
    delist = []
    que.pop(0)


print('Yes')
for i,root in enumerate(flag):#スライスで実装
    if i == 0:
        continue
    print(root)

Submission status: Time Limit Exceeded

Solution code:
def mapinput():
    return map(int,input().split())

n,m = mapinput()
rootlist = []
delist = []
flag = [False for i in range(n)]
for i in range(m):
    rootlist.append(list(mapinput()))

que = [1]
while len(que) != 0:
    for root in rootlist:
        if que[0] in root:
            for i in [j for j in root if j != que[0]]:
                if flag[i-1]:
                    break
                else:
                    flag[i-1] = que[0]
                    que.append(i)

    for root in delist:
        rootlist.remove(root)
    delist = []
    que.pop(0)


print('Yes')
for i,root in enumerate(flag):
    if i == 0:
        continue
    print(root)

Submission status: Time Limit Exceeded

Solution code:
import heapq
import numpy as np

N, M = [int(x) for x in input().split(" ")]

e = {i: [] for i in range(N)}
for _ in range(M):
  a, b = [int(x)-1 for x in input().split(" ")]
  e[a] = e[a] + [b]
  e[b] = e[b] + [a]

# roots = [[int(x) for x in input().split(" ")] for _ in range(M)]

d = np.ones(N, dtype=int) * -1
# prev = np.ones(N, dtype=int) * -1
# q = [(d[0], 0)]
# heapq.heapify(q)

# while len(q) != 0:
#   c, u = heapq.heappop(q)
#   for nv in e[u]:
#     alt = d[u] + 1
#     if d[nv] > alt:
#       d[nv] = alt
#       prev[nv] = u
#       heapq.heappush(q, (alt, nv))

q = [0]
while len(q) != 0:
  v = q.pop(0)
  for ne in e[v]:
    if d[ne] == -1:
      d[ne] = v
      q.append(ne)

print("Yes")
# for p in prev[1:]:
#   print(p+1)
print("\n".join([str(p+1) for p in d[1:]]))

Submission status: Time Limit Exceeded

Solution code:
import heapq
import numpy as np

N, M = [int(x) for x in input().split(" ")]

e = {i: [] for i in range(N)}
for _ in range(M):
  a, b = [int(x)-1 for x in input().split(" ")]
  e[a] = e[a] + [b]
  e[b] = e[b] + [a]

# roots = [[int(x) for x in input().split(" ")] for _ in range(M)]

d = np.ones(N, dtype=int) * np.inf
d[0] = 0
prev = np.ones(N, dtype=int) * -1
q = [(d[0], 0)]
heapq.heapify(q)

while len(q) != 0:
  c, u = heapq.heappop(q)
  for nv in e[u]:
    alt = d[u] + 1
    if d[nv] > alt:
      d[nv] = alt
      prev[nv] = u
      heapq.heappush(q, (alt, nv))

print("Yes")
# for p in prev[1:]:
#   print(p+1)
print("\n".join([str(p+1) for p in prev[1:]]))

Submission status: Time Limit Exceeded

Solution code:
import heapq
import numpy as np

N, M = [int(x) for x in input().split(" ")]

e = {i: [] for i in range(N)}
for _ in range(M):
  a, b = [int(x)-1 for x in input().split(" ")]
  e[a] = e[a] + [b]
  e[b] = e[b] + [a]

# roots = [[int(x) for x in input().split(" ")] for _ in range(M)]

d = np.ones(N, dtype=int) * np.inf
d[0] = 0
prev = np.ones(N, dtype=int) * -1
q = [(d[0], 0)]
heapq.heapify(q)

while len(q) != 0:
  c, u = heapq.heappop(q)
  for nv in e[u]:
    alt = d[u] + 1
    if d[nv] > alt:
      d[nv] = alt
      prev[nv] = u
      heapq.heappush(q, (alt, nv))

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[0]*n
b=[1]
left=[0]*m
right=[0]*m
for i in range(m):
    left[i],right[i]=map(int,input().split())
while b!=[]:
    c=[]
    for i in range(m):
        if left[i] in b:
            if a[right[i]-1]==0:
                a[right[i]-1]=left[i]
                c.append(right[i])
        if right[i] in b:
            if a[left[i]-1]==0:
                a[left[i]-1]=right[i]
                c.append(left[i])
    b=c
if 0 not in a:
    print("Yes")
    for i in a[1:]:
        print(i)
else:
    print("No")

Submission status: Time Limit Exceeded

Solution code:
[N, M] = list(map(int, input().split()))
X = [0]*N
A = []
B = []

for i in range(M):
    x = list(map(int, input().split()))
    A.append(x[0])
    B.append(x[1])

position = set()
np = set()
position.add(1)
count = 0


while count < N:
    for j in range(M):
        if A[j] in position and X[B[j] - 1] == 0:
            X[B[j] - 1] = A[j]
            np.add(B[j])
        if B[j] in position and X[A[j] - 1] == 0:
            X[A[j] - 1] = B[j]
            np.add(A[j])
    count += len(position)
    position.clear()
    for k in np:
        position.add(k)
    np.clear()

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

Submission status: Time Limit Exceeded

Solution code:
#inport
import sys
import numpy as np

#input
N,M = map(int, sys.stdin.readline().split())
P = [list(map(int, sys.stdin.readline().split())) for i in range(M)]

#shori
##初期化
ans = np.zeros((N,3))
tmp_latest = np.zeros(1)
tmp_latest_tmp = np.zeros(1)
tmp_latest[0] = 1
pos_latest = 0
num_latest = 1
ans[0][1] = 1 #部屋1をてがかりありに
ans[0][2] = 1 #部屋1をてがかりありに
flg_pos = 1
flg_fale = 0
num_crew = 0

for i in range(N):
    ans[i][0] = i + 1

while num_crew < N:
    if flg_pos == 0:
        flg_fale = 1
        break

    flg_pos = 0
    for i in range(N):
        if ans[i][2] == 0:
            #手がかりが置いてなければ置ける手がかりを探索
            tmp_room_num = ans[i][0]

            for j in range(M):
                if P[j][1] == tmp_room_num:
                    if P[j][0] in tmp_latest:
                        ans[P[j][1]-1][1] = P[j][0]
                        ans[P[j][1]-1][2] = 1
                        tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_room_num)
                        flg_pos = 1
                        break
                elif P[j][0] == tmp_room_num: 
                    if P[j][1] in tmp_latest:
                        ans[P[j][0]-1][1] = P[j][1]
                        ans[P[j][0]-1][2] = 1
                        tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, tmp_room_num)
                        flg_pos = 1
                        break

    #Nループ終了時にtmp_latestを更新
    tmp_latest = np.insert(tmp_latest, num_latest, tmp_latest_tmp)
    for j in range(num_latest):
        tmp_latest = np.delete(tmp_latest,0)
    
    num_latest = np.size(tmp_latest)
    num_crew = np.sum(ans,axis = 0)[2]

if flg_fale == 0:
    print('Yes')
    for i in range(N-1):
        print(int(ans[i+1][1]))
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

u = 0
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
            ans[i] = v+1
            queue.append(i)

ans.pop(0)
if None in ans:
    print("No")
else:
    print("Yes")
    for i in ans:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

u = 0
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
            ans[i] = v+1
            queue.append(i)

ANS = ans[1:]
if None in ANS:
    print("No")
else:
    print("Yes")
    for i in ANS:
        print(i)

Submission status: Time Limit Exceeded

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

def main():
    n, m = map(int, input().split())
    L = [[0] for _ in range(n)]
    for _ in [0] * m:
        a, b = map(int, input().split())
        L[a-1].append(b-1)
        L[b-1].append(a-1)
    L[0][0] = 1
    ans = [0] * n
    dq = deque([0])
    while len(dq) > 0:
        t = dq.popleft()
        for i in range(1, len(L[t])):
            next = L[t][i]
            if ans[next] == 0:
                ans[next] = t + 1
            if L[next][0] == 0:
                dq.append(next)
        else:
            L[t][0] = 1
    print('Yes')
    print(*ans[1:], sep='\n')

main()

Submission status: Time Limit Exceeded

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

def main():
    n, m = map(int, input().split())
    L = [[0] for _ in range(n)]
    for _ in [0] * m:
        a, b = map(int, input().split())
        L[a-1].append(b-1)
        L[b-1].append(a-1)
    L[0][0] = 1
    ans = [0] * n
    dq = deque([0])
    while len(dq) > 0:
        t = dq.popleft()
        for i in range(1, len(L[t])):
            if ans[L[t][i]] == 0:
                ans[L[t][i]] = t + 1
            if L[L[t][i]][0] == 0:
                dq.append(L[t][i])
        else:
            L[t][0] = 1
    print('Yes')
    print(*ans[1:], sep='\n')

main()

Submission status: Time Limit Exceeded

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

def main():
    n, m = map(int, input().split())
    L = [[0] for _ in range(n)]
    for _ in [0] * m:
        a, b = map(int, input().split())
        L[a-1].append(b-1)
        L[b-1].append(a-1)
    L[0][0] = 1
    d = [0] * n
    ans = [0] * n
    dq = deque([0])
    while len(dq) > 0:
        t = dq.popleft()
        for i in range(1, len(L[t])):
            dti = d[L[t][i]]
            if dti == 0 or dti > d[t] + 1:
                d[L[t][i]] = d[t] + 1
                ans[L[t][i]] = t + 1
            if L[L[t][i]][0] == 0:
                dq.append(L[t][i])
        else:
            L[t][0] = 1
    print('Yes')
    print(*ans[1:], sep='\n')

main()

Submission status: Time Limit Exceeded

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

def main():
    n, m = map(int, input().split())
    L = [[0] for _ in range(n)]
    for _ in range(m):
        a, b = map(int, input().split())
        L[a-1].append(b-1)
        L[b-1].append(a-1)
    L[0][0] = 1
    d = [0] * n
    ans = [0] * n
    dq = deque([0])
    while len(dq) > 0:
        t = dq.popleft()
        for i in range(1, len(L[t])):
            if d[L[t][i]] == 0:
                d[L[t][i]] = d[t] + 1
                ans[L[t][i]] = t + 1
            else:
                if d[L[t][i]] > d[t] + 1:
                    d[L[t][i]] = d[t] + 1
                    ans[L[t][i]] = t + 1
            if L[L[t][i]][0] == 0:
                dq.append(L[t][i])
        else:
            L[t][0] = 1
    print('Yes')
    print(*ans[1:], sep='\n')

main()

Submission status: Time Limit Exceeded

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

n, m = map(int, input().split())
L = [[0] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    L[a-1].append(b-1)
    L[b-1].append(a-1)
L[0][0] = 1
d = [0] * n
ans = [0] * n
dq = deque([0])
while len(dq) > 0:
    t = dq.popleft()
    for i in range(1, len(L[t])):
        if d[L[t][i]] == 0:
            d[L[t][i]] = d[t] + 1
            ans[L[t][i]] = t + 1
        else:
            if d[L[t][i]] > d[t] + 1:
                d[L[t][i]] = d[t] + 1
                ans[L[t][i]] = t + 1
        if L[L[t][i]][0] == 0:
            dq.append(L[t][i])
    else:
        L[t][0] = 1
print('Yes')
print(*ans[1:], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n, m = map(int, input().split())
L = [[0] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    L[a-1].append(b-1)
    L[b-1].append(a-1)
L[0][0] = 1
d = [0] * n
ans = [0] * n
dq = deque([0])
while len(dq) > 0:
    t = dq.popleft()
    for i in range(1, len(L[t])):
        if d[L[t][i]] == 0:
            d[L[t][i]] = d[t] + 1
            ans[L[t][i]] = t + 1
        else:
            if d[L[t][i]] > d[t] + 1:
                d[L[t][i]] = d[t] + 1
                ans[L[t][i]] = t + 1
        if L[L[t][i]][0] == 0:
            dq.append(L[t][i])
    else:
        L[t][0] = 1
print('Yes')
print(*ans[1:], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n,m = map(int,input().split())
a=[list(map(int, input().split())) for i in range(m)]
b=[[] for j in range(n)] 

michi=[-1]*(n)
michi[0]=0
heya=[]


for i in range(m):
    b[a[i][0]-1].append(a[i][1])
    b[a[i][1]-1].append(a[i][0])



heya.append(1)


while len(heya):
    now=heya.pop(0)-1
    
    for i in range(len(b[now])):
        if michi[b[now][i]-1] == -1:
            heya.append(b[now][i])
            michi[b[now][i]-1] = now+1
print("Yes")
for i in range(1,n):
    print(michi[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*n

ans[0]=1

for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])


next=[1]

while len(next):
    imaheya = next.pop(0)-1
    for j in range(len(ikeruheya[imaheya])):
        if ans[ikeruheya[imaheya][j]-1]==0:
            ans[ikeruheya[imaheya][j]-1]= imaheya + 1           
            next.append(ikeruheya[imaheya][j])
            #ikeruheya[[imaheya-1][j]-1].remove(imaheya)
        

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


#b=ikeruheya
#michi=ans
#heya=next
#now=imaheya-1

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])


next=[1]
count = 1
while len(next)!=0:
    imaheya = next.pop(0)
    for i in range(len(ikeruheya[imaheya-1])):
        if ans[ikeruheya[imaheya-1][i]]==0:
            ans[ikeruheya[imaheya-1][i]]= imaheya             
            next.append(ikeruheya[imaheya-1][i])
            #ikeruheya[[imaheya-1][i]-1].remove(imaheya)
        

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=-1
ans[1]=1
for i in range(m):
    if a[i][0]==1:
        ikeruheya[0].append(a[i][1])
    elif a[i][1]==1:
        ikeruheya[0].append(a[i][0])
    else:
        ikeruheya[a[i][0]-1].append(a[i][1])
        ikeruheya[a[i][1]-1].append(a[i][0])

#print(ikeruheya)
next=[1]
count = 1
while count < n and len(next)!=0:
    
    imaheya = next.pop(0)
    j = 0
    while j < len(ikeruheya[imaheya-1]) and count < n:
    
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya             
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            j+=1
        else:
            j+=1


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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while count < n and len(next)!=0:
    
    imaheya = next.pop(0)
    j = 0
    while j < len(ikeruheya[imaheya-1]) and count < n:
    
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya             
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            j+=1
        else:
            j+=1

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while count <= n and len(next)!=0:
    
    imaheya = next.pop(0)
    j = 0
    while j < len(ikeruheya[imaheya-1]) and count <= n:
    
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya             
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            j+=1
        else:
            j+=1

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
#check = [0]*n
#check[0]=1
ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while count <= n and len(next) != 0:
    imaheya = next.pop(0)
    j = 0
    while j < len(ikeruheya[imaheya-1]) and count <= n:
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            #check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            j+=1
        else:
            j+=1
    
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
#check = [0]*n
#check[0]=1
ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while any(ans[i] == 0 for i in range(n+1)):
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            #check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
        
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
#check = [0]*n
#check[0]=1
ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while len(next) != 0 and count <= n:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            #check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n
check[0]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while len(next) != 0 and count <= n:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n

for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])
    
next=[1]
count = 1
while len(next) != 0 and count <= n:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n

for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])
    
next=[1]
count = 1
while len(next) != 0 and count < n + 1:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n

for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])
    
next=[1]
count = 1
while len(next) != 0 and count < n + 1:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)
check = [0]*n

for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])
    
next=[1]

while len(next) != 0:
    imaheya = next.pop(0)
    for j in range(len(ikeruheya[imaheya-1])):
        if check[ikeruheya[imaheya-1][j]-1]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya 
            check[ikeruheya[imaheya-1][j]-1] = 1
            next.append(ikeruheya[imaheya-1][j])
            
print("Yes")
for i in range(2, n+1):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

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

aisles = [[] for _ in range(n)]
michi = [0] * n
num = [float('INF')] * n
loads = []
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if b < a:
        a, b = b, a
    if a == 0:
        loads.append(b)
    aisles[a].append(b)
    aisles[b].append(a)

if len(loads) == 0:
    print("No")
    exit()


def ap(ll, nm, nu):
    for l in ll:
        if num[l] > nu:
            michi[l] = nm
            num[l] = nu
            ap(aisles[l], l, nu + 1)


ap(loads, 0, 1)

print("Yes")
for mi in michi[1:]:
    print(mi + 1)

Submission status: Time Limit Exceeded

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)]
count = [[] for i in range(n)]
c=1
ans[0] = -2

for i in path[0]:
    ans[i] = 1
    count[1] += [i]
while -1 in ans:
    for i in count[c]:
        for j in path[i]:
            if ans[j]==-1:
                ans[j]=i+1
                count[c]+=[i]
        c+=1
print("Yes")
for i in range(1,n):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
lis = [0]*(n+1)
c = [[] for i in range(n+1)]
for i in range(m):
    a, b = map(int, input().split())
    c[a].append(b)
    c[b].append(a)
depth = [[1]]
i = 0
co = 1
se = set(depth[0])
while co < n:
    te = [k for j in depth[i] for k in c[j]]
    te = set(te) - se
    if len(te) == 0:
        break
    co += len(te)
    se = te | se
    depth.append(list(te))
    i += 1

    
if co != n:
    print("No")
else:
    print("Yes")
    for i in range(1, len(depth)):
        te = depth[i-1]
        for j in depth[i]:
            t = c[j]
            for k in te:
                if k in t:
                    lis[j] = k
                    break
    print("\n".join(map(str, lis[2:])))