Problem p03746 (python)

Fixed submission:
import sys
input=sys.stdin.readline

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

R=[1 for i in range(N+1)]
A,B=map(int,input().split())
R[A]=0
R[B]=0

L=[[]for i in range(N+1)]
L[A].append(B)
L[B].append(A)

for i in range(M-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)

l=[A]
P=A
#print(L)

for i in range(10**6):
    flag=0
    #print("P",L[P])
    for j in L[P]:
        #print("j",L,L[P],j,P)
        if R[j]==1:
            P=j
            R[j]=0
            l.append(j)
            flag+=1
            break
    if flag==0:
        break
#print(l)
r=[B]
P=B
for i in range(10**6):
    flag=0
    #print("P",L[P])
    for j in L[P]:
        #print("j",L,L[P],j,P)
        if R[j]==1:
            P=j
            R[j]=0
            r.append(j)
            flag+=1
            break
    if flag==0:
        break
#print(r)
l=l[::-1]
ans=l+r

ANS=str(ans[0])
for i in range(1,len(ans)):
    ANS+=" "+str(ans[i])
print(len(ans))
print(ANS)

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline

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

R=[1 for i in range(N+1)]
A,B=map(int,input().split())
R[A]=0
R[B]=0

L=[[]for i in range(N+1)]
L[A].append(B)
L[B].append(A)

for i in range(M):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)

l=[A]
P=A
#print(L)

for i in range(10**6):
    flag=0
    #print("P",L[P])
    for j in L[P]:
        #print("j",L,L[P],j,P)
        if R[j]==1:
            P=j
            R[j]=0
            l.append(j)
            flag+=1
            break
    if flag==0:
        break
#print(l)
r=[B]
P=B
for i in range(10**6):
    flag=0
    #print("P",L[P])
    for j in L[P]:
        #print("j",L,L[P],j,P)
        if R[j]==1:
            P=j
            R[j]=0
            r.append(j)
            flag+=1
            break
    if flag==0:
        break
#print(r)
l=l[::-1]
ans=l+r

ANS=str(ans[0])
for i in range(1,len(ans)):
    ANS+=" "+str(ans[i])
print(ANS)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())

a,b=map(int,input().split())
A=[a]
B=[b]

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

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

for i in range(M-1):
    x,y=map(int,input().split())
    L[x].append(y)
    L[y].append(x)
    
l[a]=0
l[b]=0


P=a
for i in range(10**5):
    for j in L[P]:
        if l[j]==0:
            pass
        else:
            P=j
            l[j]=0
            A.append(j)
            break
    if max(L[P])==0:
        break
P=b
for i in range(10**5):
    for j in L[P]:
        if l[j]==0:
            pass
        else:
            P=j
            l[j]=0
            B.append(j)
            break
    if max(L[P])==0:
        break
A=A[::-1]
ans=A+B
print(len(ans))

ANS=str(ans[0])
for i in range(1,len(ans)):
    ANS+=" "+str(ans[i])
print(ANS)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())

a,b=map(int,input().split())
A=[a]
B=[b]

L=[1 for i in range(N+1)]
L[a]=0
L[b]=0

l=[[]for i in range(N+1)]
for i in range(M-1):
    x,y=map(int,input().split())
    l[x].append(y)
    l[y].append(x)
def findA(a):
    for i in l[a]:
        if L[i]==0:
            pass
        else:
            L[i]=0
            A.append(i)
            findA(i)
    
def findB(b):
    for i in l[b]:
        if L[i]==0:
            pass
        else:
            L[i]=0
            B.append(i)
            findB(i)
findA(a)
findB(b)
#print(A)
#print(B)

A=A[::-1]
ans=A+B
print(len(ans))
ANS=str(ans[0])
for i in range(1,len(ans)):
    ANS+=" "+str(ans[i])
print(ANS)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())

a,b=map(int,input().split())
A=[a]
B=[b]

L=[1 for i in range(N+1)]
L[a]=0
L[b]=0

l=[[]for i in range(N+1)]
for i in range(M-1):
    x,y=map(int,input().split())
    l[x].append(y)
    l[y].append(x)
def findA(a):
    for i in l[a]:
        if L[i]==0:
            pass
        else:
            L[i]=0
            A.append(i)
            findA(i)
    
def findB(b):
    for i in l[b]:
        if L[i]==0:
            pass
        else:
            L[i]=0
            B.append(i)
            findB(i)
findA(a)
findB(b)
#print(A)
#print(B)

A=A[::-1]
ans=A+B
print(len(ans))
ANS=str(ans[0])
for i in range(1,len(ans)):
    ANS+=" "+str(ans[i])
print(ANS)