Problem p02678 (python)

Fixed submission:
import copy
from collections import deque
from collections import defaultdict

N,M=map(int, input().split())

line=defaultdict(list)
q=deque
next=deque()

ans=[-1]*(N+1)
ans[0]=0
ans[1]=0
for i in range(M):
  a,b=map(int, input().split())
  line[a].append(b)
  line[b].append(a)

#print(line)
    
next.append(1)
a=1
while len(next)>0:
  q=copy.copy(next)
  #print(q)
  next.clear()
  while len(q)>0:
    chk=q.popleft()
    #print('qpop',q)
    for i in range(len(line[chk])):
      #print('chk',chk)
      #print('line[chk][i]',line[chk][i])
      if ans[line[chk][i]]==-1:
        ans[line[chk][i]]=chk
        next.append(line[chk][i]) 
  if -1 not in ans:break
  #print('next',next)
#print(ans)
if -1 in ans:print('No')
else:
  print('Yes')
  for i in range(2,N+1):  
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import copy
from collections import deque
from collections import defaultdict

N,M=map(int, input().split())

line=defaultdict(list)
q=deque
next=deque()

ans=[-1]*(N+1)
ans[0]=0
ans[1]=0
for i in range(N):
  a,b=map(int, input().split())
  line[a].append(b)
  line[b].append(a)

#print(line)
    
next.append(1)
a=1
while len(next)>0:
  q=copy.copy(next)
  #print(q)
  next.clear()
  while len(q)>0:
    chk=q.popleft()
    #print('qpop',q)
    for i in range(len(line[chk])):
      #print('chk',chk)
      #print('line[chk][i]',line[chk][i])
      if ans[line[chk][i]]==-1:
        ans[line[chk][i]]=chk
        next.append(line[chk][i]) 
  if -1 not in ans:break
  #print('next',next)
#print(ans)
if -1 in ans:print('No')
else:
  print('Yes')
  for i in range(2,N+1):  
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import copy
from collections import deque
from collections import defaultdict

N,M=map(int, input().split())

line=defaultdict(list)
q=deque
next=deque()

ans=[-1]*(N+1)
ans[0]=0
ans[1]=0
for i in range(N):
  a,b=map(int, input().split())
  line[a].append(b)
  line[b].append(a)

#print(line)
    
next.append(1)
a=1
while len(next)>0:
  q=copy.copy(next)
  next.clear()
  chk=q.popleft()
  for i in range(len(line[chk])):
    #print('chk',chk)
    #print('line[chk][i]',line[chk][i])
    if ans[line[chk][i]]==-1:
      ans[line[chk][i]]=chk
      next.append(line[chk][i]) 
  if -1 not in ans:break
  #print('next',next)
#print(ans)
if -1 in ans:print('No')
else:
  print('Yes')
  for i in range(2,N+1):  
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import copy
from collections import deque
from collections import defaultdict

N,M=map(int, input().split())

line=defaultdict(list)
q=deque
next=deque()

ans=[-1]*(N+1)
ans[0]=0
ans[1]=0
for i in range(N):
  a,b=map(int, input().split())
  line[a].append(b)
  line[b].append(a)

#print(line)
    
next.append(1)
a=1
while len(next)!=0:
  q=copy.copy(next)
  next.clear()
  for i in range(len(q)):
    for j in range(len(line[q[i]])):
      if ans[line[q[i]][j]]==-1:
        ans[line[q[i]][j]]=q[i]
      for k in range(len(line[line[q[i]][j]])):
        #print('k',line[line[q[i]][j]][k])
        if ans[line[line[q[i]][j]][k]]==-1:
          #print('append',ans[line[line[q[i]][j]][k]])
          next.append(line[line[q[i]][j]][k])
      
  a+=1
  #print(next)
#print(ans)
if -1 in ans:print('No')
else:print('Yes')

for i in range(2,N+1):  
	print(ans[i])