Problem p02678 (python)

Fixed submission:

def get_input():
    N, M =  list(map(int, input().split()))
    routes = {}
    for i in range(M):
        a, b =  list(map(int, input().split()))
        a_idx = a - 1
        b_idx = b - 1
        if a_idx in routes:
            routes[a_idx].append(b_idx)
        else:
            routes[a_idx] = [b_idx]
        
        if b_idx in routes:
            routes[b_idx].append(a_idx)
        else:
            routes[b_idx] = [a_idx]

    return N, M, routes

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


marks = {0:0}

N, M, routes = get_input()
mark = {0:0}
start_nodes = [0]
while start_nodes:
    next_nodes = []
    for i in start_nodes:
        for n in routes[i]:
            if n in marks:
                continue
            marks[n] = i
            next_nodes.append(n)
    start_nodes = next_nodes   
write_result(marks, N)

Buggy submission (verdict: Runtime Error):
# セル3(変数のリセット)
input_count = 0

def func(i, routes, marks):
    update_rooms = []
    if i not in routes:
        return marks
    for j in routes[i]:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        update_rooms.append(room_idx)
    for j in update_rooms:
        marks = func(j, routes, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    routes = {}
    for i in range(M):
        a, b =  list(map(int, input().split()))
        a_idx = a - 1
        b_idx = b - 1
        if a_idx in routes:
            routes[a_idx].append(b_idx)
        else:
            routes[a_idx] = [b_idx]
        
        if b_idx in routes:
            routes[b_idx].append(a_idx)
        else:
            routes[b_idx] = [a_idx]

    return N, M, routes

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


marks = {0:0}

N, M, routes = get_input()
marks = func(0, routes, {0: 0})
try:
    write_result(marks, N)
except:
    pass

Buggy submission (verdict: Runtime Error):


def func(i, routes, marks):
    update_rooms = []
    if i not in routes:
        return marks
    for j in routes[i]:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        update_rooms.append(room_idx)
    for j in update_rooms:
        marks = func(j, routes, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    routes = {}
    for i in range(M):
        a, b =  list(map(int, input().split()))
        a_idx = a - 1
        b_idx = b - 1
        if a_idx in routes:
            routes[a_idx].append(b_idx)
        else:
            routes[a_idx] = [b_idx]
        
        if b_idx in routes:
            routes[b_idx].append(a_idx)
        else:
            routes[b_idx] = [a_idx]

    return N, M, routes

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


marks = {0:0}

N, M, routes = get_input()
    marks = func(0, routes, {0: 0})
try:
    write_result(marks, N)
except:
    pass

Buggy submission (verdict: Runtime Error):

def func(i, routes, marks):
    update_rooms = []
    if i not in routes:
        return marks
    for j in routes[i]:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        update_rooms.append(room_idx)
    for j in update_rooms:
        marks = func(j, routes, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    routes = {}
    for i in range(M):
        a, b =  list(map(int, input().split()))
        a_idx = a - 1
        b_idx = b - 1
        if a_idx in routes:
            routes[a_idx].append(b_idx)
        else:
            routes[a_idx] = [b_idx]
        
        if b_idx in routes:
            routes[b_idx].append(a_idx)
        else:
            routes[b_idx] = [a_idx]

    return N, M, routes

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy

N, M, routes = get_input()

marks = {0:0}

marks = func(0, routes, {0: 0})
write_result(marks, N)

Buggy submission (verdict: Runtime Error):
# セル3(変数のリセット)
input_count = 0

def func(i, d, marks):
    rooms = numpy.nonzero(d[i])[0]
    new_room = []
    if rooms.size == 0:
        return marks
    for j in rooms:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        new_room.append(room_idx)
    for j in new_room:
        marks = func(j, d, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    d = numpy.zeros((N,N))
    for i in range(M):
        a, b =  list(map(int, input().split()))
        d[a-1, b-1] = 1
        d[b-1, a-1] = 1
    return N, M, d

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


N, M, d = get_input()

marks = {0:0}
try:
    marks = func(0, d, {0: 0})
    write_result(marks, N)
except:
    pass

Buggy submission (verdict: Runtime Error):

def func(i, d, marks):
    rooms = numpy.nonzero(d[i])[0]
    new_room = []
    if rooms.size == 0:
        return marks
    for j in rooms:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        new_room.append(room_idx)
    for j in new_room:
        marks = func(j, d, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    d = numpy.zeros((N,M))
    for i in range(M):
        a, b =  list(map(int, input().split()))
        d[a-1, b-1] = 1
        d[b-1, a-1] = 1
    return N, M, d

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


N, M, d = get_input()

marks = {0:0}
try:
    marks = func(0, d, {0: 0})
    write_result(marks, N)
except:
    pass

Buggy submission (verdict: Runtime Error):
# セル3(変数のリセット)
input_count = 0

def func(i, d, marks):
    rooms = numpy.nonzero(d[i])[0]
    new_room = []
    if rooms.size == 0:
        return marks
    for j in rooms:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        new_room.append(room_idx)
    for j in new_room:
        marks = func(j, d, marks)
    return marks

def get_input():
    N, M =  list(map(int, input().split()))
    d = numpy.zeros((N,M))
    for i in range(M):
        a, b =  list(map(int, input().split()))
        d[a-1, b-1] = 1
        d[b-1, a-1] = 1
    return N, M, d

def write_result(marks, N):
    flag = True
    output_str = ''
    for i in range(1, N):
        if i not in marks:
            flag = False
            break
        output_str += f'{marks[i]+1}\n'
    if flag:
        print('Yes')    
        print(output_str)
    else:
        print('No')

import numpy


N, M, d = get_input()

marks = {0:0}
try:
    marks = func(0, d, {0: 0})
except:
    pass

write_result(marks, N)

Buggy submission (verdict: Runtime Error):
# セル3(変数のリセット)
input_count = 0

def func(i, d, marks):
    rooms = numpy.nonzero(d[i])[0]
    new_room = []
    if rooms.size == 0:
        return marks
    for j in rooms:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        new_room.append(room_idx)
    for j in new_room:
        marks = func(j, d, marks)
    return marks

import numpy

N, M =  list(map(int, input().split()))
d = numpy.zeros((N,M))
for i in range(M):
    a, b =  list(map(int, input().split()))
    d[a-1, b-1] = 1
    d[b-1, a-1] = 1

marks = func(0, d, {0: 0})

flag = True
output_str = ''
for i in range(1, N):
    if i not in marks:
        flag = False
        break
    output_str += f'{marks[i]+1}\n'
if flag:
    print('Yes')    
    print(output_str)
else:
    print('No')

Buggy submission (verdict: Runtime Error):

def func(i, d, marks):
    rooms = numpy.nonzero(d[i])
    new_room = []
    for j in rooms[0]:
        room_idx = j
        if room_idx in marks:
            continue
        marks[room_idx] = i
        new_room.append(room_idx)
    for j in new_room:
        marks = func(j, d, marks)
    return marks

import numpy

N, M =  list(map(int, input().split()))
d = numpy.zeros((N,M))
for i in range(M):
    a, b =  list(map(int, input().split()))
    d[a-1, b-1] = 1
    d[b-1, a-1] = 1

marks = func(0, d, {0: 0})

flag = True
output_str = ''
for i in range(1, N):
    if i not in marks:
        flag = False
        break
    output_str += f'{marks[i]+1}\n'
if flag:
    print('Yes')    
    print(output_str)
else:
    print('No')