Problem p02976 (python)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 12:05:59 2019

@author: ctiuser
"""
import sys
sys.setrecursionlimit(10**6)

N, M = map(int,input().split())
A = [list(map(int,input().split())) for i in range(M)]

if M % 2 == 1:
    print(-1)
    sys.exit()

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for i in range(size+1)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            return self.find(self.table[x])
    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return False
        if self.table[s1] < self.table[s2]:
            self.table[s1] = self.table[s1] + self.table[s2]
            self.table[s2] = self.find(x)
        else:
            self.table[s2] = self.table[s1] + self.table[s2]
            self.table[s1] = self.find(y)
        return True

UF = UnionFind(N)
ans = []
T = [[-1] for i in range(N+1)]
color = [-1 for i in range(N+1)]
counta = [0 for i in range(N+1)]
B = []
C = [[] for i in range(N+1)]
for c in A:
    if UF.union(c[0],c[1]):
        B.append(c)
        C[c[0]].append(c[1])
        C[c[1]].append(c[0])
    else:
        ans.append(c)
        counta[c[0]] += 1

def b(x):
    color[x] = 1
    for c in C[x]:
        if color[c] == -1:
            T[c][0] = x
            T[x].append(c)
            b(c)
b(1)

edge_leaf = []
for i in range(1,N+1):
    if len(T[i]) == 1:
        edge_leaf.append(i)

def a(x):
    p = T[x][0]
    if p == -1:
        return 0
    if counta[x] % 2 == 0:
        ans.append([p,x])
        counta[p] += 1
    else:
        ans.append([x,p])
    return p

for c in edge_leaf:
    point = c
    while color[point] == len(T[point]):
        point = a(point)
        color[point] += 1


for c in ans:
    print(' '.join(map(str,c)))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 12:22:32 2019

@author: ctiuser
"""
import sys

N, M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(M)]

if M & 1:
    print(-1)
    sys.exit()    

T = [[] for _ in range(N+1)]
for a,b in AB:
    T[a].append(b)
    T[b].append(a)

visited = [False] * (N+1)
tree = [set([]) for _ in range(N+1)]
def dfs(x=1):
    for c in T[x]:
        if visited[c]:
            continue
        tree[x].add(c)
        tree[c].add(x)
        visited[c] = True
        dfs(c)
dfs()

counta = [0] * (N+1)
ans = []
for a,b in AB:
    if b in tree[a]:
        continue
    ans.append('{} {}'.format(a,b))
    counta[a] += 1

def dfs2(x=1, parent = None):
    for y in tree[x]:
        if y == parent:
            continue
#        print(x,y)
        dfs2(y,x)
        if counta[y]&1:
            ans.append('{} {}'.format(y,x))
            counta[y] += 1
        else:
            ans.append('{} {}'.format(x,y))
            counta[x] += 1
#        print(x,y,counta)
dfs2()
for i in range(M):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 12:22:32 2019

@author: ctiuser
"""
import sys

N, M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(M)]

if M & 1:
    print(-1)
    sys.exit()    

T = [[] for _ in range(N+1)]
for a,b in AB:
    T[a].append(b)
    T[b].append(a)

visited = [False] * (N+1)
tree = [set([]) for _ in range(N+1)]
def dfs(x=1):
    for c in T[x]:
        if visited[c]:
            continue
        tree[x].add(c)
        tree[c].add(x)
        visited[c] = True
        dfs(c)
dfs()

counta = [0] * (N+1)
ans = []
for a,b in AB:
    if b in tree[a]:
        continue
    ans.append('{} {}'.format(a,b))
    counta[a] += 1

def dfs2(x=1, parent = None):
    for y in tree[x]:
        if y == parent:
            continue
        dfs2(y,x)
        if counta[y]&1:
            ans.append('{} {}'.format(x,y))
            counta[y] += 1
        else:
            ans.append('{} {}'.format(y.x))
            counta[x] += 1
dfs2()
for i in range(M):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 12:05:59 2019

@author: ctiuser
"""
import sys

N, M = map(int,input().split())
A = [list(map(int,input().split())) for i in range(M)]

if M % 2 == 1:
    print(-1)
    sys.exit()

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for i in range(size+1)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            return self.find(self.table[x])
    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 == s2:
            return False
        if self.table[s1] < self.table[s2]:
            self.table[s1] = self.table[s1] + self.table[s2]
            self.table[s2] = self.find(x)
        else:
            self.table[s2] = self.table[s1] + self.table[s2]
            self.table[s1] = self.find(y)
        return True

UF = UnionFind(N)
ans = []
T = [[-1] for i in range(N+1)]
color = [-1 for i in range(N+1)]
counta = [0 for i in range(N+1)]
B = []
C = [[] for i in range(N+1)]
for c in A:
    if UF.union(c[0],c[1]):
        B.append(c)
        C[c[0]].append(c[1])
        C[c[1]].append(c[0])
    else:
        ans.append(c)
        counta[c[0]] += 1

def b(x):
    color[x] = 1
    for c in C[x]:
        if color[c] == -1:
            T[c][0] = x
            T[x].append(c)
            b(c)
b(1)

edge_leaf = []
for i in range(1,N+1):
    if len(T[i]) == 1:
        edge_leaf.append(i)

def a(x):
    p = T[x][0]
    if p == -1:
        return 0
    if counta[x] % 2 == 0:
        ans.append([p,x])
        counta[p] += 1
    else:
        ans.append([x,p])
    return p

for c in edge_leaf:
    point = c
    while color[point] == len(T[point]):
        point = a(point)
        color[point] += 1


for c in ans:
    print(' '.join(map(str,c)))