Problem p02976 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100001)

n, m = map(int, input().split())
edges = [[] for _ in range(n)]
visited = [False] * n
for _ in range(m):
    a, b = map(int, input().split())
    la = len(edges[a - 1])
    lb = len(edges[b - 1])
    edges[a - 1].append([b - 1, lb])
    edges[b - 1].append([a - 1, la])

def dfs(p, v):
    visited[v] = True
    for (i, r) in edges[v]:
        if not visited[i] and r != -1 and i != p:
            dfs(v, i)
    odd = False
    pr = -1
    for (i, r) in edges[v]:
        if i != p:
            if r != -1:
                edges[i][r][1] = -1
                odd = not odd
        else:
            pr = r
    if odd:
        edges[p][pr][1] = -1

if m % 2 == 1:
    print(-1)
else:
    dfs(-1, 0)
    for v in range(n):
        for (i, r) in edges[v]:
            if r >= 0:
                print(v + 1, i + 1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100001)

n, m = map(int, input().split())
edges = [[] for _ in range(n)]
visited = [False] * n
for _ in range(m):
    a, b = map(int, input().split())
    la = len(edges[a - 1])
    lb = len(edges[b - 1])
    edges[a - 1].append([b - 1, lb])
    edges[b - 1].append([a - 1, la])

def dfs(p, v):
    visited[v] = True
    for (i, r) in edges[v]:
        if not visited[i] and r != -1 and i != p:
            dfs(v, i)
    odd = False
    pr = -1
    for (i, r) in edges[v]:
        if i != p:
            if r != -1:
                print(v + 1, i + 1)
                edges[i][r][1] = -1
		odd = not odd
        else:
            pr = r
    if odd:
        print(v + 1, p + 1)
        edges[p][pr][1] = -1

if m % 2 == 1:
    print(-1)
else:
    dfs(-1, 0)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100001)

n, m = map(int, input().split())
edges = [[] for _ in range(n)]
visited = [False] * n
for _ in range(m):
    a, b = map(int, input().split())
    la = len(edges[a - 1])
    lb = len(edges[a - 1])
    edges[a - 1].append([b - 1, lb])
    edges[b - 1].append([a - 1, la])

def dfs(p, v):
    if visited[v]:
        return
    visited[v] = True
    for (i, r) in edges[v]:
        if i != p and r != -1:
            dfs(v, i)
    count = 0
    pr = -1
    for (i, r) in edges[v]:
        if i != p and r != -1:
            print(v + 1, i + 1)
            edges[i][r][1] = -1
            count += 1
        if i == p:
            pr = r
    if count % 2 == 1:
        print(v + 1, p + 1)
        edges[p][pr][1] = -1

if m % 2 == 1:
    print(-1)
else:
    dfs(-1, 0)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
edges = [set() for _ in range(n)]
visited = [False for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].add(b - 1)
    edges[b - 1].add(a - 1)

def dfs(p, v):
    if visited[v]:
        return
    visited[v] = True
    vi = list(edges[v])
    for i in vi:
        if i != p:
            dfs(v, i)
    count = 0
    for i in edges[v]:
        if i != p:
            print(v + 1, i + 1)
            edges[i].remove(v)
            count += 1
    if count % 2 == 1:
        print(v + 1, p + 1)
        edges[p].remove(v)

if m % 2 == 1:
    print(-1)
else:
    dfs(-1, 0)