Problem p02976 (python)

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

int1 = lambda x: int(x) - 1
n, m = map(int, input().split())
if m % 2:
  print(-1); exit(0)

l = [tuple(map(int1, input().split())) for _ in range(m)]
G = [list() for _ in range(n)]

ps = [-1]*n
tot = [0]*n

def find(x):
  if ps[x] < 0:
    return x
  ps[x] = find(ps[x])
  return ps[x]

def unite(u, v):
  u = find(u)
  v = find(v)
  if ps[u] > ps[v]:
    u, v = v, u
  ps[u] += ps[v]
  ps[v] = u
  return

def same(u, v):
  return find(u) == find(v)

for u, v in l:
  if same(u, v):
    print(u+1, v+1)
    tot[u] += 1
  else:
    G[u].append(v)
    G[v].append(u)
    unite(u, v)

def dfs(v, p):
  for u in G[v]:
    if u == p: continue
    dfs(u, v)
  if p >= 0:
    if tot[v] % 2:
      print(v+1, p+1)
      tot[v] += 1
    else:
      print(p+1, v+1)
      tot[p] += 1
  return

dfs(0, -1)

Buggy submission (verdict: Runtime Error):
class LinearSystemIncidence:
    '''
    need repair
    https://atcoder.jp/contests/agc035/submissions/13967640
    '''
    def __init__(self, N, edges, mod2=False, idx1=True):
        self.N = N
        self.M = len(edges)
        self.mod2 = mod2
        self.X = [0] * self.M
        self.seen = [False] * self.N
        self.G = [list() for _ in range(self.N)]
        for i, (u, v) in enumerate(edges):
            u -= idx1; v -= idx1
            self.G[u].append((i, v))
            self.G[v].append((i, ~u))

    def dfs(self, b, u):
        self.seen[u] = True
        ret = b[u]
        for eid, v in self.G[u]:
            fwd = (v >= 0)
            if not fwd:
                v = ~v
            if self.seen[v]:
                continue
            tmp = self.dfs(b, v)
            self.X[eid] = tmp if fwd else -tmp
            ret += tmp

            if self.mod2:
                self.X[eid] &= 1
        if self.mod2:
            ret &= 1
        return ret

    def _solve(self, b):
        for s in range(self.N):
            if self.seen[s]:
                continue
            if self.dfs(b, s):
                return False, list()
        return True, self.X

    def solve(self, b):
        par = [-1] * self.N
        for s in range(self.N):
            if self.seen[s]:
                continue
            q = [s]
            while q:
                u = q[-1]
                if self.seen[u] > 1:
                    q.pop()
                elif self.seen[u]:
                    for eid, v in self.G[u]:
                        fwd = (v >= 0)
                        if not fwd:
                            v = ~v
                        if self.seen[v] > 1 and par[v] == u:
                            self.X[eid] = b[v] if fwd else -b[v]
                            b[u] += b[v]
                            if self.mod2:
                                self.X[eid] &= 1
                    self.seen[u] += 1
                    if self.mod2:
                        b[u] &= 1
                    q.pop()

                else:
                    self.seen[u] = True
                    for eid, v in self.G[u]:
                        fwd = (v >= 0)
                        if not fwd:
                            v = ~v
                        if not self.seen[v]:
                            par[v] = u
                            q.append(v)
            if b[s]:
                return False, list()
        return True, self.X


def main():
    N, M = map(int, input().split())
    E = [tuple(map(int, input().split())) for _ in range(M)]
    C = [0] * N
    for u, v in E:
        C[u-1] += 1

    solver = LinearSystemIncidence(N, E, mod2=True)
    bo, ret = solver._solve(C)
    if bo:
        for i in range(M):
            u, v = E[i]
            if ret[i]:
                print(u, v)
            else:
                print(v, u)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
class LinearSystemIncidence:
    def __init__(self, N, edges, mod2=False, idx1=True):
        self.N = N
        self.M = len(edges)
        self.mod2 = mod2
        self.X = [0] * self.M
        self.seen = [False] * self.N
        self.G = [list() for _ in range(self.N)]
        for i, (u, v) in enumerate(edges):
            u -= idx1; v -= idx1
            self.G[u].append((i, v))
            self.G[v].append((i, ~u))

    def dfs(self, b, u):
        self.seen[u] = True
        res = b[u]
        for eid, v in self.G[u]:
            fwd = v >= 0
            if not fwd:
                v = ~v
            if self.seen[v]:
                continue
            tmp = self.dfs(b, v)
            self.X[eid] = tmp if fwd else -tmp
            res += tmp

            if self.mod2:
                self.X[eid] &= 1
        if self.mod2:
            res &= 1
        return res

    def solve(self, b):
        for s in range(self.N):
            if self.seen[s]:
                continue
            if self.dfs(b, s):
                return False, list()
        return True, self.X


def main():
    N, M = map(int, input().split())
    E = [tuple(map(int, input().split())) for _ in range(M)]
    C = [0] * N
    for u, v in E:
        C[u-1] += 1

    solver = LinearSystemIncidence(N, E, mod2=True)
    bo, ret = solver.solve(C)
    if bo:
        for i in range(M):
            u, v = E[i]
            if ret[i]:
                print(u, v)
            else:
                print(v, u)
    else:
        print(-1)

if __name__ == '__main__':
    main()