Problem p02976 (python)

Fixed submission:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)
readline = sys.stdin.readline
def solve(p, v, rs):
    als = G[v]
    cs = g[v]
    c = 0
    for u in als:
        if u in cs:
            c += solve(v, u, rs)
        elif u != p:
            if v < u:
                c += 1
            rs.add((min(u,v), max(u,v)))
    if p == None:
        return
    elif c%2 != 0:
        rs.add((v, p))
        return 0
    else:
        rs.add((p,v))
        return 1

N, M = map(int, readline().split())
if M %2 != 0:
    print(-1)
else:
    G = defaultdict(set)
    for v in range(M):
        a, b = map(int, readline().split())
        G[a].add(b)
        G[b].add(a)

    stack=[1]
    vs = set([1])
    g = defaultdict(set)
    counter = 1
    while True:
        v = stack.pop()
        for u in G[v]:
            if u in vs:
                continue
            vs.add(u)
            g[v].add(u)
            stack.append(u)
            counter += 1
        if counter == N:
            break
    rs = set()
    solve(None,1, rs)
    for r in rs:
        print(r[0], r[1])

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline
def solve(p, v, rs):
    als = G[v]
    cs = g[v]
    c = 0
    for u in als:
        if u in cs:
            c += solve(v, u, rs)
        elif u != p:
            if v < u:
                c += 1
            rs.add((min(u,v), max(u,v)))
    if p == None:
        return
    elif c%2 != 0:
        rs.add((v, p))
        return 0
    else:
        rs.add((p,v))
        return 1

N, M = map(int, readline().split())
if M %2 != 0:
    print(-1)
else:
    G = defaultdict(set)
    for v in range(M):
        a, b = map(int, readline().split())
        G[a].add(b)
        G[b].add(a)

    stack=[1]
    vs = set([1])
    g = defaultdict(set)
    counter = 1
    while True:
        v = stack.pop()
        for u in G[v]:
            if u in vs:
                continue
            vs.add(u)
            g[v].add(u)
            stack.append(u)
            counter += 1
        if counter == N:
            break
    rs = set()
    solve(None,1, rs)
    for r in rs:
        print(r[0], r[1])

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = defaultdict(set)
for v in range(M):
    a, b = map(int, readline().split())
    G[a].add(b)
    G[b].add(a)


def main():

    if M % 2 != 0:
        print(-1)
        return
    ks = []
    for i in G:
        if len(G[i])%2 != 0:
            ks.append(i)
    if len(ks) not in [0, 2]:
        2//0
        print(-1)
        return
    if len(ks) == 0:
        s=t=1
    else:
        s,t=ks
    v=s
    tr = []
    while True:
        tr.append(v)
        if len(G[v]) == 0:
            break
        u = G[v].pop()
        G[u].remove(v)
        v = u
    tr = tr[::-1]
    tr2 = []
    cc = None
    for (i, t) in enumerate(tr):
        if len(G[t]) != 0:
            cc = i
            v = t
            while True:
                tr2.append(v+1)
                if len(G[v]) == 0:
                    break
                u = G[v].pop()
                G[u].remove(v)
                v = u
            break
    if cc != None:
        ind = 0
        for i in range(cc):
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
        for i in range(len(tr2)-1):
            if ind % 2 == 0:
                print(tr2[i+1], tr2[i])
            else:
                print(tr2[i], tr2[i+1])
            ind+=1
        owari = cc+len(tr2)-1
        nokori = M-owari
        for i in range(nokori):
            i = i + cc
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
    else:
        for i in range(M):
            if i %2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = defaultdict(set)
for v in range(M):
    a, b = map(int, readline().split())
    G[a].add(b)
    G[b].add(a)


def main():

    if M % 2 != 0:
        2//0
        print(-1)
        return
    ks = []
    for i in G:
        if len(G[i])%2 != 0:
            ks.append(i)
    if len(ks) not in [0, 2]:
        print(-1)
        return
    if len(ks) == 0:
        s=t=1
    else:
        s,t=ks
    v=s
    tr = []
    while True:
        tr.append(v)
        if len(G[v]) == 0:
            break
        u = G[v].pop()
        G[u].remove(v)
        v = u
    tr = tr[::-1]
    tr2 = []
    cc = None
    for (i, t) in enumerate(tr):
        if len(G[t]) != 0:
            cc = i
            v = t
            while True:
                tr2.append(v+1)
                if len(G[v]) == 0:
                    break
                u = G[v].pop()
                G[u].remove(v)
                v = u
            break
    if cc != None:
        ind = 0
        for i in range(cc):
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
        for i in range(len(tr2)-1):
            if ind % 2 == 0:
                print(tr2[i+1], tr2[i])
            else:
                print(tr2[i], tr2[i+1])
            ind+=1
        owari = cc+len(tr2)-1
        nokori = M-owari
        for i in range(nokori):
            i = i + cc
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
    else:
        for i in range(M):
            if i %2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = defaultdict(set)
