Problem p03746 (python)

Fixed submission:
N,M=map(int,input().split())
from collections import deque,defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a-1]|={b-1}
    branch[b-1]|={a-1}
path=deque([0])
for x in branch[0]:
    path.append(x)
    break
used=set(path)
while True:
    A,B=False,False
    if len(branch[path[0]]-used)==0:
        A=True
    else:
        for x in branch[path[0]]:
            if x in used:
                continue
            path.appendleft(x)
            used|={x}
            break
    if len(branch[path[-1]]-used)==0:
        B=True
    else:
        for x in branch[path[-1]]:
            if x in used:
                continue
            path.append(x)
            used|={x}
            break
    if A and B:
        break
path=[p+1 for p in path]
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
def dfs1(check,visited,P):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
def dfs2(check,visited,P,goal):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if nex==goal:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
if jisuu.count(1)>=2:
    exit()
    start=jisuu.index(1)
    visited={start}
    check={start}
    P=[0]*(N+1)
    P[start]=[start]
    path=dfs1(check,visited,P)
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check={nex}
    P=[0]*(N+1)
    P[start]=[start]
    P[nex]=[start,nex]
    path=dfs2(check,visited,P,goal)
    print(1)
    exit()
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
def dfs1(check,visited,P):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
def dfs2(check,visited,P,goal):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if nex==goal:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check={start}
    P=[0]*(N+1)
    P[start]=[start]
    path=dfs1(check,visited,P)
    print(1)
    exit()
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check={nex}
    P=[0]*(N+1)
    P[start]=[start]
    P[nex]=[start,nex]
    path=dfs2(check,visited,P,goal)
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
print(1)
def dfs1(check,visited,P):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
def dfs2(check,visited,P,goal):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if nex==goal:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check={start}
    P=[0]*(N+1)
    P[start]=[start]
    path=dfs1(check,visited,P)
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check={nex}
    P=[0]*(N+1)
    P[start]=[start]
    P[nex]=[start,nex]
    path=dfs2(check,visited,P,goal)
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
def dfs1(check,visited,P):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
def dfs2(check,visited,P,goal):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if nex==goal:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check={start}
    P=[0]*(N+1)
    P[start]=[start]
    path=dfs1(check,visited,P)
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check={nex}
    P=[0]*(N+1)
    P[start]=[start]
    P[nex]=[start,nex]
    path=dfs2(check,visited,P,goal)
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
def dfs1(check,visited,P):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
def dfs2(check,visited,P,goal):
    while len(check)>0:
        now=check.pop()
        for nex in branch[now]:
            if nex not in visited:
                if nex==goal:
                    return P[now]+[nex]
                else:
                    check|={nex}
                    visited|={nex}
                    P[nex]=P[now]+[nex]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check={start}
    P=[0]*(N+1)
    P[start]=[start]
    path=dfs1(check,visited,P)
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check={nex}
    P=[0]*(N+1)
    P[start]=[start]
    P[nex]=[start,nex]
    path=dfs2(cehck,visited,P,goal)
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
path=[0]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check=[[start]]
    while len(check)>0:
        c=check.pop()
        now=c[-1]
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    path=c+[nex]
                    check=[]
                    break
                else:
                    check.append(c+[nex])
                    visited|={nex}
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check=[[start,nex]]
    while len(check)>0:
        c=check.pop()
        now=c[-1]
        for nex in  branch[now]:
            if nex not in visited:
                if nex==goal:
                    path=c+[nex]
                    check=[]
                    break
                else:
                    check.append(c+[nex])
                    visited|={nex}
print(len(path))
print(" ".join(map(str,path)))

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
from collections import defaultdict
branch=defaultdict(set)
for i in range(M):
    a,b=map(int,input().split())
    branch[a]|={b}
    branch[b]|={a}
jisuu=[len(branch[i]) for i in range(N+1)]
if jisuu.count(1)>=2:
    start=jisuu.index(1)
    visited={start}
    check=[[start]]
    while len(check)>0:
        c=check.pop()
        now=c[-1]
        for nex in branch[now]:
            if nex not in visited:
                if jisuu[nex]==1:
                    path=c+[nex]
                    check=[]
                    break
                else:
                    check+=[c+[nex]]
                    visited|={nex}
else:
    for i in range(1,1+N):
        if jisuu[i]==2:
            a,b=list(branch[i])
            if jisuu[a]==2:
                start=i
                goal=a
                nex=b
                break
            elif jisuu[b]==2:
                start=i
                goal=b
                nex=a
                break
    visited={start,nex}
    check=[[start,nex]]
    while len(check)>0:
        c=check.pop()
        now=c[-1]
        for nex in  branch[now]:
            if nex not in visited:
                if nex==goal:
                    path=c+[nex]
                    check=[]
                    break
                else:
                    check+=[c+[nex]]
                    visited|={nex}
print(len(path))
print(" ".join(map(str,path)))