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):
class WeightedUnionFind():
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0 for i in range(n)]
        
        self.diff_weight = [0 for i in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        
        root = self.find(self.par[x])
        self.diff_weight[x] += self.diff_weight[self.par[x]]
        self.par[x] = root
        return self.par[x]

    def weight(self, x):
        """ x 根に対する重みを返す """
        self.find(x)
        return self.diff_weight[x]

    def diff(self, x, y):
        """ x と y の差分をとる """
        return self.weight(y) - self.weight(x)

    def unite(self, x, y, w):
        """ ノード x の下にノード y をつける。辺の重みは、w """
        
        w += self.weight(x); w -= self.weight(y)
        x = self.find(x); y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            
            self.par[x] = y
            w = -w
            self.diff_weight[x] = w
        elif self.rank[y] < self.rank[x]:
            self.par[y] = x
            self.diff_weight[y] = w
        else:
            self.par[y] = x
            self.rank[x] += 1
            self.diff_weight[y] = w
            
N, M = map(int, input().split())
UF = WeightedUnionFind(N)
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    
    if UF.find(L) == UF.find(R):
        if UF.diff(L, R) != D:
            print('No')
            break
    else:
        UF.unite(L, R, D)
else:
    print('Yes')

Solution (Python):
"""
unionFind木を使う。
根に対する距離を記録していく。
"""
import sys
sys.setrecursionlimit(2000000)
input = sys.stdin.readline
class UnionFind():
    def __init__(self,n):
        self.n=n
        self.parents = [i for i in range(n+1)]
        self.distance = [0]*(n+1)

    def find(self,x):
        if self.parents[x]==x:
            return x,0
        else:
            self.parents[x],dist2=self.find(self.parents[x])
            self.distance[x] += dist2
            return self.parents[x],self.distance[x]

    def union(self,l,r,dist):
        lRoot,lDist = self.find(l)
        rRoot,rDist = self.find(r)
        
        
        
        
        if lRoot==rRoot and lDist-rDist != dist:
            print("No")
            exit()

        
        if lDist < dist+rDist:
            
            self.parents[lRoot]=rRoot
            self.distance[lRoot]=(dist+rDist)-lDist
        else:
            self.parents[rRoot]=lRoot
            self.distance[rRoot]=lDist-(dist+rDist)

N,M = map(int,input().split())
tree = UnionFind(N)
for i in range(M):
    L,R,D = map(int,input().split())
    tree.union(L,R,D)

print('Yes')

Solution (Python):
class WeightedUnionFind:

    def __init__(self, n):
        self.parents = [i for i in range(n + 1)]
        self.weights = [0 for _ in range(n + 1)]
        self.ranks = [0 for _ in range(n + 1)]

    def find_root(self, node):
        parent_node = self.parents[node]
        if node == parent_node:
            return node
        else:
            root_node = self.find_root(parent_node)
            
            self.parents[node] = root_node
            self.weights[node] += self.weights[parent_node]
            return root_node

    def union(self, one, other, weight):
        one_root = self.find_root(one)
        other_root = self.find_root(other)
        if one_root == other_root:
            return
        one_rank = self.ranks[one_root]
        other_rank = self.ranks[other_root]
        diff_weight = weight + self.weights[one] - self.weights[other]
        if one_rank < other_rank:
            self.parents[one_root] = other_root
            
            self.weights[one_root] -= diff_weight
        else:
            self.parents[other_root] = one_root
            
            self.weights[other_root] += diff_weight
            if one_rank == other_rank:
                self.ranks[other_root] += 1

    def is_same_group(self, one, other):
        one_root = self.find_root(one)
        other_root = self.find_root(other)
        return one_root == other_root

    def diff(self, one, other):
        if not self.is_same_group(one, other):
            return None
        return self.weights[other] - self.weights[one]


def main():
    N, M = list(map(int, input().split(' ')))
    tree = WeightedUnionFind(N)
    for _ in range(M):
        L, R, D = list(map(int, input().split(' ')))
        d = tree.diff(L, R)
        if d is None:
            tree.union(L, R, D)
            continue
        
        
        if d != D:
            print('No')
            exit(0)
    print('Yes')


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

INF=10**9
N,M=map(int,input().split())

def dfs(u,d1):
  pos_list[u]=d1
  for v,d2 in graph[u]:
    if pos_list[v]==-INF:
      dfs(v,d1+d2)
    elif pos_list[v]!=d1+d2:
      print("No")
      sys.exit(0)      
  return

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


pos_list=[-INF]*(N+1)
for i in range(1,N+1):
  if pos_list[i]!=-INF:
    continue
  dfs(i,0)
          

print("Yes")

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)

    
    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 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]

