Problem: There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.  You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.  It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Submission attempts (in order):
N,M = list(map(int,input().split()))

edges = [[] for _ in range(N)]

for i in range(M):
    L,R,D = list(map(int,input().split()))
    edges[L - 1].append((R -1,D))
    edges[R - 1].append((L-1, -D))


visited = [None] * N
place = [0] * N
inf = 10 ** 10


def walk(i):
    if visited[i]:
        return (i,i)
    visited[i] = True
    (n,m) = (inf, - inf)
    for j,d in edges[i]:
        print(j,d)
        if visited[j] and place[j] != place[i] + d:
            print("No")
            exit()
        place[j] = place[i] + d
        (x,y) = walk(j)
        (n,m) = (min(n,x),max(m,y))
    return (n,m)

for i in range(N):
    if not visited[i]:
        place[i] = 0
    
        (n,m) = walk(i)
        if m - n > 10 ** 9:
            print("No")
            exit()
print("Yes")

from collections import defaultdict
N,M = list(map(int,input().split()))

edges = [[] for _ in range(N)]

for i in range(M):
    L,R,D = list(map(int,input().split()))
    edges[L - 1].append((R -1,D))
    edges[R - 1].append((L-1, -D))


visited = [None] * N
place = [0] * N
inf = 10 ** 10


def walk(i):
    if visited[i]:
        return (i,i)
    visited[i] = True
    (n,m) = ( - inf, inf)
    for j,d in edges[i]:
        if visited[j] and place[j] != place[i] + d:
            print("No")
            exit()
        place[j] = place[i] + d
        (x,y) = walk(j)
        (n,m) = (min(n,x),max(m,y))
    return (n,m)

for i in range(N):
    
    (n,m) = walk(i)
    if m -n > 10 ** 9:
        print("No")
        exit()
print("Yes")

N,M = list(map(int,input().split()))

edges = [[] for _ in range(N)]

for i in range(M):
    L,R,D = list(map(int,input().split()))
    edges[L - 1].append((R -1,D))
    edges[R - 1].append((L-1, -D))


visited = [None] * N
place = [0] * N
inf = 10 ** 10


def walk(i):
    if visited[i]:
        return (i,i)
    visited[i] = True
    (n,m) = (inf, - inf)
    for j,d in edges[i]:
        print(j,d)
        if visited[j] and place[j] != place[i] + d:
            print("No")
            exit()
        place[j] = place[i] + d
        (x,y) = walk(j)
        (n,m) = (min(n,x),max(m,y))
    return (n,m)

for i in range(N):
    if not visited[i]:
        place[i] = 0
    
        (n,m) = walk(i)
        if m - n > 10 ** 9:
            print("No")
            exit()
print("Yes")

from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
 
def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]
 
setrecursionlimit(1000000)
INF = 10**10
 
(N, M) = reads()
 
edges = [[] for _ in range(N)]
 
for _ in range(M):
    (L, R, D) = reads()
    (L, R) = (L-1, R-1)
    edges[L].append((R, D))
    edges[R].append((L, -D))
 
visited = [False] * N
place = [None] * N
 
def walk(i):
    if visited[i]:
        return (place[i], place[i])
    visited[i] = True
    (m, M) = (INF, -INF)
    for (j, d) in edges[i]:
        if visited[j] and place[j] != place[i] + d:
            # print("v = ", visited)
            # print("p = ", place)
            print("No")
            exit()
        place[j] = place[i] + d
        (x, y) = walk(j)
        (m, M) = (min(m, x), max(M, y))
    return (m, M)
 
for i in range(N):
    if not visited[i]:
        place[i] = 0
        (m, M) = walk(i)
        if M - m > 10**9:
            # print("(m, M) = ", (m, M))
            print("No")
            exit()
 
print("Yes")

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted