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



Sample input:
3 3
1 2 1
2 3 1
1 3 2


Sample output:
Yes

Submission status: Runtime Error

Solution code:
import math
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = math.inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == math.inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge


N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))
for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == math.inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      break
if judge:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
from math import inf
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge


N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))
for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      break
if judge:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
import sys
sys.getrecursionlimit(100000)

N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))


from math import inf
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge



for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      print("No")
      break
if judge:
  print("Yes")

Submission status: Runtime Error

Solution code:
N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))


from math import inf
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge



for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      print("No")
      break
if judge:
  print("Yes")

Submission status: Runtime Error

Solution code:
from math import inf
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge


N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))
for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      print("No")
      break
if judge:
  print("Yes")

Submission status: Runtime Error

Solution code:
from math import inf
class Vertice:
  def __init__(self, num):
    self.num = num
    self.connected = []
    self.edge = []
    self.pos = inf
    
def DFS(vertice):
  judge = True
  for con in vertice.connected:
    index = vertice.connected.index(con)
    if con.pos == inf:
      con.pos = vertice.pos + vertice.edge[index]
      judge = DFS(con)
    elif con.pos != vertice.pos + vertice.edge[index]:
      judge = False
    if judge == False:
      break
  return judge


N, M = (int(_) for _ in input().split())
vertices = [Vertice(i) for i in range(N)]
L, R, D = ([None for i in range(M)] for j in range(3))
for i in range(M):
  L[i], R[i], D[i] = (int(_) for _ in input().split())
for i in range(M):
  if not vertices[R[i] - 1] in vertices[L[i] - 1].connected:
    vertices[L[i] - 1].connected.append(vertices[R[i] - 1])
    vertices[R[i] - 1].connected.append(vertices[L[i] - 1])
    vertices[L[i] - 1].edge.append(D[i])
    vertices[R[i] - 1].edge.append(-D[i])
judge = True
for i in range(N):
  if vertices[i].pos == inf:
    vertices[i].pos = 0
    judge = DFS(vertices[i])
    if judge == False:
      print('No')
      break
if judge:
  print('Yes')

Submission status: Runtime Error

Solution code:
import sys                                                   
l1 = list(map(int, input().split()))                         
N = l1[0]                                                    
M = l1[1]                                                    
                                                             
dat = []                                                     
                                                             
per_dis = [ [] for n in range(N+1)]                          
                                                             
for n in range(M):                                           
    x = list(map(int, input().split()))                      
    dat.append(x)                                            
    per_dis[x[0]].append((x[1],x[2]))                        
    per_dis[x[1]].append((x[0],-x[2]))                       
                                                             
per_point = [ None for n in range(N+1)]                      
                                                             
if M != 0:                                                   
    visited = []                                             
    unvisited = [(dat[0][0], 0)]                             
    while unvisited:                                         
        now = unvisited[0]                                   
        unvisited = unvisited[1:]                            
        for next in per_dis[now[0]]:                         
            if next[0] not in visited:                       
                per_point[next[0]] = now[1] + next[1]        
                unvisited.append((next[0], now[1] + next[1]))
                visited.append(next[0])                      
                                                             
for x in dat:                                                
    if per_point[x[1]] - per_point[x[0]] != x[2]:            
        print("No")                                          
        sys.exit(0)                                          
print("Yes")

Submission status: Runtime Error

Solution code:
import sys                                               
l1 = list(map(int, input().split()))                     
N = l1[0]                                                
M = l1[1]                                                
                                                         
dat = []                                                 
                                                         
per_dis = [ [] for n in range(N+1)]                      
                                                         
for n in range(M):                                       
    x = list(map(int, input().split()))                  
    dat.append(x)                                        
    per_dis[x[0]].append((x[1],x[2]))                    
    per_dis[x[1]].append((x[0],-x[2]))                   
                                                         
per_point = [ None for n in range(N+1)]                  
                                                         
visited = []                                             
unvisited = [(dat[0][0], 0)]                             
while unvisited:                                         
    now = unvisited[0]                                   
    unvisited = unvisited[1:]                            
    for next in per_dis[now[0]]:                         
        if next[0] not in visited:                       
            per_point[next[0]] = now[1] + next[1]        
            unvisited.append((next[0], now[1] + next[1]))
            visited.append(next[0])                      
                                                         
for x in dat:                                            
    if per_point[x[1]] - per_point[x[0]] != x[2]:        
        print("No")                                      
        sys.exit(0)                                      
print("Yes")

Submission status: Runtime Error

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

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np] is None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    else:
      if dist[np]!=(dist[n]+nd):
        return False
  return True
ans=True
for i in range(1,N+1):
  if dist[i] is None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

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

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np] is None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    else:
      if dist[np]!=dist[n]+nd:
        return False
  return True