import sys

input=sys.stdin.readline

N,M=map(int,input().split())
uf=WeightedUnionFind(N)
for i in range(0,M):
    l,r,d=map(int,input().split())
    if uf.same(l,r):
        if d!=uf.diff(l,r):
            print('No')
            break
    else:
        uf.union(l,r,d)
else:
    print('Yes')

Solution (Python):


import sys
sys.setrecursionlimit(200000)



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


def judge(W, D, visit, i):
    visit[i] = True
    for j, d in W[i]:
        if D[j] is None:
            D[j] = D[i] + d
        else:
            if D[i] + d != D[j]:
                return True
        if visit[j]:
            continue
        if judge(W, D, visit, j):
            return True
    
    return False


def main():
    W = [[] for _ in range(N)]
    for l, r, d in LRD:
        W[l - 1].append((r - 1, d))
        W[r - 1].append((l - 1, -d))
    
    D = [None] * N
    visit = [False] * N
    for i in range(N):
        if visit[i]:
            continue
        D[i] = 0
        if judge(W, D, visit, i):
            print("No")
            return
    
    print("Yes")


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

INF=10**9
N,M=map(int,input().split())

def dfs(u,d1):
  pos_list[u]=d1
  for v,d2 in graph[u]:
    if pos_list[v]==-INF:
      dfs(v,d1+d2)
    elif pos_list[v]!=d1+d2:
      print("No")
      sys.exit(0)      
  return

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


pos_list=[-INF]*(N+1)
for i in range(1,N+1):
  if pos_list[i]!=-INF:
    continue
  dfs(i,0)
          

print("Yes")

Solution (Python):
import sys
INF=10**9
N,M=map(int,input().split())

graph=[]
for i in range(N+1):
  graph.append([])

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


pos_list=[-INF]*(N+1)
for i in range(1,N+1):
  if pos_list[i]!=-INF:
    continue
    
  queue={i}
  pos_list[i]=0
  while(len(queue)>0):
    new_queue=set()
    for q in queue:
      for v,d in graph[q]:
        if pos_list[v]==-INF:
          pos_list[v]=pos_list[q]+d
          new_queue.add(v)
        elif pos_list[v]!=pos_list[q]+d:
          print("No")
          sys.exit(0)
          
    queue=new_queue
          

print("Yes")

Solution (Python):
from sys import exit

class WeightedUnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.weights = [0] * (n+1)

    def find(self, x):
        if self.parent[x] == x:
            return x
        nx = self.find(self.parent[x])
        self.weights[x] += self.weights[self.parent[x]]
        self.parent[x] = nx
        return nx

    def unite(self, x, y, d):
        rx = self.find(x)
        ry = self.find(y)
        if rx == ry:
            return
        if self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
            self.weights[rx] = d + self.weights[y] - self.weights[x]
        else:
            self.parent[ry] = rx
            self.weights[ry] = - d + self.weights[x] - self.weights[y]
            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.weights[x] - self.weights[y]

n, m = map(int, input().split())
wuf = WeightedUnionFind(n)
for _ in range(m):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    if not wuf.same(l, r):
        wuf.unite(l, r, d)
    else:
        if wuf.diff(l, r) != d:
            print("No")
            exit()
print("Yes")

Solution (Python):
class WeightedUnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.weights = [0] * (n + 1)  

    def find(self, x):
        if self.parent[x] == x:
            return x
        nx = self.find(self.parent[x])
        self.weights[x] += self.weights[self.parent[x]]
        self.parent[x] = nx
        return nx

    def unite(self, x, y, d):
        rx = self.find(x)
        ry = self.find(y)
        if rx == ry:
            return
        if self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
            self.weights[rx] = d + self.weights[y] - self.weights[x]
        else:
            self.parent[ry] = rx
            self.weights[ry] = - d + self.weights[x] - self.weights[y]
            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.weights[x] - self.weights[y]


def main():
    n, m = map(int, input().split())
    wuf = WeightedUnionFind(n)
    for _ in range(m):
        l, r, d = map(int, input().split())
        l -= 1
        r -= 1
        if not wuf.same(l, r):
            wuf.unite(l, r, d)
        else:
            if wuf.diff(l, r) != d:
                print("No")
                exit()
    print("Yes")


if __name__ == "__main__":
    main()

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

def bfs(s):
    q = deque([s])
    visited[s] = True
    
    while q:
        v = q.popleft()
        
        for nv, d in G[v]:
            if visited[nv]:
                if dist[v]+d!=dist[nv]:
                    print('No')
                    exit()
            else:
                visited[nv] = True
                dist[nv] = dist[v]+d
                q.append(nv)

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

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append((R-1, D))
    G[R-1].append((L-1, -D))
    
