Problem:
 Score : 400 points    Problem Statement 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.      Constraints  1 \leq N \leq 100  000  0 \leq M \leq 200  000  1 \leq L_i, R_i \leq N ( 1 \leq i \leq M )  0 \leq D_i \leq 10  000 ( 1 \leq i \leq M )  L_i \neq R_i ( 1 \leq i \leq M )  If i \neq j , then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j) .  D_i are integers.         Input Input is given from Standard Input in the following format:  N  M  L_1  R_1  D_1  L_2  R_2  D_2  :  L_M  R_M  D_M       Output If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes ; if it does not exist, print No .        Sample Input 1 3 3
1 2 1
2 3 1
1 3 2      Sample Output 1 Yes  Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103) .       Sample Input 2 3 3
1 2 1
2 3 1
1 3 5      Sample Output 2 No  If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.       Sample Input 3 4 3
2 1 1
2 3 5
3 4 2      Sample Output 3 Yes       Sample Input 4 10 3
8 7 100
7 9 100
9 8 100      Sample Output 4 No       Sample Input 5 100 0      Sample Output 5 Yes

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines









from collections import deque














def main():
	n,m=map(int,input().split())
	li=[[] for _ in range(n+1)]
	for i in range(m):
		l,r,d=map(int,input().split())
		li[l].append((r,d))
		li[r].append((l,-d))
	
	q=deque()
	a=[None]*(n+1)

	for i in range(1,n+1):
		if a[i]==None:
			q=[i]
			a[i]=0
			while q:
				now=q.pop()
				for r,d in li[now]:
					if a[r]==None:
						a[r] = a[now] + d
						q.append(r)
					else:
						if a[r]-a[now]!=d:
							print("No")
							exit(0)
						else:
							continue
					
	print("Yes")
	
	
if __name__ == '__main__':
	main()

Solution (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    inf=10**10
    to = defaultdict(list)
    n, m = map(int, input().split())
    for _ in range(m):
        l, r, d = map(int, input().split())
        l, r = l - 1, r - 1
        to[l].append([r, d])
        to[r].append([l, -d])
    pos = [inf] * n
    for root in range(n):
        if pos[root] != inf: continue
        pos[root] = 0
        stack = [[root, 0]]
        while stack:
            u, p = stack.pop()
            for v, d in to[u]:
                if pos[v] == inf:
                    stack.append([v, p + d])
                    pos[v] = p + d
                else:
                    if pos[v] != p + d:
                        print("No")
                        exit()
    print("Yes")

main()

Solution (Python):
from collections import deque
def bfs(n,fordfs):
    l = [None] * n
    g = fordfs
    for i in range(n):
        if l[i] != None:
            
            continue
        l[i] = 0
        que = deque()
        que.append(i)
        while que:
            d = que.pop()
            for node, cost in g[d]:
                if l[node] == None:
                    l[node] = l[d] + cost
                    que.append(node)
                else:
                    if l[node] != l[d] + cost:
                        print('No')
                        exit(0)
    print('Yes')

    return

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, M = LI()
g = [[] for _ in range(N)]
for i in range(M):
    x,y,z = map(int,input().split())


    g[x-1].append((y-1, z))
    g[y-1].append((x-1, -z))

bfs(N,g)

Solution (Python):
import sys

class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0]*n
        self.weight = [0]*n
    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y
    
    def union(self, x, y, w):
        px = self.find(x)
        py = self.find(y)
        d = w - self.weight[x] + self.weight[y]
        if self.rank[px] < self.rank[py]:
            self.par[px] = py
            self.weight[px] = d
        else:
            self.par[py] = px
            self.weight[py] = -d
            if self.rank[px] == self.rank[py]:
                self.rank[px] += 1
    
    def same(self, x, y):
        return self.find(x) == self.find(y)
    
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

N,M = map(int,input().split())
G = WeightedUnionFind(N)