ans=True
for i in range(1,N+1):
  if dist[i] is None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
 import sys                                                                                   
 l1 = list(map(int, input().split()))                                                         
 N = l1[0]                                                                                    
 M = l1[1]                                                                                    
                                                                                              
 dat = []                                                                                     
                                                                                              
 #per_dis = [[ 0 for n in range(N+1)] for n in range(N+1)]                                    
                                                                                              
 per_dis = [ [] for n in range(N+1)]                                                          
                                                                                              
 for n in range(M):                                                                           
     x = list(map(int, input().split()))                                                      
     dat.append(x)                                                                            
                                                                                              
                                                                                              
 for x in dat:                                                                                
     if per_dis[x[0]][x[1]] == 0 and per_dis[x[1]][x[0]] == 0:                                
         per_dis[x[0]][x[1]] = x[2]                                                           
         per_dis[x[1]][x[0]] = -x[2]                                                          
         for i in range(1,N):                                                                 
             for j in range(i+1,N):                                                           
                 if per_dis[i][j] != 0 and per_dis[i+1][j+1] != 0 and per_dis[i][j+1] == 0:   
                     per_dis[i][j+1] = per_dis[i][j] + per_dis[i+1][j+1]                      
                     per_dis[j+1][i] = -per_dis[i][j+1]                                       
                 elif per_dis[i][j] == 0 and per_dis[i+1][j+1] != 0 and per_dis[i][j+1] != 0: 
                     per_dis[i][j] = per_dis[i][j+1] - per_dis[i+1][j+1]                      
                     per_dis[j][i] = -per_dis[i][j]                                           
                 elif per_dis[i][j] != 0 and per_dis[i+1][j+1] == 0 and per_dis[i][j+1] != 0: 
                     per_dis[i+1][j+1] = per_dis[i][j+1] - per_dis[i][j]                      
                     per_dis[j+1][i+1] = -per_dis[i+1][j+1]                                   
     else:                                                                                    
         if abs(per_dis[x[0]][x[1]]) != x[2]:                                                 
             print("No")                                                                      
             sys.exit(0)                                                                      
                                                                                              
 print("Yes")

Submission status: Runtime Error

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

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np] is None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    else:
      if dist[np]!=dist[n]+nd:
        return False
  return True

ans=True
for i in range(1,N+1):
  if dist[i] is None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

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

dist=[None]*(N+1)

def dfs(n):
  for np,nd in mp[n]:
    if dist[np]==None:
      dist[np]=dist[n]+nd
      if not dfs(np):
        return False
    elif dist[np]!=dist[n]+nd:
      return False
  return True

ans=True
for i in range(1,N+1):
  if dist[i]==None:
    dist[i]=0
    if not dfs(i):
      ans=False
      break
if ans:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [0 for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

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

f = False
#for i in range(1, N+1):
#    for t in G[i]:
#        if ((P[t[0]] - P[i]) != t[1]):
#            f = True
if (f):
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [0 for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

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

f = False
for i in range(1, N+1):
    for t in G[i]:
        if ((P[t[0]] - P[i]) != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

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

f = False
for i in range(1, N+1):
    for t in G[i]:
        if ((P[t[0]] - P[i]) != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

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

f = False
for i in range(1, N+1):
    for t in G[i]:
        if (P[t[0]] - P[i] != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

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

f = False
for i in range(N):
    for t in G[i]:
        if (P[t[0]] - P[i] != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
def dfs(s):
	global x, G
	for t, d in G[s]:
		if x[t] is None:
			x[t] = x[s] + d
			if not dfs(t):
				return False
		else:
			if x[t] != x[s] + d:
				return False
	return True

N,M = map(int,input().split())
x = [None] * N
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])

res = True
for i in range(N):
	if x[i] is None:
		x[i] = 0
		res &= dfs(i)

print("Yes" if res == True else "No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
inf = float('inf')

ans = True
def dfs(s):
    global ans
    global x
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                ans = False

N, M = map(int, input().split())
x = [inf] * N
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])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        dfs(i)
print(('No', 'Yes')[ans])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
inf = float('inf')

ans = True
def dfs(s):
    global ans
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                ans = False

N, M = map(int, input().split())
x = [inf] * N
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])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        dfs(i)
print(('No', 'Yes')[ans])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
inf = float('inf')

def dfs(s):
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

N, M = map(int, input().split())
x = [inf] * N
G = [[] for _ in range(N)]
ans = True
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])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        if not dfs(i):
            ans = False
print(('No', 'Yes')[ans])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
x = [0] * N
used = [False] * N
G = [[] for _ in range(N)]

def dfs(s):
    for t, d in G[s]:
        if not used[t]:
            used[t] = True
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

ans = True
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])
for i in range(N):
    if not used[i]:
        used[i] = True
        if not dfs(i):
            ans = False
            break
print(('No', 'Yes')[ans])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
N, M = map(int, input().split())
x = [-1] * N
used = [False] * N
G = [[] for _ in range(N)]

def dfs(s):
    for t, d in G[s]:
        if not used[t]:
            used[t] = True
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

ans = True
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])
for i in range(N):
    if not used[i]:
        used[i] = True
        x[i] = 0
        if not dfs(i):
            ans = False
            break
print(('No', 'Yes')[ans])

Submission status: Runtime Error

