Problem p02678 (python)

Fixed submission:
N, M = map(int, input().split())
A, B = [], []
dic = {i:[] for i in range(1, N+1)}
for _ in range(M):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)

node = [float('inf')] * N
node[0] = 0 #1までの距離
route = [0]*N #ランプ置く部屋
rooms = [1] #次調べる部屋
st = {1}

for r1 in rooms:
    for r2 in dic[r1]:
        if node[r1-1] + 1 <= node[r2-1]:
            node[r2-1] = node[r1-1] + 1
            route[r2-1] = r1
            if not r2 in st:
                rooms.append(r2)
                st.add(r2)
            try:
                dic[r2].remove(r1)
            except:
                continue

if float('inf') in node:
    print('No')
else:
    print('Yes')
    for i in route[1:]:
        print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A, B = [], []
dic = {i:[] for i in range(1, N+1)}
for _ in range(M):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)

node = [float('inf')] * N
node[0] = 0 #1までの距離
route = [0]*N #ランプ置く部屋
rooms = [1] #次調べる部屋
st = (1)

for r1 in rooms:
    for r2 in dic[r1]:
        if node[r1-1] + 1 <= node[r2-1]:
            node[r2-1] = node[r1-1] + 1
            route[r2-1] = r1
            if not r2 in st:
                rooms.append(r2)
                st.add(r2)
            try:
                dic[r2].remove(r1)
            except:
                continue

if float('inf') in node:
    print('No')
else:
    print('Yes')
    for i in route[1:]:
        print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A, B = [], []
dic = {i:[] for i in range(1, N+1)}
for _ in range(M):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)

node = [float('inf')] * N
node[0] = 0 #1までの距離
route = [0]*N #ランプ置く部屋
rooms = [1] #次調べる部屋

for r1 in rooms:
    for r2 in dic[r1]:
        if node[r1-1] + 1 <= node[r2-1]:
            node[r2-1] = node[r1-1] + 1
            route[r2-1] = r1
            rooms.append(r2)
            dic[r2].remove(r1)

if float('inf') in node:
    print('No')
else:
    print('Yes')
    for i in route[1:]:
        print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

dic = {i:set() for i in range(1, N+1)}

for _ in range(M):
    a, b = map(int, input().split())
    dic[a].add(b)
    dic[b].add(a)


l1 = [float('inf')]*N
l1[0] = 0
ans = [0] * N
x = {1}

k1 = 1
rooms = dic[k1]

def search(k1, dic, l1, ans, x):

    if x == {i for i in range(1, N)}:
        return

    rooms = dic[k1]
    for room in rooms:
        x.add(room)
        
        if l1[k1-1] + 1 < l1[room-1]:
            l1[room-1] = l1[k1-1] + 1
            ans[room-1] = k1
            search(room, dic, l1, ans, x)



search(k1, dic, l1, ans, x)

if float('inf') in l1:
    print('No')
else:
    print('Yes')
    for n in ans[1:]:
        print(n)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

dic = {i:set() for i in range(1, N+1)}

for _ in range(M):
    a, b = map(int, input().split())
    dic[a].add(b)
    dic[b].add(a)


l1 = [float('inf')]*N
l1[0] = 0
ans = [0] * N
x = {1}

k1 = 1
rooms = dic[k1]

def search(k1, dic, l1, ans, x):

    if x == {i for i in range(1, N)}:
        return

    rooms = dic[k1]
    for room in rooms:
        x.add(room)
        print(room)
        if l1[k1-1] + 1 < l1[room-1]:
            l1[room-1] = l1[k1-1] + 1
            ans[room-1] = k1
            search(room, dic, l1, ans, x)



search(k1, dic, l1, ans, x)

if float('inf') in l1:
    print('No')
else:
    print('Yes')
    for n in ans[1:]:
        print(n)