visited = [False]*N
dist = [0]*N

for i in range(N):
    if not visited[i]:
        bfs(i)

print('Yes')

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
n,m=map(int,input().split())
g=[[] for _ in range(n)]
indeg=[0]*n
touch=[0]*n
for _ in range(m):
    l,r,d=map(int,input().split())
    l-=1
    r-=1
    touch[l]=1
    touch[r]=1
    g[l].append((r,d))
    indeg[r]+=1
if m==0:
    print('Yes')
    exit()
entered=False
for i in range(n):
    if indeg[i]==0 and touch[i]==1:
        vis=[False]*n
        entered=True
        dist=[-1]*n
        dist[i]=0
        def dfs(u):
            vis[u]=1
            for v,wt in g[u]:
                if dist[v]==-1:
                    dist[v]=dist[u]+wt
                elif dist[v]!=dist[u]+wt:
                    print('No')
                    exit()
                if vis[v]==False:
                    dfs(v)
        dfs(i)

if entered==False:
    print('No')
else:
    print('Yes')

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
n,m=map(int,input().split())
g=[[] for _ in range(n)]
indeg=[0]*n
touch=[0]*n
for _ in range(m):
    l,r,d=map(int,input().split())
    l-=1
    r-=1
    touch[l]=1
    touch[r]=1
    g[l].append((r,d))
    indeg[r]+=1
if m==0:
    print('Yes')
    exit()
entered=False
for i in range(n):
    if indeg[i]==0 and touch[i]==1:
        vis=[False]*n
        entered=True
        dist=[-1]*n
        dist[i]=0
        def dfs(u):
            vis[u]=1
            for v,wt in g[u]:
                if dist[v]==-1:
                    dist[v]=dist[u]+wt
                elif dist[v]!=dist[u]+wt:
                    print('No')
                    exit()
                if vis[v]==False:
                    dfs(v)
        dfs(i)

if entered==False:
    print('No')
else:
    print('Yes')

Solution (Python):


from collections import deque

n, m = map(int, input().split())

graph = {i: {} for i in range(1, n + 1)}


for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a][b] = c
    graph[b][a] = -c



distance = ["inf" for i in range(n + 1)]


visited = [False for i in range(n + 1)]
visited[0] = True


def find(x):
    d = deque()
    visited[x] = True
    distance[x] = 0
    d.append(x)
    flag = True
    while d and flag:

        x = d.popleft()
        for node in graph[x]:
            if visited[node] == False:
                visited[node] = True
                d.append(node)
                if distance[node] == "inf":
                    distance[node] = distance[x] + graph[x][node]
                else:
                    if  distance[node] != distance[x] + graph[x][node]:
                        flag = False
                        break
            elif visited[node]==True:
                if distance[node]!=distance[x]+graph[x][node]:
                    flag=False
                    break
         
    if flag:
        return False
    else:
        return True

all_flag = True
not_visited = True
while not_visited and all_flag:
    judge = False
    for i in range(1, n + 1):
        if visited[i] == False:
            judge = True
            if find(i):
                all_flag = False
                break
            else:
                pass

    if not judge:
        not_visited = False

if all_flag:
    print("Yes")

else:
    print("No")

Solution (Python):


from collections import deque

n, m = map(int, input().split())

graph = {i: {} for i in range(1, n + 1)}


for _ in range(m):
    a, b, c = map(int, input().split())
    graph[a][b] = c
    graph[b][a] = -c



distance = ["inf" for i in range(n + 1)]


visited = [False for i in range(n + 1)]
visited[0] = True


def find(x):
    d = deque()
    visited[x] = True
    distance[x] = 0
    d.append(x)
    flag = True
    while d and flag:

        x = d.popleft()
        for node in graph[x]:
            if visited[node] == False:
                visited[node] = True
                d.append(node)
                if distance[node] == "inf":
                    distance[node] = distance[x] + graph[x][node]
                else:
                    if  distance[node] != distance[x] + graph[x][node]:
                        flag = False
                        break
            elif visited[node]==True:
                if distance[node]!=distance[x]+graph[x][node]:
                    flag=False
                    break
         
    if flag:
        return False
    else:
        return True

all_flag = True
not_visited = True
while not_visited and all_flag:
    judge = False
    for i in range(1, n + 1):
        if visited[i] == False:
            judge = True
            if find(i):
                all_flag = False
                break
            else:
                pass

    if not judge:
        not_visited = False

