Problem p02976 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

def dfs1(v):
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      g2[v].append(u)
      g3[v].append(u)
      g3[u].append(v)
      dfs1(u)
      
def dfs2(v):
  for u in g2[v]:
    if checked[u]==0:
      checked[u]=1
      dfs2(u)
      if cnt[u]%2==1:
        ans.append((u,v))
        cnt[u]+=1
      else:
        ans.append((v,u))
        cnt[v]+=1
      
n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
g2=[[] for _ in range(n+1)]
g3=[[] for _ in range(n+1)]
cnt=[0]*(n+1)
ans=[]
ansset=set()
for _ in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
if m%2==1:
  print(-1)
else:
  checked=[0]*(n+1)
  checked[1]=1
  dfs1(1)
  for v in range(1,n+1):
    for u in g[v]:
      if u not in g3[v]:
        if (u,v) not in ansset:
          ans.append((v,u))
          ansset.add((v,u))
          cnt[v]+=1
  checked=[0]*(n+1)
  checked[1]=1
  dfs2(1)
  for v,u in ans:
    print(v,u)

Buggy submission (verdict: Runtime Error):
def dfs1(v):
  if len(g[v])==0:
    return 0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      g2[v].append(u)
      dfs1(u)
      g3[v].append(u)
      g3[u].append(v)
      
def dfs2(v):
  if len(g2[v])==0:
    return 0
  for u in g2[v]:
    if checked[u]==0:
      checked[u]=1
      dfs2(u)
      if cnt[u]%2==1:
        ans.append((u,v))
        cnt[u]+=1
      else:
        ans.append((v,u))
        cnt[v]+=1
      
n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
g2=[[] for _ in range(n+1)]
g3=[[] for _ in range(n+1)]
g4=[[] for _ in range(n+1)]
cnt=[0]*(n+1)
ans=[]
for _ in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
if m%2==1:
  print(-1)
else:
  checked=[0]*(n+1)
  checked[1]=1
  dfs1(1)
  for v in range(1,n+1):
    for u in g[v]:
      if u not in g3[v]:
        g4[v].append(u)
  for v in range(1,n+1):
    for u in g4[v]:
      ans.append((v,u))
      cnt[v]+=1
      g4[v].remove(u)
      g4[u].remove(v)
  checked=[0]*(n+1)
  checked[1]=1
  dfs2(1)
  for v,u in ans:
    print(v,u)

Buggy submission (verdict: Runtime Error):
def dfs1(v):
  if len(g[v])==0:
    return 0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      g[u].remove(v)
      g[v].remove(u)
      g2[u].append(v)
      g2[v].append(u)
      dfs1(u)

def dfs2(v):
  if len(g2[v])==0:
    return 0
  for u in g2[v]:
    g2[u].remove(v)
    dfs2(u)
    if cnt[u]%2==1:
      print(u,v)
      cnt[u]+=1
    else:
      print(v,u)
      cnt[v]+=1

n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
g2=[[] for _ in range(n+1)]
cnt=[0]*(n+1)
for _ in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
if m%2==1:
  print(-1)
else:
  checked=[0]*(n+1)
  checked[1]=1
  dfs1(1)
  for v in range(1,n+1):
    if len(g[v])==0:
      continue
    else:
      for u in g[v]:
        print(u,v)
        g[u].remove(v)
        g[v].remove(u)
        cnt[u]+=1
  dfs2(1)

Buggy submission (verdict: Runtime Error):
import collections

def printCircuit(v):
  edge_count=[0]*(n+1)
  for i in range(1,n+1):
    edge_count[i]=len(g[i])
  curr_path=collections.deque()
  circuit=[]
  curr_path.appendleft(v)
  curr_v=v
  while len(curr_path)!=0:
    if edge_count[curr_v]:
      curr_path.appendleft(curr_v)
      next_v=g[curr_v].pop(-1)
      g[next_v].remove(curr_v)
      edge_count[curr_v]-=1
      edge_count[next_v]-=1
      curr_v=next_v
    else:
      circuit.append(curr_v)
      curr_v=curr_path.popleft()
  return circuit

n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
cnt=[0]*(n+1)
for _ in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
  cnt[a]+=1
  cnt[b]+=1
cnt1=0
cnt2=0
cand=[]
for i in range(1,n+1):
  if cnt[i]%2==0:
    cnt1+=1
  else:
    cnt2+=1
    cand.append(i)
if m%2==1:
  print(-1)
  else:
  if cnt1==n:
    path=printCircuit(1)
    if len(path)%2==1:
      for i in range(len(path)):
        if i%2==1:
          print(path[i],path[i-1])
          print(path[i],path[i+1])
    else:
      print(-1)
  elif cnt1==n-2 and cnt2==2:
    if len(g[cand[0]])==1:
      c=cand[0]
    else:
      c=cand[1]
    path=printCircuit(c)
    if len(path)%2==1:
      for i in range(len(path)):
        if i%2==1:
          print(path[i],path[i-1])
          print(path[i],path[i+1])
    else:
      print(-1)
  else:
    print(-1)