Problem p02902 (python)

Fixed submission:
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, M = map(int, input().split())
e = dd(list)
reve = dd(list)
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
  reve[v].append(u)
vis = set()
inc = [0] * (N + 1)
def dfs(x, l):
  global vis
  global inc
  vis.add(x)
  ok = 0
  for y in reve[x]:
    if y in vis: inc[x] += 1
  if inc[x] > 1:
    inc[x] = 0
    vis.discard(x)
    return
  for y in e[x]:
    if y == l:
      ok = 1
      continue
    if y in vis:
      inc[x] = 0
      vis.discard(x)
      return
  if ok:
    print(len(vis))
    for v in vis: print(v)
    #print(vis, inc)
    exit(0)
  for y in e[x]:
    dfs(y, l)
  vis.discard(x)

for s in range(1, N + 1):
  dfs(s, s)
print(-1)

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, M = map(int, input().split())
if M == 0:
  print(1)
  print(1)
  exit(0)
e = dd(list)
reve = dd(list)
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
  reve[v].append(u)
vis = set()
inc = [0] * (N + 1)
def dfs(x, l):
  global vis
  global inc
  vis.add(x)
  ok = 0
  for y in reve[x]:
    if y in vis: inc[x] += 1
  if inc[x] > 1:
    inc[x] = 0
    vis.discard(x)
    return
  for y in e[x]:
    if y == l:
      ok = 1
      continue
    if y in vis:
      inc[x] = 0
      vis.discard(x)
      return
  if ok:
    print(len(vis))
    for v in vis: print(v)
    #print(vis, inc)
    exit(0)
  for y in e[x]:
    dfs(y, l)
  vis.discard(x)

for s in range(1, N + 1):
  dfs(s, s)
print(-1)

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, M = map(int, input().split())
if M == 0:
  print(1)
  print(1)
  exit(0)
e = dd(list)
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
vis = set()
res = []
chk = None
def check():
  global chk
  if chk != None:
    return chk
  ress = set(res)
  c = 0
  for x in res:
    for y in e[x]:
      if y in ress:
        c += 1
        if c > len(res):
          chk = None
          return
  chk = True
  return True
def dfs(x):
  global vis
  global res
  vis.add(x)
  if len(res):
    if check(): return
  for y in e[x]:
    if y in vis and len(res) == 0:
      res += sorted(vis)
      #print(res, vis)
      if check(): return
      else: res = []
    else:
      dfs(y)
  vis.discard(x)

for s in range(1, N + 1):
  dfs(s)
  #print(res, s)
if len(res) == 0:
  print(-1)
  exit(0)
print(len(res))
for r in res: print(r)

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict as dd
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
e = dd(list)
for _ in range(M):
  u, v = map(int, input().split())
  e[u].append(v)
res = []
l = []
vis = [0] * (N + 1)
globalvis = [0] * (N + 1)
def dfs(p):
  global vis
  global globalvis
  global l
  for q in e[p]:
    if vis[q] == 0:
      vis[q] = 1
      globalvis[q] = 1
      dfs(q)
      vis[q] = 0
    else:
      l = [p, q]
def finaldfs(p, g):
  global res
  for q in e[p]:
    if q == g:
      res.append(g)
      print(len(res))
      for r in res:
        print(r)
      exit(0)
    if vis[q] == 0:
      res.append(q)
      vis[q] = 1
      finaldfs(q, g)
      res.pop()
      vis[q] = 0
for i in range(1, N + 1):
  if globalvis[i] == 1: continue
  vis[i] = 1
  globalvis[i] = 1
  dfs(i)
  if len(l): break
if len(l):
  vis = [0] * (N + 1)
  #print("a", vis)
  res.append(l[1])
  finaldfs(l[1], l[0])
else:
  print(-1)