for i in range(M):
    l,r,d = map(int,input().split())
    l -= 1
    r -= 1
    if G.same(l,r):
        if G.diff(l,r) != d:
            print("No")
            sys.exit()
    else:
        G.union(l,r,d)
    
print("Yes")

Solution (Python):
N,M = map(int, input().split())
L = [0]*M
R = [0]*M
D = [0]*M
for i in range(M):
    L[i], R[i], D[i] = map(int, input().split())
    
G = [[] for _ in range(N)]
for i in range(M):
    G[L[i]-1].append([R[i]-1, D[i]])
    G[R[i]-1].append([L[i]-1, -D[i]])
    
ans = "Yes"
visited = ["?"]*N
for i in range(N):
    if visited[i] == "?":
        q = [[i, 0]]
        visited[i] = 0
        while len(q) > 0:
            temp = q.pop(-1)
            pos = temp[0]
            d = temp[1]
            for j in range(len(G[pos])):
                if visited[G[pos][j][0]] == "?":
                    q.append([G[pos][j][0], d+G[pos][j][1]])
                    visited[G[pos][j][0]] = d+G[pos][j][1]
                elif visited[G[pos][j][0]] != d+G[pos][j][1]:
                    ans = "No"
                    break

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

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

graph = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    graph[L].append((R, D))
    graph[R].append((L, -D))

d = [None] * (N + 1)

def calc(start): 
    d[start] = 0
    stack = [start]
    while len(stack) != 0:
        tmp = stack.pop()
        for next_, diff in graph[tmp]:
            if d[next_] == None:
                d[next_] = d[tmp] + diff
                stack.append(next_)
            else:
                if d[next_] == d[tmp] + diff:
                    pass
                else:
                    print ('No')
                    exit()
    return 




for i in range(1, N):
    if d[i] == None:
        calc(i)
print ('Yes')

Solution (Python):
import sys
input = sys.stdin.readline

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

graph = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    graph[L].append((R, D))
    graph[R].append((L, -D))

d = ['a'] * (N + 1)

def calc(start): 
    d[start] = 0
    stack = [start]
    while len(stack) != 0:
        tmp = stack.pop()
        for next_, diff in graph[tmp]:
            if d[next_] == 'a':
                d[next_] = d[tmp] + diff
                stack.append(next_)
            else:
                if d[next_] == d[tmp] + diff:
                    pass
                else:
                    print ('No')
                    exit()
    return 




for i in range(1, N):
    if d[i] == 'a':
        calc(i)
print ('Yes')

Solution (Python):
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    g[l - 1].append((r - 1, d))
    g[r - 1].append((l - 1, -d))
l = [None] * n
for i in range(n):
    if l[i] != None:
        continue
    l[i] = 0
    stack = [i]
    while stack:
        d = stack.pop()
        for node, cost in g[d]:
            if l[node] == None:
                l[node] = l[d] + cost
                stack.append(node)
            else:
                if l[node] != l[d] + cost:
                    print('No')
                    exit(0)
print('Yes')

Solution (Python):
n,m=map(int,input().split())
graph = [[] for i in range(n)]
x=[None for _ in range(n)]
for _ in range(m):
    a,b,c=map(int,input().split())
    graph[b-1].append([a-1, -c])
    graph[a-1].append([b-1, c])
def fuck():
    for i in range(n):
        if x[i]==None:
            x[i]=0
            q=[i]
        while q:
            j=q.pop()
            for  d, e in graph[j]:
                if x[d]==None:
                    q.append(d)
                    x[d]=x[j]+e
                elif x[d]!=x[j]+e:
                    return False
    return True
if fuck():
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import deque
INFTY = 10**10
N,M = map(int,input().split())
ind = 2
if N==1:
    ind = 0
