Problem p02678 (python)

Fixed submission:

from collections import deque

N, M = map(int, input().split())
AB = [map(int, input().split()) for _ in range(M)]

links = [[] for _ in range(N + 1)]
for a, b in AB:
    links[a].append(b)
    links[b].append(a)

result = [-1] * (N + 1)
q = deque([1])
while q:
    i = q.popleft()
    for j in links[i]:
        if result[j] == -1:
            result[j] = i
            q.append(j)
print('Yes')
print('\n'.join(str(i) for i in result[2:]))

Buggy submission (verdict: Runtime Error):
num_room, num_path = map(int, input().split())

room_paths_dict = {}

start_room_to_paths = set()
result = {}

def solve():
    for i in range(num_path):
        a_room_num, b_room_num = map(int, input().split())
        if a_room_num == 1:
            start_room_to_paths.add(b_room_num)
            continue
        elif b_room_num == 1:
            start_room_to_paths.add(a_room_num)
            continue

        result[a_room_num] = 0
        result[b_room_num] = 0

        if not room_paths_dict.get(a_room_num):
            room_paths_dict[a_room_num] = set()
        if not room_paths_dict.get(b_room_num):
            room_paths_dict[b_room_num] = set()

        room_paths_dict[a_room_num].add(b_room_num)
        room_paths_dict[b_room_num].add(a_room_num)
    if len(start_room_to_paths) == 0:
        print('No')
        return

    def search(room_num):
        targets = []
        for next_room in room_paths_dict[room_num]:
            if result[next_room] == 0:
                result[next_room] = room_num
                targets.append(room_num)

        for t in targets:
            search(t)

    for start_room_to_path in start_room_to_paths:
        result[start_room_to_path] = 1
        search(start_room_to_path)

    if 0 in result.values():
        print('No')
    else:
        print('Yes')
        for key in sorted(result.keys()):
            print(result[key])

solve()

Buggy submission (verdict: Runtime Error):
num_room, num_path = map(int, input().split())

room_paths_dict = {}

start_room_to_paths = set()
result = {}

for i in range(num_path):
    a_room_num, b_room_num = map(int, input().split())
    if a_room_num == 1:
        start_room_to_paths.add(b_room_num)
        continue
    elif b_room_num == 1:
        start_room_to_paths.add(a_room_num)
        continue

    result[a_room_num] = 0
    result[b_room_num] = 0

    if not room_paths_dict.get(a_room_num):
        room_paths_dict[a_room_num] = set()
    if not room_paths_dict.get(b_room_num):
        room_paths_dict[b_room_num] = set()

    room_paths_dict[a_room_num].add(b_room_num)
    room_paths_dict[b_room_num].add(a_room_num)

def search(room_num):
    targets = []
    for next_room in room_paths_dict[room_num]:
        if result[next_room] == 0:
            result[next_room] = room_num
            targets.append(room_num)

    for t in targets:
        search(t)

for start_room_to_path in start_room_to_paths:
    result[start_room_to_path] = 1
    search(start_room_to_path)

if 0 in result.values():
    print('No')
else:
    print('Yes')
    for key in sorted(result.keys()):
        print(result[key])

Buggy submission (verdict: Runtime Error):
num_room, num_path = map(int, input().split())

room_paths_dict = {}

start_room_to_paths = []
result = {}

for i in range(num_path):
    a_room_num, b_room_num = map(int, input().split())
    if a_room_num == 1:
        start_room_to_paths.append(b_room_num)
        continue
    elif b_room_num == 1:
        start_room_to_paths.append(a_room_num)
        continue

    result[a_room_num] = 0
    result[b_room_num] = 0

    if not room_paths_dict.get(a_room_num):
        room_paths_dict[a_room_num] = set()
    if not room_paths_dict.get(b_room_num):
        room_paths_dict[b_room_num] = set()

    room_paths_dict[a_room_num].add(b_room_num)
    room_paths_dict[b_room_num].add(a_room_num)

def search(room_num):
    targets = []
    for next_room in room_paths_dict[room_num]:
        if result[next_room] == 0:
            result[next_room] = room_num
            targets.append(room_num)

    for t in targets:
        search(t)

for start_room_to_path in start_room_to_paths:
    result[start_room_to_path] = 1
    search(start_room_to_path)

if 0 in result.values():
    print('No')
else:
    print('Yes')
    for key in sorted(result.keys()):
        print(result[key])