Problem p02976 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()
  
E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

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

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()
  
E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

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

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()
  
E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

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

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()
  
E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

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

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()
  
E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
else:
  E = []
  A = [[] for _ in range(N)]
  for _ in range(M):
    i, j = map(int, input().split())
    E.append([i-1, j-1])
    A[i-1].append(j-1)
    A[j-1].append(i-1)

  tree = [[] for _ in range(N)]
  visited = [False] * N
  queue = [0]
  visited[0] = True
  while queue:
    i = queue.pop()
    for j in A[i]:
      if not visited[j]:
        tree[i].append(j)
        tree[j].append(i)
        visited[j] = True
        queue.append(j)

  D = [0] * N
  for i, j in E:
    if i not in tree[j]:
      print(i+1, j+1)
      D[i] += 1

  def f(parent, i):
    for j in tree[i]:
      if j == parent:
        continue
      f(i, j)
      if D[j] % 2 == 0:
        print(i+1, j+1)
        D[i] += 1
      else:
        print(j+1, i+1)
        D[j] += 1
    return

  f(0, 0)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()

E = []
A = [[] for _ in range(N)]
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])
  A[i-1].append(j-1)
  A[j-1].append(i-1)

tree = [[] for _ in range(N)]
visited = [False] * N
queue = [0]
visited[0] = True
while queue:
  i = queue.pop()
  for j in A[i]:
    if not visited[j]:
      tree[i].append(j)
      tree[j].append(i)
      visited[j] = True
      queue.append(j)

D = [0] * N
for i, j in E:
  if i not in tree[j]:
    print(i+1, j+1)
    D[i] += 1

def f(parent, i):
  for j in tree[i]:
    if j == parent:
      continue
    f(i, j)
    if D[j] % 2 == 0:
      print(i+1, j+1)
      D[i] += 1
    else:
      print(j+1, i+1)
      D[j] += 1
  return

f(0, 0)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()

E = []
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])

tree = [set() for _ in range(N)]
visited = [False] * N
root = E[0][0]
visited[root] = True
queue = [root]
D = [0] * N
for i, j in E:
  if visited[j]:
    D[i] += 1
    print(i+1, j+1)
  else:
    tree[i].add(j)

def f(tree, i):
  if tree[i] == set():
    return
  for j in tree[i]:
    f(tree, j)
    if D[i] % 2 == 0:
      D[j] += 1
      print(j+1, i+1)
    else:
      D[i] += 1
      print(i+1, j+1)
  return

f(tree, root)

Buggy submission (verdict: Runtime Error):
class UnionFind:
  def __init__(self, N):
    self.par = list(range(N))
    
  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      self.par[x] = y
    return
  
  def find(self, x):
    if self.par[x] == x:
      return x
    else:
      return self.find(self.par[x])

N, M = map(int, input().split())
if M % 2 == 1:
  print(-1)
  exit()

E = []
for _ in range(M):
  i, j = map(int, input().split())
  E.append([i-1, j-1])

uf = UnionFind(N)
tree = []
D = [0] * N
for i, j in E:
  if uf.find(i) != uf.find(j):
    uf.union(i, j)
    tree.append([i, j])
  else:
    D[i] += 1
    print(i+1, j+1)

for i, j in tree:
  if D[i] % 2 == 0:
    D[j] += 1
    print(j+1, i+1)
  else:
    D[i] += 1
    print(i+1, j+1)