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.table = [[-1, 0] for _ in range(size + 1)]

    def find(self, x):
        n, d = self.table[x] 
        if n < 0:
            return [x, d]
        else:
            n1, d1 = self.find(n)
            r = [n1, d1 + d]
            self.table[x] = r
            return r

    
    def union(self, x, y, d):
        n1, d1 = self.find(x)
        n2, d2 = self.find(y)
        if n1 != n2:
            if self.table[n1][0] <= self.table[n2][0]:
                
                self.table[n2] = [n1, d1+d]
            else:
                self.table[n2][0] -= 1
                self.table[n1] = [n2, d2-d]
        else:
            print(d1, d2, d)
            if d2 - d1 != d:
                return False
        return True

def solve(N, M, lrd):
    uf = UnionFind(N)
    for l, r, d in lrd:
        if not uf.union(l, r, d):
            return False
    return True

if __name__ == "__main__":
    N, M = [int(x) for x in input().split()]
    lrd = []
    for i in range(M):
        lrd.append([int(x) for x in input().split()])
    if solve(N, M, lrd):
        print("Yes")
    else:
        print("No")


import sys

sys.setrecursionlimit(100000)

class UnionFind:
    def __init__(self, size):
        self.table = [[-1, 0] for _ in range(size + 1)]

    def find(self, x):
        n, d = self.table[x] 
        if n < 0:
            return [x, d]
        else:
            n1, d1 = self.find(n)
            r = [n1, d1 + d]
            self.table[x] = r
            return r

    
    def union(self, x, y, d):
        n1, d1 = self.find(x)
        n2, d2 = self.find(y)
        if n1 != n2:
            if self.table[n1][0] <= self.table[n2][0]:
                
                self.table[n1][0] -= 1
                self.table[n2] = [n1, d1+d]
            else:
                self.table[n2][0] -= 1
                self.table[n1] = [n2, d2-d]
        else:
            print(d1, d2, d)
            if d2 - d1 != d:
                return False
        return True

def solve(N, M, lrd):
    uf = UnionFind(N)
    for l, r, d in lrd:
        if not uf.union(l, r, d):
            return False
    return True

if __name__ == "__main__":
    N, M = [int(x) for x in input().split()]
    lrd = []
    for i in range(M):
        lrd.append([int(x) for x in input().split()])
    if solve(N, M, lrd):
        print("Yes")
    else:
        print("No")


from collections import defaultdict

class XUnionFind(object):

    def __init__(self):
        self.table = defaultdict(lambda:-1)
        self.dtable = defaultdict(lambda:0)

    def find(self, x):
        parent = self.table[x] 
        if parent < 0:
            return x, 0
        else:
            root, distance = self.find(parent)
            new_distance = distance + self.dtable[x]
            self.table[x] = root
            self.dtable[x] = new_distance
            return root, new_distance

    def union(self, x1, x2, d = 0):
        root1, distance1 = self.find(x1)
        root2, distance2 = self.find(x2)
        if root1 == root2:
            if distance2 - distance1 != d:
                return False
            return True
        if root1 < root2:
            self.table[root1] += self.table[root2]
            self.table[root2] = root1
            self.dtable[root2] = distance1 + d - distance2
        else:
            self.table[root2] += self.table[root1]
            self.table[root1] = root2
            self.dtable[root1] = distance2 - d - distance1
        return True

def solve(N, M, lrd):
    uf = XUnionFind()
    for l, r, d in lrd:
        rc = uf.union(l, r, d)
        if rc == False:
            return False
    return True
        

if __name__ == "__main__":
    N, M = [int(x) for x in input().split()]
    lrd = []
    for i in range(M):
        lrd.append([int(x) for x in input().split()])
    if solve(N, M, lrd):
        print("Yes")
    else:
        print("No")


Verdicts: Wrong Answer

Wrong Answer

Accepted