else:
    G = {}
    for _ in range(M):
        L,R,D = map(int,input().split())
        if L not in G:
            G[L]=[]
        G[L].append((R,D))
        if R not in G:
            G[R] = []
        G[R].append((L,-D))
    dist = [INFTY for _ in range(N+1)]
    hist = [0 for _ in range(N+1)]
    for i in G:
        ind = 2
        heap = deque([i])
        while heap:
            cur = heap.popleft()
            if hist[cur]==0:
                dist[cur] = 0
                hist[cur]=1
                ind = 0
            elif hist[cur]==1 and ind==2:
                break
            for x in G[cur]:
                v,w = x[0],x[1]
                if hist[v]==0:
                    dist[v] = dist[cur]+w
                    heap.append(v)
                    hist[v]=1
                else:
                    if dist[v]!=dist[cur]+w:
                        ind = 1
                        break
            if ind==1:break
        if ind==1:break
if ind!=1:
    print("Yes")
else:
    print("No")

Solution (Python):
n,m=map(int,input().split())
x=[[]for _ in range(n+1)]
y=[None]*(n+1)
a="Yes"
for num in range(m):
	i,j,k=map(int,input().split())
	x[i].append((j,k))
	x[j].append((i,-k))
for i in range(1,n+1):
	if y[i]==None:
		y[i]=0;q=[i]
		while q:
			p=q.pop()
			for j,k in x[p]:
				if y[j]==None:q+=[j];y[j]=y[p]+k
for i in range(1,n+1):
	for j,k in x[i]:
		if y[i]+k!=y[j]:a="No"
print(a)

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    def inputs():
        return (int(x) for x in input().split())
    N,M = inputs()
    E={i:[] for i in range(1,N+1)}
    for i in range(M):
        L,R,D = inputs()
        E[L].append((R,D))
        E[R].append((L,-D))
    visited = {i: False for i in range(1,N+1)}
    dis = [0]*(N+1)
    for i in range(1,N+1):
        if not visited[i]:
            stk = [( i, -1, 0)]
            while stk:
                v, p, d = stk.pop()
                
                if visited[v]:
                    if dis[v]!=dis[p]+d:
                        print("No")
                        exit()
                    continue
                visited[v] = True
                dis[v]=dis[p]+d
                for u,d in E[v]:
                    if u != p:
                        stk.append((u,v,d))
    print("Yes")
main()

Solution (Python):
INF = 10**20
N,M = map(int,input().split())
node = [[] for _ in range(N)]
for _ in range(M):
  l,r,d = map(int,input().split())
  node[l-1].append([r-1,d])
  node[r-1].append([l-1,-1*d])

ans = "Yes"
pos = [INF]*N
for i in range(N):
  if pos[i]==INF:
    que = [[i,0]]
    while(que):
      c,cp = que.pop()
      if pos[c]==INF:
        pos[c] = cp
        for n,d in node[c]:
          if pos[n]==INF:
            que.append([n,cp+d])
          elif pos[n] != cp+d:
            ans="No"
            break
        else:
          continue
        break
      elif pos[c] != cp:
        ans="No"
        break
    else:
      continue
    break
print(ans)

Solution (Python):
import collections
import sys

input = sys.stdin.readline

n, m = map(int, input().split())
es = [collections.deque() for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    es[l].append((l, r, d))
    es[r].append((r, l, -d))

visited = [False] * n
for i in range(n):
    if not visited[i]:
        visited[i] = True
        if len(es[i]) == 0:
            continue
        distances = [float('inf')] * n
        distances[i] = 0
        q = collections.deque()
        for e in es[i]:
            q.append(e)
        while q:
            j, k, d = q.popleft()
            if visited[k] and distances[k] != distances[j] + d:
                print('No')
                exit()
            distances[k] = distances[j] + d
            visited[k] = True
            while es[k]:
                ee = es[k].popleft()
                if len(es[ee[1]]) > 0:
                    q.append(ee)
print('Yes')

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list,zip(*read_all))



