Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

from collections import deque
no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=deque([[0,no_seen.pop()]])
  #print(todo)
  while todo:
    d,t=todo.popleft()
    no_seen.discard(t)
    dst[t]=d
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])

print('Yes')

Fixed submission:
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

from collections import deque
no_seen=set(range(n))
inf=float('inf')
while no_seen:
  todo=deque([[0,no_seen.pop()]])
  dst=[inf]*n
  #print(todo)
  while todo:
    d,t=todo.popleft()
    dst[t]=d
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        no_seen.discard(t)
        pass
      else:
        todo.append([d+di,ti])

print('Yes')

Fixed submission:
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
ds={} # ds[(l,r)]:rがlのどれだけ右にいるか
dr={} # dr[i]:r=iのヒントのlの集合
import sys
sys.setrecursionlimit(10**7)
def add(l,r,d):
  if (r,l) in ds:
    print('No')
    exit()
  if (l,r) in ds and ds[(l,r)]!=d:
    print('No')
    #print(ds)
    exit()
  ds[(l,r)]=d
  if r in dr:
    dr[r].add(l)
  else:
    dr[r]=set([l])
  if l in dr:
    for i in dr[l]:
      add(i,r,ds[(i,l)]+d)

for l,r,d in lrd:
  add(l,r,d)

print('Yes')
#print(ds)


Fixed submission:
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
ds={} # ds[(l,r)]:rがlのどれだけ右にいるか
dr={} # dr[i]:r=iのヒントのlの集合
import sys
sys.setrecursionlimit(10**7)
def add(l,r,d):
  if (r,l) in ds:
    print('No')
    exit()
  if (l,r) in ds and ds[(l,r)]!=d:
    print('No')
    #print(ds)
    exit()
  ds[(l,r)]=d
  if r in dr:
    dr[r].add(l)
  else:
    dr[r]=set([l])
  if l in dr:
    for i in dr[l]:
      add(i,r,ds[(i,l)]+d)

for l,r,d in lrd:
  add(l,r,d)

print('Yes')
#print(ds)


Fixed submission:
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
ds={} # ds[(l,r)]:rがlのどれだけ右にいるか
dr={} # dr[i]:r=iのヒントのlの集合
import sys
sys.setrecursionlimit(10**7)
def add(l,r,d):
  if (l,r) in ds and ds[(l,r)]!=d:
    print('No')
    #print(ds)
    exit()
  ds[(l,r)]=d
  if r in dr:
    dr[r].add(l)
  else:
    dr[r]=set([l])
  if l in dr:
    for i in dr[l]:
      add(i,r,ds[(i,l)]+d)

for l,r,d in lrd:
  add(l,r,d)

print('Yes')
#print(ds)


Fixed submission:
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')