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

Submission attempts (in order):
class UnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]
        self.diff_potential = [0 for i in range(size)]

    def root(self, x): 
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) 
            self.diff_potential[x] += self.diff_potential[self.parent[x]] 
            self.parent[x] = rx 
            return rx

    def merge(self, x, y, diffxy): 
        diffxy = self.diff_potential[x] + diffxy - self.diff_potential[y] 
        x = self.root(x) 
        y = self.root(y) 
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: 
            x,y=y,x
            diffxy = -diffxy
        self.parent[x] += self.parent[y] 
        self.parent[y] = x 
        self.diff_potential[y] = diffxy 
        return True

    def issame(self, x, y): 
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): 
        if self.root(x) == self.root(y): 
            return self.diff_potential[y] - self.diff_potential[x]
        else:
            return None

    def size(self,x): 
        return -self.parent[self.root(x)]
#>>>>>>>>>>>>>>EhdCutHere>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

uf = UnionFind(n)
ans = True
for i in range(m):
    l,r,d=list(map(int,input().split()))
    if uf.issame(l,r):
        ans = d==uf.diff(l,r)
    else:
        uf.merge(l,r,d)
    if ans == False:
        break

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






class PotUnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]
        self.diff_p = [0 for i in range(size)]

    def root(self, x): 
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) 
            self.diff_p[x] += self.diff_p[self.parent[x]] 
            self.parent[x] = rx 
            return rx

    def merge(self, x, y, dxy): 
        dxy = self.diff_p[x] + dxy - self.diff_p[y] 
        x = self.root(x) 
        y = self.root(y) 
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: 
            x,y=y,x
            dxy = -dxy
        self.parent[x] += self.parent[y] 
        self.parent[y] = x 
        self.diff_p[y] = dxy 
        return True

    def issame(self, x, y): 
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): 
        if self.root(x) == self.root(y): 
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

    def size(self,x): 
        return -self.parent[self.root(x)]
#>>>>>>>>>>>>>>EhdCutHere>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

uf = PotUnionFind(n)
ans = True
for i in range(m):
    l,r,d=list(map(int,input().split()))
    l-=1;r-=1;
    if uf.issame(l,r):
        ans = d==uf.diff(l,r)
    else:
        uf.merge(l,r,d)
    if ans == False:
        break

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






Verdicts: Runtime Error

Accepted