if all_flag:
    print("Yes")

else:
    print("No")

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
    self.weight = [0]*(n+1)
  def Find_Root(self, x):
    if(self.root[x] < 0):
      return x
    else:
      r = self.Find_Root(self.root[x])
      self.weight[x] += self.weight[self.root[x]]
      self.root[x] = r
      return r
  def GetWeight(self, x):
    self.Find_Root(x)
    return self.weight[x]
  def Unite(self, x, y, w):
    w += self.GetWeight(x)
    w -= self.GetWeight(y)
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if(x == y):
      return False
    if self.rnk[x] < self.rnk[y]:
      x, y = y, x
      w = -w
    if self.rnk[x] == self.rnk[y]:
      self.rnk[x] += 1
    self.root[y] = x
    self.weight[y] = w
    return True
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  def Count(self, x):
    return -self.root[self.Find_Root(x)]
  def Diff(self, x, y):
    return self.GetWeight(y) - self.GetWeight(x)

n, m = map(int, input().split())
lrd = [list(map(int, input().split())) for _ in range(m)]
uf = UnionFind(n+1)
for l, r, d in lrd:
  if not uf.isSameGroup(l, r):
    uf.Unite(l, r, d)
  else:
    if uf.Diff(l, r) != d:
      print('No')
      exit()

print('Yes')

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**5+1)
def dfs(p):
  for edge in g[p]:
    if x[edge[0]] is not None:
      if x[edge[0]]-x[p]!=edge[1]:
        print('No')
        exit()
    else:
      x[edge[0]]=x[p]+edge[1]
      dfs(edge[0])
n,m=map(int,input().split())
g=[[] for i in range(n)]
for i in range(m):
  u,v,c=map(int,input().split())
  g[u-1].append((v-1,c))
  g[v-1].append((u-1,-c))
x=[None]*n
for i in range(n):
  if x[i]==None:
    x[i]=0
    dfs(i)
print('Yes')

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


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

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

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Solution (Python):
n,m=map(int,input().split())
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)

    
    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 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]
uf=WeightedUnionFind(n)
for i in range(m):
  l,r,d=map(int,input().split())
  l-=1
  r-=1
  if uf.same(l,r):
    if uf.diff(l,r)!=d:
      print('No')
      exit()
  else:
    uf.union(l,r,d)
print('Yes')

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, M = [int(x) for x in input().strip().split()]
L = [deque([]) for _ in range(N)]

for m in range(M):
    l, r, d = [int(x) for x in input().strip().split()]
    L[l-1].append((r, d))
    L[r-1].append((l, -d))

D = [0] * N
F = [False] * N

def dfs(n, d):
    
    if F[n-1]:
        if D[n-1] == d:
            
            return True
        else:
            
            return False
    F[n-1] = True
    D[n-1] = d
    for nn, d_ in L[n-1]:
        
        if not dfs(nn, d+d_):
            return False
    else:
        return True

for n in range(1, N+1):
    if not dfs(n, D[n-1]):
        print('No')
        exit()
else:
    print('Yes')

Solution (Python):
from collections import deque

n,m = map(int, input().split())
edges = [{} for i in range(n+1)]

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

remains = set(range(1,n+1))
deq = deque()

min_ = -1 * 10**10
xs = [min_] * (n+1)

while( len(remains) > 0):
    deq.append( remains.pop() )
    while(len(deq) > 0):
        tmp = deq.popleft()
        if(xs[tmp]  == min_):
            xs[tmp] = 0
        tmp_x = xs[tmp]

        edge = edges[tmp]

        for key,val in edge.items():
            if(xs[key] == min_):
                xs[key] = tmp_x + val
                deq.append( key )
                remains.remove( key )
            else:
                if( xs[key] != tmp_x + val ):
                    print('No')
                    exit()

print('Yes')

Solution (Python):
from collections import deque

n,m = map(int, input().split())
edges = [{} for i in range(n+1)]

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

remains = set(range(1,n+1))
deq = deque()

min_ = -1 * 10**10
xs = [min_] * (n+1)

while( len(remains) > 0):
    deq.append( remains.pop() )
    while(len(deq) > 0):
        tmp = deq.popleft()
        if(xs[tmp]  == min_):
            xs[tmp] = 0
        tmp_x = xs[tmp]

        edge = edges[tmp]

        for key,val in edge.items():
            if(xs[key] == min_):
                xs[key] = tmp_x + val
                deq.append( key )
                remains.remove( key )
            else:
                if( xs[key] != tmp_x + val ):
                    print('No')
                    exit()

print('Yes')

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