def scc(N, G, RG):
    used = [False]*N
    group = [None]*N
    order = []
    def dfs(S):
        i_order = []
        temp_order = []
        level = 0
        while S:
            v1 = S.pop()
            if used[v1[0]]:
                continue
            if v1[1]<level:
                for _ in range(len(temp_order)-v1[1]):
                    x = temp_order.pop()
                    i_order.append(x)
                level = v1[1]
            level += 1
            if not used[v1[0]]:
                for v1_to in G[v1[0]]:
                    if not used[v1_to]:
                        S.append((v1_to,level))
                temp_order.append(v1[0])
                used[v1[0]] = True
        for t in reversed(temp_order):
            i_order.append(t)
        return i_order
    for i in range(N):
        if not used[i]:
            i_order = dfs([(i,0)])
            order += i_order
    def rdfs(S,col):
        used[S[0]] = True
        while S:
            v1 = S.pop()
            for v1_to in RG[v1]:
                if not used[v1_to]:
                    used[v1_to] = True
                    S.append(v1_to)
            group[v1] = col
    used = [False]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs([s], label)
            label += 1
    return label, group

def edges_to_pt(s,t,n,directed=False):
    pt = [[] for _ in range(n)]
    for i in range(len(s)):
        pt[s[i]-1].append(t[i]-1)
        if directed==False:
            pt[t[i]-1].append(s[i]-1)
    return pt

N,M = II()
if M>0:
    L,R,D = Line(M,3)
else:
    print('Yes')
    exit()

pt = edges_to_pt(L,R,N)
label,group = scc(N,pt,pt)

x = []
used = [False]*label
for i in range(N):
    if not used[group[i]]:
        x.append(i)
        used[group[i]] = True

pt_l = [[] for _ in range(N)]
for i in range(M):
    pt_l[L[i]-1].append((R[i]-1,D[i]))
    pt_l[R[i]-1].append((L[i]-1,-D[i]))

check_visited = [False]*N
l = [None]*N 
for v in x:
    check_visited[v] = True
    l[v] = 0
    S = [v]
    while S:
        v1 = S.pop()
        for i in pt_l[v1]:
            if check_visited[i[0]] == False:
                check_visited[i[0]] = True
                l[i[0]] = l[v1] + i[1]
                S.append(i[0])
            else:
                if l[i[0]] == l[v1] + i[1]:
                    continue
                else:
                    print('No')
                    exit()

print('Yes')

Solution (Python):
from collections import defaultdict as dd
N, M = map(int, input().split())
e = dd(list)
inf = 10 ** 9
pos = [inf] * (N + 1)
for i in range(M):
  a, b, d = map(int, input().split())
  e[a].append((b, d))
  e[b].append((a, -d))
visited = [0] * (N + 1)
s = []
for i in range(1, N + 1):
  if visited[i] == 0:
    s.append(i)
    pos[i] = 0
  while len(s):
    p = s.pop()
    for q in e[p]:
      if pos[q[0]] == inf:
        pos[q[0]] = pos[p] + q[1]
        visited[q[0]] = 1
        s.append(q[0])
      else:
        if pos[q[0]] - pos[p] != q[1]:
          print("No")
          exit(0)
print("Yes")

Solution (Python):
import heapq
N, M = map(int, input().split())
E = [[] for _ in range(N)]
RE = [[] for _ in range(N)]
S = [0] * N
for _ in range(M):
  l, r, d = map(int, input().split())
  S[l - 1] = 1
  E[l - 1].append([d, r - 1])
  RE[r - 1].append([d, l - 1])
D = [None] * N
def dijkstra(i):
  D[i] = 0
  Q = [[0, i]]
  while Q:
    d, s = heapq.heappop(Q)
    for dist, adj in E[s]:
      if D[adj] == None:
        D[adj] = D[s] + dist
        heapq.heappush(Q, [D[adj], adj])
      elif D[adj] == 'Error' or D[adj] != D[s] + dist:
        D[s] = 'Error'
        break
    else:
      for dist, adj in RE[s]:
        if D[adj] == None:
          D[adj] = D[s] - dist
          heapq.heappush(Q, [D[adj], adj])
        elif D[adj] == 'Error' or D[adj] != D[s] - dist:
          D[s] = 'Error'
          break
      else:
        continue
    break
