Buggy python submission (verdict: Runtime Error):
# coding=utf-8

line = input().split(" ")
N = int(line[0])
M = int(line[1])

if M==0:
    print("Yes")
    exit()

l = []
for _ in range(M):
    line = input().split(" ")
    l.append((
        int(line[0])-1
        ,int(line[1])-1
        ,int(line[2])
    ))
l.sort()
o = l[0][0]
for i in range(M):
    l[i] = (
        l[i][0]-o
        ,l[i][1]-o
        ,l[i][2]
    )

def check(l):
    nl = [None for x in range(N)]
    nl[0] = 0

    flag = True
    while flag:
        flag = False
        ll = []
        for c in l:
            if nl[c[0]]!=None and nl[c[0]]>=0 and nl[c[1]]==None:
                nl[c[1]] = nl[c[0]]+c[2]
            elif nl[c[0]]==None and nl[c[1]]!=None and nl[c[1]]>=0:
                nl[c[0]] = nl[c[1]]-c[2]
            elif nl[c[0]]!=None and nl[c[0]]>=0 and nl[c[1]]!=None and nl[c[1]]>=0:
                if nl[c[1]]-nl[c[0]]==c[2]:
                    pass
                else:
                    print("No")
                    exit()
            else:
                ll.append(c)
                flag = True
        if len(l)==len(ll):
            break
        else:
            l = ll.copy()
    for x in nl:
        if x<0:
            print("No")
            exit()
    return l

while len(l)>0:
    l = check(l)
    if len(l)==0:
        break
    l.sort()
    o = l[0][0]
    for i in range(len(l)):
        l[i] = (
            l[i][0]-o
            ,l[i][1]-o
            ,l[i][2]
        )


print("Yes")


Fixed submission:
# -*- coding: utf-8 -*-

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 unite(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ 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)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

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

print("Yes")

Buggy python submission (verdict: Time Limit Exceeded):
# coding=utf-8

line = input().split(" ")
N = int(line[0])
M = int(line[1])

if M==0:
    print("Yes")
    exit()

l = []
for _ in range(M):
    line = input().split(" ")
    l.append((
        int(line[0])-1
        ,int(line[1])-1
        ,int(line[2])
    ))
l.sort()
o = l[0][0]
for i in range(M):
    l[i] = (
        l[i][0]-o
        ,l[i][1]-o
        ,l[i][2]
    )

nl = [-1 for x in range(N)]
nl[0] = 0

flag = True
while flag:
    flag = False
    ll = []
    for c in l:
        if nl[c[0]]>=0 and nl[c[1]]==-1:
            nl[c[1]] = nl[c[0]]+c[2]
        elif nl[c[0]]==-1 and nl[c[1]]>=0:
            nl[c[0]] = nl[c[1]]-c[2]
        elif nl[c[0]]>=0 and nl[c[1]]>=0:
            if nl[c[1]]-nl[c[0]]==c[2]:
                pass
            else:
                print("No")
                exit()
        else:
            ll.append(c)
            flag = True
    l = ll.copy()

print("Yes")


Fixed submission:
# -*- coding: utf-8 -*-

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 unite(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ 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)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

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

print("Yes")