class WeightedUnionFind:
    def __init__(self, x):
        self.p = [e for e in range(x)]
        self.rank = [0] * x
        self.weight = [0] * x

    def same(self, u, v):
        return self.find_set(u) == self.find_set(v)

    def find_set(self, u):
        if u != self.p[u]:
            v = self.find_set(self.p[u])
            self.weight[u] += self.weight[self.p[u]]
            self.p[u] = v

        return self.p[u]

    def unite(self, u, v, w):
        w += self.weight[u]
        w -= self.weight[v]

        u = self.find_set(u)
        v = self.find_set(v)

        if self.rank[u] < self.rank[v]:
            u, v = v, u
            w = -w

        if self.rank[u] == self.rank[v]:
            self.rank[u] += 1

        self.p[v] = u

        self.weight[v] = w

    def get_weight(self, u):
        self.find_set(u)
        return self.weight[u]

    def diff(self, u, v):
        return self.get_weight(v) - self.get_weight(u)


wuf = WeightedUnionFind(n)

for l, r, d in lrd:
    l -= 1
    r -= 1

    if wuf.same(l, r):
        
        if wuf.diff(l, r) != d:
            print("No")
            exit()

    else:
        wuf.unite(l, r, d)

print("Yes")

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
INF = 10**10
def bfs(graph, visited, position, root):
    visited[root] = 1
    position[root] = 0
    deq = deque([root])
    while(deq):
        cur = deq.pop()
        
        for nxt, dist in graph[cur]:
            if position[cur] + dist != position[nxt]:
                if position[nxt] != INF:
                    return False
                else:
                    position[nxt] = position[cur] + dist
            if not visited[nxt]:
                deq.append(nxt)
                visited[nxt] = 1

    return True

def main():
    n,m = getList()
    position = [INF for i in range(n)]
    graph = [[] for i in range(n)]
    for i in range(m):
        a,b,c = getList()
        graph[a-1].append((b-1, c))
        graph[b - 1].append((a-1, -c))

    
    visited = [0 for i in range(n)]
    for i in range(n):
        if not visited[i]:
            
            res = bfs(graph, visited, position, i)
            if res == False:
                print("No")
                return

    print("Yes")

if __name__ == "__main__":

    main()

Solution (Python):
def root(v):
    if par[v] == v:
        return v
    r = root(par[v])
    diff_weight[v] += diff_weight[par[v]]
    par[v] = r
    return r

def weight(v):
    root(v)
    return diff_weight[v]

def diff(u, v):
    return weight(v) - weight(u)

def unite(u, v, w):
    w += diff(v, u)
    u = root(u)
    v = root(v)
    if u == v:
        return
    if rank[u] < rank[v]:
        u, v = v, u
        w = -w
    par[v] = u
    diff_weight[v] = w
    if rank[u] == rank[v]:
        rank[u] += 1

def same(u, v):
    return root(u) == root(v)

N, M = map(int, input().split())
par = list(range(N))
rank = [0] * N
diff_weight = [0] * N
for _ in range(M):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    if same(l, r):
        dist = diff(l, r)
        if dist != d:
            print("No")
            exit()
    else:
        unite(l, r, d)

print("Yes")

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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)

    
    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 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 chk(a,b,c):
    return True if uf.diff(a,b) == c else False
n,m = inpl()
z = [-1] * n
uf = WeightedUnionFind(n)
for i in range(m):
    a,b,c = inpl()
    a -= 1; b -= 1
    if uf.same(a,b):
        if not chk(a,b,c):
            print('No')
            quit()
    else:
        uf.union(a,b,c)
print('Yes')

Solution (Python):
vs = set()
from collections import defaultdict
def dfs_w(d, stack):
    depthh = defaultdict(int)
    path = [0]
    pvs = set()
    if stack[0] in vs:
        return True
    while stack:
        v, dpd = stack.pop()
        if v > 0:
            parent = path[-1]
            path.append(v)
            vs.add(v)
            pvs.add(v)
            depthh[v] = depthh[parent] + dpd

            for u, dpd in d[v]:
                if u in pvs:
                    if depthh[u] != depthh[v] + dpd:
                        return False
                if u in vs:
                    continue
                stack += [(-v, dpd), (u, dpd)]
        else:
            path.pop()

    return True


if __name__ == '__main__':
    N, M = map(int, input().split())
    G = [set() for _ in range(N+1)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        G[a].add((b,c))
        G[b].add((a,-c))
    for i in range(N):
        if i in vs:
            continue
        stack = [(i+1, 0)] 
        r =  dfs_w(G, stack)
        if not r:
            break
    if r:
        print("Yes")
    else:
        print("No")