Problem p03450 (python)

Fixed submission:
class UnionFindTree:
    def __init__(self, size):
        self.parent = list(range(size))
        self.dist = [0] * size

    def find_root(self, x):
        if self.parent[x] == x:
            return x, 0
        else:
            self.parent[x], d = self.find_root(self.parent[x])
            self.dist[x] += d
            return self.parent[x], self.dist[x]

    def union(self, x, y, d):
        r1, d1 = self.find_root(x)
        r2, d2 = self.find_root(y)
        if r1 != r2:
            self.parent[r2] = r1
            self.dist[r2] = (d1 - d2) + d
        else:
            if (d2 - d1) != d:
                return False
        return True

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

main()

Buggy submission (verdict: Runtime Error):
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()

Buggy submission (verdict: Runtime Error):
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()