Problem p02678 (python)

Fixed submission:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(M)]
 
l = [[] for i in range(N + 1)]
 
for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    l[a].append(b)
    l[b].append(a)
 
point = [0] * (N + 1)
search = [0] * (N + 1)
search[1] = 1

que = []
que.append(1)
p = 0

while len(que) != N:
    now = que[p]
    for i in range(len(l[now])):
        n = l[now][i]
        if search[n] == 0:
            search[n] = 1
            que.append(n)
            point[n] = now
    p += 1
    
print('Yes')
for i in range(2,N+1):
    print(point[i])

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

l = [[] for i in range(N + 1)]

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    l[a].append(b)
    l[b].append(a)

p = [-1] * (N + 1)
c = [-1] * (N + 1)
c[1] = 0


def dfs(i):
    if len(l[i]) == 0:
        return
    for j in range(len(l[i])):
        point = l[i][j]
        if c[point] == -1 or c[point] > c[i] + 1:
            p[point] = i
            c[point] = c[i] + 1
            dfs(point)
        else:
            continue


dfs(1)
flag = 1

for i in range(2, N + 1):
    if c[i] == -1:
        flag = 0
if flag == 0:
    print('No')
else:
    print('Yes')
    for i in range(2, N + 1):
        print(p[i])

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

l = [[] for i in range(N + 1)]

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    l[a].append(b)
    l[b].append(a)

p = [-1] * (N + 1)
c = [-1] * (N + 1)
c[1] = 0


def dfs(i):
    if len(l[i]) == 0:
        return
    for j in range(len(l[i])):
        point = l[i][j]
        if c[point] == -1 or c[point] > c[i] + 1:
            p[point] = i
            c[point] = c[i] + 1
            dfs(point)


dfs(1)
flag = 1

for i in range(2, N + 1):
    if c[i] == -1:
        flag = 0
if flag == 0:
    print('No')
else:
    print('Yes')
    for i in range(2, N + 1):
        print(p[i])

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

l = [[] for i in range(N + 1)]

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    l[a].append(b)
    l[b].append(a)

p = [-1] * (N + 1)
c = [-1] * (N + 1)
c[1] = 0


def dfs(i):
    if len(l[i]) == 0:
        return
    for j in range(len(l[i])):
        point = l[i][j]
        if c[point] == -1 or c[point] > c[i] + 1:
            p[point] = i
            c[point] = c[i] + 1
            dfs(point)


dfs(1)
flag = 1
for i in range(2, N + 1):
    if c[i] == -1:
        flag = 0
if flag == 0:
    print('No')
else:
    print('Yes')
    for i in range(2, N + 1):
        print(p[i])

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

l = [[] for i in range(N + 1)]

for i in range(M):
    a = AB[i][0]
    b = AB[i][1]
    l[a].append(b)
    l[b].append(a)

p = [[] for i in range(N + 1)]
c = [-1] * (N + 1)
c[1] = 0


def dfs(i):
    for j in range(len(l[i])):
        if c[l[i][j]] == -1:
            p[l[i][j]] = i
            c[l[i][j]] = c[i] + 1
            dfs(l[i][j])
        elif c[l[i][j]] > c[i] + 1:
            p[l[i][j]] = i
            c[l[i][j]] = c[i] + 1
            dfs(l[i][j])


dfs(1)
flag = 1
for i in range(2, N + 1):
    if c[i] == -1:
        flag = 0
if flag == 0:
    print('No')
else:
    print('Yes')
    for i in range(2, N + 1):
        print(p[i])