Problem p02678 (python)

Fixed submission:
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"

for i in list(connect_list[0]):
    root_list[i-1] = 1
now_set = connect_list[0]
while len(now_set) != 0:
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                now_set.add(i)
        now_set.remove(j)

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"

for i in list(connect_list[0]):
    root_list[i-1] = 1
now_set = connect_list[0]

def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for m in list(connect_list[j-1]):
            if root_list[m-1] == 0:
                root_list[m-1] = j
                # next_set.add(i)
                now_set.add(m)
        now_set.remove(j)
    do_ranking(now_set)


do_ranking(now_set)   #道しるべ作成

for k in root_list:
    print(k)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for m in list(connect_list[j-1]):
            if root_list[m-1] == 0:
                root_list[m-1] = j
                # next_set.add(i)
                now_set.add(m)
        now_set.remove(j)
    do_ranking(now_set)


for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for k in root_list:
    print(k)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
connect_list = [set() for _ in range(N)]    #繋がっているリスト
for _ in range(M):
    A, B = map(int, input().split())
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                # next_set.add(i)
                now_set.add(i)
        now_set.remove(j)
    do_ranking(now_set)


# for A, B in AB:
#     connect_list[A-1].add(B)
#     connect_list[B-1].add(A)

for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    # next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                # next_set.add(i)
                now_set.add(i)
        now_set.remove(j)
    do_ranking(now_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)

for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
    
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    if i - 1 < N:
        root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        if j-1 >= N:
            break
        for i in list(connect_list[j-1]):
            if i-1 >= N:
                break
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]    #繋がっているリスト
ranking = [set() for _ in range(N)]     #入口に近い順
ranked_set = set()
full_set = {i for i in range(1, N+1)}
root_list = [0]*N
root_list[0] = "Yes"


def do_ranking(now_set):
    global connect_list, root_list
    if len(now_set) == 0:
        return 0
    next_set = set()
    for j in list(now_set):
        for i in list(connect_list[j-1]):
            if root_list[i-1] == 0:
                root_list[i-1] = j
                next_set.add(i)
    do_ranking(next_set)


for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
for i in list(connect_list[0]):
    root_list[i-1] = 1
do_ranking(connect_list[0])   #道しるべ作成

for i in root_list:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]
ranking = [set() for _ in range(N)]
ranked_set = set()
full_set = {i for i in range(1, N+1)}


def do_ranking(set, rank):
    global connect_list, ranking, ranked_set, full_set
    if ranked_set == full_set:
        return 0
    for j in list(set):
        for i in list(connect_list[j-1]):
            if i not in ranked_set:
                ranking[rank].add(i)
                ranked_set.add(i)
    do_ranking(ranking[rank], rank+1)



for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
ranking[0] = connect_list[0]
ranked_set = ranked_set | ranking[0]
ranked_set.add(1)
do_ranking(ranking[0], 1)

root_list = [0]*N
root_list[0] = "Yes"
count = 0
no_flag = False
for i in range(1, N):
    flag = True
    j = 0
    while flag:
        if len(ranking[j]) == 0:
            no_flag = True
            break
        seki = connect_list[i] & ranking[j]
        if 1 in connect_list[i]:
            root_list[i] = 1
            flag = False
        elif len(seki) != 0:
            root_list[i] = list(seki)[0]
            flag = False
        else:
            j += 1
    if no_flag:
        print("No")
        break
if no_flag == False:
    for i in root_list:
        print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]

connect_list = [set() for _ in range(N)]
ranking = [set() for _ in range(N)]
ranked_set = set()
full_set = {i for i in range(1, N+1)}


def do_ranking(set, rank):
    global connect_list, ranking, ranked_set, full_set
    if ranked_set == full_set:
        return 0
    for j in list(set):
        for i in list(connect_list[j-1]):
            if i not in ranked_set:
                ranking[rank].add(i)
                ranked_set.add(i)
    do_ranking(ranking[rank], rank+1)



for A, B in AB:
    connect_list[A-1].add(B)
    connect_list[B-1].add(A)
ranking[0] = connect_list[0]
ranked_set = ranked_set | ranking[0]
ranked_set.add(1)
do_ranking(ranking[0], 1)

root_list = [0]*N
root_list[0] = "Yes"
count = 0
no_flag = False
for i in range(1, N):
    flag = True
    j = 0
    while flag:
        if len(ranking[j]) == 0:
            no_flag = True
            break
        seki = connect_list[i] & ranking[j]
        if 1 in connect_list[i]:
            root_list[i] = 1
            flag = False
        elif len(seki) != 0:
            print(seki)
            root_list[i] = list(seki)[0]
            flag = False
        else:
            j += 1
    if no_flag:
        print("No")
        break
if no_flag == False:
    for i in root_list:
        print(i)