for i in range(N):
  if D[i] == 'Error':
    print('No')
    break
  elif D[i] == None:
    dijkstra(i)
else:
  for d in D:
    if d == 'Error':
      print('No')
      break
  else:
    print('Yes')

Solution (Python):
n, m = map(int, input().split())
D = [list(map(int, input().split())) for i in range(m)]

A = [0] * (n+1)

M = [[] for i in range(n+1)]

for i in range(m):
    M[D[i][0]].append([D[i][1], D[i][2]])
    M[D[i][1]].append([D[i][0], -D[i][2]])

V = [0] * (n+1)




for ii in range(1, n+1):
    if V[ii] == 0 and len(M[ii]) > 0:

        Q = [[ii, 0]]
        st = 0
        while st < len(Q):
            
            node = Q[st][0]
            dist = Q[st][1]
            if V[node] == 0 and len(M[node]) > 0:
                V[node] = 1
                A[node] += dist
                for i in M[node]:
                    if V[i[0]] == 0:
                        Q.append([i[0], A[node] + i[1]])
            st += 1



def d():
    for i in range(m):
        if A[D[i][1]] - A[D[i][0]] != D[i][2]:
            return "No"
    return "Yes"

print(d())

Solution (Python):
def sol():
    n,m=map(int,input().split())
    t,w={},{}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w: w[l].add((r,d))
        else: w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    chk=set()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        chk.add((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if (i,j[0]) in t and t[(i,j[0])]!=t[(i,x[0])]+j[1]:
                        print("No")
                        exit()
                    elif (i,j[0]) not in t:
                        
                        t[(i,j[0])]=x[1]+j[1]
                        chk.add((j[0],x[1]+j[1]))
    print("Yes")

if __name__=="__main__":
    sol()

Solution (Python):
class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        
        self.weight = [0] * (n+1)
        self.flag = 0
 
    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y
 
    
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        
        if rx == ry:
            if self.diff(x,y) != w:
                self.flag = 1
        
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        
        else:
            self.par[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1
 
    
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]
    
def main():
    import sys
    input = sys.stdin.readline
    
    N,M = map(int,input().split())
    flag = 0
    A = WeightedUnionFind(N)
    for i in range(M):
        L,R,D = map(int,input().split())
        A.union(L,R,D)
            
    if A.flag == 1:
        print("No")
    else:
        print("Yes")
    
    
main()

Solution (Python):
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n,q = m()

data = [l() for i in range(q)]

edges = [[] for i in range(n)]

for a,b,c in data:
    edges[a-1].append([b-1,c])
    edges[b-1].append([a-1,-c])

num = [0 for _ in range(n)]

Ma,mi = 0,0

vi = [False for i in range(n)]
ans = 0

for i in range(n):
    if vi[i]:
        continue
    stack = [i]
    vi[i] = True
    while stack:
        v = stack.pop()
        for e,r in edges[v]:
            if not vi[e]:
                num[e] = num[v] + r
                stack.append(e)
                vi[e] = True

for i in range(n):
    if num[i] < mi:
        mi = num[i]
    elif num[i] > Ma:
        Ma = num[i]
    for e,r in edges[i]:
        if num[i] + r != num[e]:
            ans = -1
            break
    else:
        continue
    break
else:
    print("Yes")
    exit()
print("No")

Solution (Python):
N, M = map(int, input().split())
LRD = [[int(i) for i in input().split()] for _ in range(M)]

class WeightedUnionFind() :
    def __init__(self, N) :
        self.parent = [-1] * N
        self.potential = [0] * N
        
    def root(self, x) :
        while self.parent[x] >= 0:
            x = self.parent[x]
        return x

    def unite(self, x, y, w) :
        rx = self.root(x)
        ry = self.root(y)
        
        if rx != ry :
            w = w + self.weight(x) - self.weight(y)
            if self.parent[rx] > self.parent[ry] :
                self.parent[rx] = ry
                self.potential[rx] = -w
            else :
                if self.parent[rx] == self.parent[ry] :
                    self.parent[rx] -= 1
                self.parent[ry] = rx
                self.potential[ry] = w
    
    def same(self, x, y) :
        return self.root(x) == self.root(y)
        
    def size(self, x) :
        return -self.parent[self.root(x)]
    
    def weight(self, x) :
        w = 0
        while self.parent[x] >= 0 :
            w += self.potential[x]
            x = self.parent[x]
        return w
        
    def diff(self, x, y) :
        return self.weight(y) - self.weight(x)
            
def solve() :
    WUF = WeightedUnionFind(N)
    for L, R, D in LRD :
        L, R = L - 1, R - 1
        if WUF.same(L, R) :
            if WUF.diff(L, R) != D :
                return 'No'
        else :
            WUF.unite(L, R, D)
    
    return 'Yes'
    
print(solve())

Solution (Python):
N,Q = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(Q)]
edge = [[] for _ in range(N)]

for a,b,c in ABC:
    edge[a-1].append([b-1,c])
    edge[b-1].append([a-1,-c])

numlist = [0] * N
MIN,MAX = 0,0
visited = [False] * N
ans = 0

for i in range(N):
    if visited[i]:
      continue
    stack = [i]
    visited[i] = True
    while stack:
      v = stack.pop()
      for u,d in edge[v]:
        if not visited[u]:
          numlist[u] = numlist[v] + d
          stack.append(u)
          visited[u] = True
ans = 0
for i in range(N):
    if numlist[i] < MIN: MIN = numlist[i]
    elif numlist[i] > MAX: MAX = numlist[i]
    for u,d in edge[i]:
      if numlist[i] + d != numlist[u]:
        ans = -1
        break
    else:
      continue
    break
if ans != -1:
    ans = 'Yes'
else:
  	ans = 'No'

    
print(ans)

Solution (Python):

N,Q = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(Q)]
edge = [[] for _ in range(N)]