for v in range(M):
    a, b = map(int, readline().split())
    G[a].add(b)
    G[b].add(a)


def main():

    if M % 2 != 0:
        print(-1)
        return
    ks = []
    for i in G:
        if len(G[i])%2 != 0:
            ks.append(i)
    if len(ks) not in [0, 2]:
        print(-1)
        return
    if len(ks) == 0:
        s=t=1
    else:
        s,t=ks
    v=s
    tr = []
    es = set()
    while True:
        tr.append(v)
        if len(G[v]) == 0:
            break
        u = G[v].pop()
        G[u].remove(v)
        v = u
    tr = tr[::-1]
    tr2 = []
    cc = None
    for (i, t) in enumerate(tr):
        if len(G[t-1]) != 0:
            cc = i
            v = t-1
            while True:
                tr2.append(v+1)
                if len(G[v]) == 0:
                    break
                u = G[v].pop()
                G[u].remove(v)
                v = u
            break
    rs = []
    if cc != None:
        2//0
        ind = 0
        for i in range(cc):
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
        for i in range(len(tr2)-1):
            if ind % 2 == 0:
                print(tr2[i+1], tr2[i])
            else:
                print(tr2[i], tr2[i+1])
            ind+=1
        owari = cc+len(tr2)-1
        nokori = M-owari
        for i in range(nokori):
            i = i + cc
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
    else:
        for i in range(M):
            if i %2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = defaultdict(set)
for v in range(M):
    a, b = map(int, readline().split())
    G[a].add(b)
    G[b].add(a)


def main():

    if M % 2 != 0:
        print(-1)
        return
    ks = []
    for i in G:
        if len(G[i])%2 != 0:
            ks.append(i)
    if len(ks) not in [0, 2]:
        print(-1)
        return
    if len(ks) == 0:
        2//0
        s=t=1
    else:
        s,t=ks
    v=s
    tr = []
    es = set()
    while True:
        tr.append(v)
        if len(G[v]) == 0:
            break
        u = G[v].pop()
        G[u].remove(v)
        v = u
    tr = tr[::-1]
    tr2 = []
    cc = None
    for (i, t) in enumerate(tr):
        if len(G[t-1]) != 0:
            cc = i
            v = t-1
            while True:
                tr2.append(v+1)
                if len(G[v]) == 0:
                    break
                u = G[v].pop()
                G[u].remove(v)
                v = u
            break
    rs = []
    if cc != None:
        ind = 0
        for i in range(cc):
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
        for i in range(len(tr2)-1):
            if ind % 2 == 0:
                print(tr2[i+1], tr2[i])
            else:
                print(tr2[i], tr2[i+1])
            ind+=1
        owari = cc+len(tr2)-1
        nokori = M-owari
        for i in range(nokori):
            i = i + cc
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
    else:
        for i in range(M):
            if i %2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = defaultdict(set)
for v in range(M):
    a, b = map(int, readline().split())
    G[a].add(b)
    G[b].add(a)


def main():

    if M % 2 != 0:
        print(-1)
        return
    ks = []
    for i in G:
        if len(G[i])%2 != 0:
            ks.append(i)
    if len(ks) not in [0, 2]:
        print(-1)
        return
    if len(ks) == 0:
        s=t=0
    else:
        s,t=ks
    v=s
    tr = []
    es = set()
    while True:
        tr.append(v)
        if len(G[v]) == 0:
            break
        u = G[v].pop()
        G[u].remove(v)
        v = u
    tr = tr[::-1]
    tr2 = []
    cc = None
    for (i, t) in enumerate(tr):
        if len(G[t-1]) != 0:
            cc = i
            v = t-1
            while True:
                tr2.append(v+1)
                if len(G[v]) == 0:
                    break
                u = G[v].pop()
                G[u].remove(v)
                v = u
            break
    rs = []
    if cc != None:
        ind = 0
        for i in range(cc):
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
        for i in range(len(tr2)-1):
            if ind % 2 == 0:
                print(tr2[i+1], tr2[i])
            else:
                print(tr2[i], tr2[i+1])
            ind+=1
        owari = cc+len(tr2)-1
        nokori = M-owari
        for i in range(nokori):
            i = i + cc
            if ind % 2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
            ind+=1
    else:
        for i in range(M):
            if i %2 == 0:
                print(tr[i+1], tr[i])
            else:
                print(tr[i], tr[i+1])
    return

if __name__ == '__main__':
    main()