Problem p02678 (python)

Fixed submission:
MAX_INT = 10**6
N, M = map(int, input().split())
room = [set() for _ in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    room[a].add(b)
    room[b].add(a)

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
def sign_root(n):
    nxt = []
    for i in room[n]:
        if dis[n] + 1 < dis[i]:
            sign[i] = n
            dis[i] = dis[n] + 1
            nxt += [i]
    return nxt

nxt = sign_root(1)
while nxt != []:
    for i in nxt.copy():
        nxt.pop(0)
        nxt += sign_root(i)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
MAX_INT = 10**6
N, M = map(int, input().split())
room = [set() for _ in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    room[a].add(b)
    room[b].add(a)

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
def sign_root(n):
    for i in room[n]:
        if dis[n] + 1 < dis[i] and n in room[i]:
            sign[i] = n
            dis[i] = dis[n] + 1
            room[i].remove(n)
            sign_root(i)
sign_root(1)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
MAX_INT = 10**6
N, M = map(int, input().split())
room = [set() for _ in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    room[a].add(b)
    room[b].add(a)

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
aa = [False] * (N + 1)
def sign_root(n):
    if not aa[n]:
        for i in room[n]:
            if dis[n] + 1 < dis[i]:
                sign[i] = n
                dis[i] = dis[n] + 1
                sign_root(i)
    aa[n] = True
sign_root(1)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
MAX_INT = 10**6
N, M = map(int, input().split())
room = [set() for _ in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    room[a].add(b)
    room[b].add(a)

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
def sign_root(n):
    for i in room[n]:
        if dis[n] + 1 < dis[i]:
            sign[i] = n
            dis[i] = dis[n] + 1
            sign_root(i)
sign_root(1)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
MAX_INT = 10**6
N, M = map(int, input().split())
room = dict()
for i in range(1, N+1):
    room[i] = set()
for i in range(M):
    a, b = map(int, input().split())
    room[a].add(b)
    room[b].add(a)

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
def sign_root(n):
    
    for i in room[n]:
        if dis[n] + 1 < dis[i]:
            sign[i] = n
            dis[i] = dis[n] + 1
            sign_root(i)
sign_root(1)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
MAX_INT = 10**6
N, M = map(int, input().split())
room = dict()
for i in range(M):
    a, b = map(int, input().split())
    if room.get(a):
        room[a] += [b]
    else:
        room[a] = [b]
    if room.get(b):
        room[b] += [a]
    else:
        room[b] = [a]

dis = [MAX_INT] * (N + 1)
sign = [-1] * (N + 1)
dis[1] = 0
def sign_root(n):
    nxt = []
    for i in room[n]:
        if dis[n] + 1 < dis[i]:
            sign[i] = n
            dis[i] = dis[n] + 1
            sign_root(i)
sign_root(1)
if MAX_INT in dis[2:]:
    print("No")
else:
    print("Yes")
    for i in sign[2:]:
        print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
room = dict()
for i in range(M):
    a, b = map(int, input().split())
    if room.get(a):
        room[a] += [b]
    else:
        room[a] = [b]
    if room.get(b):
        room[b] += [a]
    else:
        room[b] = [a]
dp = [-1] * (N + 1)
dp[1] = 0
def sign_root(n):
    nxt = []
    for i in room[n]:
        if dp[i] == -1:
            dp[i] = n
            nxt += [i]
    for i in nxt:
        sign_root(i)
sign_root(1)
if -1 in dp[1:]:
    print("No")
else:
    print("Yes")
    for i in dp[2:]:
        print(i)