for a,b,c in ABC:
    edge[a-1].append([b-1,c])
    edge[b-1].append([a-1,-c])

numlist = [0] * N
MIN,MAX = 0,0
visited = [False] * N
ans = 0

for i in range(N):
    if visited[i]:
      continue
    stack = [i]
    visited[i] = True
    while stack:
      v = stack.pop()
      for u,d in edge[v]:
        if not visited[u]:
          numlist[u] = numlist[v] + d
          stack.append(u)
          visited[u] = True
ans = 0
for i in range(N):
    if numlist[i] < MIN: MIN = numlist[i]
    elif numlist[i] > MAX: MAX = numlist[i]
    for u,d in edge[i]:
      if numlist[i] + d != numlist[u]:
        ans = -1
        break
    else:
      continue
    break
if ans != -1:
    ans = 'Yes'
else:
  	ans = 'No'

    
print(ans)

Solution (Python):
from collections import deque, defaultdict
import sys
input = sys.stdin.readline


def main():
    inf = 10 ** 10
    N, M = map(int, input().split())
    x = [inf] * (N + 1)
    adj = defaultdict(lambda: [])
    for _ in range(M):
        L, R, D = map(int, input().split())
        adj[L].append((R, D))
        adj[R].append((L, -D))
    seen = [0] * (N + 1)

    def dfs(adj, start):
        que = deque()
        que.append(start)
        x[start] = 0
        while que:
            v = que.pop()
            seen[v] = 1
            for u, d in adj[v]:
                if seen[u] == 0:
                    que.append(u)
                    if x[u] == inf:
                        x[u] = x[v] + d
                    else:
                        if x[u] != x[v] + d:
                            print('No')
                            sys.exit()

    for i in range(1, N + 1):
        if seen[i] == 0:
            dfs(adj, i)

    print('Yes')


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys
import time
input = sys.stdin.readline


