Problem p03450 (python)

Fixed submission:
import collections
from pprint import pprint as pp

N,M = tuple(map(int, input().split()))
LRD = collections.defaultdict(list)
Lset = set()
Rset = set()
for _ in range(M):
  l,r,d = map(int, input().split())
  l -= 1
  r -= 1
  LRD[l].append((r,d))
  Lset.add(l)
  Rset.add(r)

n = [ None for _ in range(N) ]

deq = collections.deque()

def func(l):
  lst = LRD.pop(l,[])
  for r,d in lst:
    if n[l] != None:
      d2 = n[l] + d
    else:
      n[l] = 0
      d2 = d
    if n[r] not in (d2, None):
      return True
    n[r] = d2
    deq.append(r)
  return False

def func2():
  lst = Lset-Rset
  for l in lst:
    n[l] = 0
    deq.append(l)
  while deq:
    l = deq.popleft()
    if l in LRD:
      if func(l):
        return True
  deq.extend(LRD.keys())
  while deq:
    l = deq.popleft()
    if l in LRD:
      if func(l):
        return True
  return False

if func2():
  print('No')
else:
  print('Yes')

Buggy submission (verdict: Runtime Error):
import collections
from pprint import pprint as pp

N,M = tuple(map(int, input().split()))
LRD = collections.defaultdict(list)
Lset = set()
Rset = set()
for _ in range(M):
  l,r,d = map(int, input().split())
  l -= 1
  r -= 1
  LRD[l].append((r,d))
  Lset.add(l)
  Rset.add(r)

n = [ None for _ in range(N) ]

deq = collections.deque()

def func(l):
  lst = LRD.pop(l,[])
  for r,d in lst:
    if n[l] != None:
      d2 = n[l] + d
    else:
      n[l] = 0
      d2 = d
    if n[r] not in (d2, None):
      return True
    n[r] = d2
    deq.push(r)
  return False

def func2():
  lst = Lset-Rset
  for l in lst:
    n[l] = 0
    deq.push(l)
  while deq:
    l = deq.popleft()
    if l in LRD:
      if func(l):
        return True
  deq.extend(LRD.keys())
  while deq:
    l = deq.popleft()
    if l in LRD:
      if func(l):
        return True
  return False

if func2():
  print('No')
else:
  print('Yes')

Buggy submission (verdict: Runtime Error):
import collections
from pprint import pprint as pp

N,M = tuple(map(int, input().split()))
LRD = collections.defaultdict(list)
Lset = set()
Rset = set()
for _ in range(M):
  l,r,d = map(int, input().split())
  l -= 1
  r -= 1
  LRD[l].append((r,d))
  Lset.add(l)
  Rset.add(r)

n = [ None for _ in range(N) ]

def func(l):
  lst = LRD.pop(l,[])
  for r,d in lst:
    if n[l] != None:
      d2 = n[l] + d
    else:
      n[l] = 0
      d2 = d
    if n[r] not in (d2, None):
      return True
    n[r] = d2
    if r in LRD:
      if func(r):
        return True
  return False

def func2():
  lst = Lset-Rset
  for l in lst:
    n[l] = 0
  for l in lst:
    if l in LRD:
      if func(l):
        return True
  lst = tuple(LRD.keys())
  for l in lst:
    if l in LRD:
      if func(l):
        return True
  return False

if func2():
  print('No')
else:
  print('Yes')

Buggy submission (verdict: Runtime Error):
import collections
from pprint import pprint as pp

N,M = tuple(map(int, input().split()))
LRD = collections.defaultdict(list)
Lset = set()
Rset = set()
for _ in range(M):
  l,r,d = map(int, input().split())
  l -= 1
  r -= 1
  LRD[l].append((r,d))
  Lset.add(l)
  Rset.add(r)

n = [ None for _ in range(N) ]

def func(l):
  lst = LRD.pop(l, [])
  for r,d in lst:
    if n[l] != None:
      d2 = n[l] + d
    else:
      n[l] = 0
      d2 = d
    if n[r] not in (d2, None):
      return True
    n[r] = d2
    if func(r):
      return True
  return False

def func2():
  lst = Lset-Rset
  for l in lst:
    n[l] = 0
  for l in lst:
    if func(l):
      return True
  lst = tuple(LRD.keys())
  for l in lst:
    if func(l):
      return True
  return False

if func2():
  print('No')
else:
  print('Yes')

Buggy submission (verdict: Runtime Error):
import collections
from pprint import pprint as pp

N,M = tuple(map(int, input().split()))
#LRD = [tuple(map(int, input().split())) for _ in range(M)]

LRD = collections.defaultdict(list)
Lset = set()
Rset = set()
for _ in range(M):
  l,r,d = map(int, input().split())
  l -= 1
  r -= 1
  LRD[l].append((r,d))
  Lset.add(l)
  Rset.add(r)

n = [ None for _ in range(N) ]

def func(l):
  lst = LRD.pop(l)
  for r,d in lst:
    if n[l] != None:
      d2 = n[l] + d
    else:
      n[l] = 0
      d2 = d
    if n[r] not in (d2, None):
      return True
    n[r] = d2
    if r in LRD:
      if func(r):
        return True
  return False

def func2():
  lst = Lset-Rset
  for l in lst:
    n[l] = 0
  while lst:
    for l in lst:
      if func(l):
        return True
    Lset2 = set(LRD.keys())
    Rset2 = set(r for r,d in LRD)
    lst = Lset2-Rset2
  lst = tuple(LRD.keys())
  for l in lst:
    if l in LRD:
      if func(l):
        return True
  return False

if func2():
  print('No')
else:
  print('Yes')