Solution code:
class UnionFind():
    def __init__(self, n):
        self.table = [i for i in range(n)]
        self.rank = [1 for _ in range(n)]

    def unite(self, x, y):
        # x,yの根を格納する 
        x = self.find(x)
        y = self.find(y)

        #同一グラフ内だった場合
        if x == y:
            return False

        if self.rank[x] > self.rank[y]:
            self.table[y] = x
        else:
            self.table[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1


    def find(self, x):
        """ xの根の値を返すメソッド """
        #xが根である時
        if self.table[x] == x:
            return x
        #xが根でない ==> 根へたどる&根へつけかえる
        self.table[x] = self.find(self.table[x])
        #根の値を返す
        return self.table[x]

    def SameGragh(self,x,y):
        """二つの値が同一グラフ内だった場合:True,違う場合:False"""
        if self.find(x) == self.find(y):
            return True
        else:
            return False

def dfs(i,goal,dis,used):
	used[i] = 1
	if goal == i:
		return dis

	num = -float('inf')

	for next_ver,d in E[i].items():
		if used[next_ver] == 0:
			num = max(dfs(next_ver,goal,dis+d,used),num)

	return num

N,M = map(int,input().split(" "))
tree = UnionFind(N+1)
E = [dict() for _ in range(N+1)]

for i in range(M):
    l,r,d = map(int,input().split(" "))
    if tree.SameGragh(l,r):
    	if d != dfs(l,r,0,[0]*(N+1)):
    		print("No")
    		exit(0)
    else:
    	E[l][r] = d
    	E[r][l] = -d
    	tree.unite(l,r)

print("Yes")

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
  N,M=map(int,input().split())
  if N*M==0:
    print("Yes")
    return
  X=np.zeros((M,N))
  D=np.zeros((M,1))
  for i in range(M):
    l,r,d=map(int,input().split())
    X[i,r-1]=1
    X[i,l-1]=-1
    D[i,0]=d
  if np.linalg.matrix_rank(X)==np.linalg.matrix_rank(np.c_[X,D]):
    print("Yes")
  else:print("No")
  return

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, parent, value):
        self.parent = parent
        self.value = value

class UnionFindTree:
    def __init__(self, size):
        self.table = [Node(i, 0) for i in range(size)]
    
    def hoge(self, x):
        while self.table[x].parent != self.table[self.table[x].parent].parent:
            parent = self.table[x].parent
            self.table[x].value += self.table[parent].value
            self.table[x].parent = self.table[parent].value

    def find_root(self, x):
        self.hoge(x)
        return self.table[x].parent

    def find_value(self, x):
        self.hoge(x)
        return self.table[x].value

    def union(self, x, y, d):
        r1 = self.find_root(x)
        r2 = self.find_root(y)
        v1 = self.find_value(x)
        v2 = self.find_value(y)
        print(r1, r2, v1, v2)
        if r1 != r2:
            self.table[r2].parent = r1
            self.table[r2].value = (v1 - v2) + d
        else:
            if (v2 - v1) != d:
                return False
        return True
    
    def __repr__(self):
        return repr(self.table)

def main():
    n, m = map(int, input().split())
    tree = UnionFindTree(n+1)
    for i in range(m):
        li, ri, di = map(int, input().split())
        if not tree.union(li, ri, di):
            print('No')
            return
    print('Yes')

main()

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, parent, value):
        self.parent = parent
        self.value = value

class UnionFindTree:
    def __init__(self, size):
        self.table = [Node(i, 0) for i in range(size)]
    
    def hoge(self, x):
        while self.table[x].parent != self.table[self.table[x].parent].parent:
            parent = self.table[x].parent
            self.table[x].value += self.table[parent].value
            self.table[x].parent += self.table[parent].value

    def find_root(self, x):
        self.hoge(x)
        return self.table[x].parent

    def find_value(self, x):
        self.hoge(x)
        return self.table[x].value

    def union(self, x, y, d):
        r1 = self.find_root(x)
        r2 = self.find_root(y)
        v1 = self.find_value(x)
        v2 = self.find_value(y)
        print(r1, r2, v1, v2)
        if r1 != r2:
            self.table[r2].parent = r1
            self.table[r2].value = (v1 - v2) + d
        else:
            if (v2 - v1) != d:
                return False
        return True
    
    def __repr__(self):
        return repr(self.table)

def main():
    n, m = map(int, input().split())
    tree = UnionFindTree(n+1)
    for i in range(m):
        li, ri, di = map(int, input().split())
        if not tree.union(li, ri, di):
            print('No')
            return
    print('Yes')

main()

Submission status: Runtime Error

Solution code:
import numpy as np

N,M=map(int,input().split())
X=np.zeros((M,N))
D=np.zeros((M,1))
for i in range(M):
  l,r,d=map(int,input().split())
  X[i,r-1]=1
  X[i,l-1]=-1
  D[i,0]=d
if np.linalg.matrix_rank(X)==np.linalg.matrix_rank(np.c_[X,D]):
  print("Yes")
else:print("No")