def main():
    t1 = time.time()
    N, M = map(int, input().split())
    adj = [[] for _ in range(N + 1)]
    for _ in range(M):
        L, R, D = map(int, input().split())
        adj[L].append((R, D))
        adj[R].append((L, -D))
    seen = [0] * (N + 1)

    def dfs(adj, seen, start):
        inf = 10 ** 10
        que = deque()
        que.append(start)
        x = [inf] * (N + 1)
        x[start] = 0
        while que:
            v = que.pop()
            seen[v] = 1
            for u, d in adj[v]:
                if seen[u] == 0:
                    que.append(u)
                    if x[u] == inf:
                        x[u] = x[v] + d
                    else:
                        if x[u] != x[v] + d:
                            print('No')
                            sys.exit()
            t2 = time.time()
            if t2 - t1 > 1.8:
                print('Yes')
                sys.exit()
        return seen

    for i in range(1, N + 1):
        if seen[i] == 0:
            seen = dfs(adj, seen, i)

    print('Yes')


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline

def topological_sort(graph: list, n_v: int) -> list:
    
    indegree = [0] * n_v 
    for i in range(n_v):
        for c, v in graph[i]:
            indegree[v] += 1

    cand = deque([i for i in range(n_v) if indegree[i] == 0])
    res = []
    while cand:
        v1 = cand.popleft()
        res.append(v1)
        for c, v2 in graph[v1]:
            indegree[v2] -= 1
            if indegree[v2] == 0:
                cand.append(v2)

    return res


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    edges[l].append((d, r))

ts = topological_sort(edges, n)

if len(ts) != n:
    
    ans = "No"
else:
    
    dist = [-1] * n
    ans = "Yes"
    for v in ts:
        if dist[v] == -1:
            dist[v] = 0

        for c, u in edges[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + c
            elif dist[u] != dist[v] + c:
                ans = "No"
                break

print(ans)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline

def topological_sort(graph: list, n_v: int) -> list:
    
    indegree = [0] * n_v 
    for i in range(n_v):
        for c, v in graph[i]:
            indegree[v] += 1

    cand = deque([i for i in range(n_v) if indegree[i] == 0])
    res = []
    while cand:
        v1 = cand.popleft()
        res.append(v1)
        for c, v2 in graph[v1]:
            indegree[v2] -= 1
            if indegree[v2] == 0:
                cand.append(v2)

    return res


n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    edges[l].append((d, r))

ts = topological_sort(edges, n)

if len(ts) != n:
    
    ans = "No"
else:
    
    dist = [-1] * n
    ans = "Yes"
    for i in range(n):
        v = ts[i]
        if dist[v] == -1:
            dist[v] = 0

        for c, u in edges[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + c
            elif dist[u] != dist[v] + c:
                ans = "No"
                break

print(ans)

Solution (Python):
from collections import deque

def topological_sort(graph: list, n_v: int) -> list:
    
    indegree = [0] * n_v 
    for i in range(n_v):
        for c, v in graph[i]:
            indegree[v] += 1

    cand = deque([i for i in range(n_v) if indegree[i] == 0])
    res = []
    while cand:
        v1 = cand.popleft()
        res.append(v1)
        for c, v2 in graph[v1]:
            indegree[v2] -= 1
            if indegree[v2] == 0:
                cand.append(v2)

    return res




n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    edges[l].append((d, r))

ts = topological_sort(edges, n)

if len(ts) != n:
    ans = "No"
    
else:
    
    dist = [-1] * n
    ans = "Yes"
    for i in range(n):
        v = ts[i]
        if dist[v] == -1:
            dist[v] = 0

        for c, u in edges[v]:
            if dist[u] == -1:
                dist[u] = dist[v] + c
            elif dist[u] != dist[v] + c:
                ans = "No"
                break

print(ans)