Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        u,v,w=map(int,input().split())
        u-=1; v-=1
        E[v].append((u,w))
    # dfsで各連結成分毎に最長経路を計算していく
    # 仮に更新されるようなことがあれば矛盾
    dist=[INF]*n
    visited=[0]*n
    def dfs(v)->bool:
        d=0
        flag=False
        visited[v]=1
        for nv,w in E[v]:
            # ループ検出
            if(visited[nv] and dist[nv]==INF):
                return False
            # nv の最長経路を計算
            if(not dfs(nv)):
                return False
            else:
                if(not flag):
                    d=dist[nv]+w
                    flag=True
                else:
                    if(d!=dist[nv]+w):
                        return False
        dist[v]=d
        return True
    print("Yes" if(all(dfs(v) for v in range(n))) else "No")
resolve()

Fixed submission:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        u,v,w=map(int,input().split())
        u-=1; v-=1
        E[u].append((v,w))
        E[v].append((u,-w))

    pos=[INF]*n # position
    def dfs(v)->bool:
        if(pos[v]!=INF): return True
        pos[v]=0
        Q=[v]
        while(Q):
            v=Q.pop()
            for nv,w in E[v]:
                if(pos[nv]==INF):
                    pos[nv]=pos[v]+w
                    Q.append(nv)
                else:
                    if(pos[nv]!=pos[v]+w):
                        return False
        return True
    for v in range(n):
        if(not dfs(v)):
            print("No")
            return
    print("Yes")
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        u,v,w=map(int,input().split())
        u-=1; v-=1
        E[v].append((u,w))
    # dfsで各連結成分毎に最長経路を計算していく
    # 仮に更新されるようなことがあれば矛盾
    dist=[INF]*n
    visited=[0]*n
    def dfs(v)->bool:
        d=0
        flag=False
        visited[v]=1
        for nv,w in E[v]:
            # ループ検出
            if(visited[nv] and dist[nv]==INF):
                return False
            # nv の最長経路を計算
            if(not dfs(nv)):
                return False
            else:
                if(not flag):
                    d=dist[nv]+w
                    flag=True
                else:
                    if(d!=dist[nv]+w):
                        return False
        dist[v]=d
        return True
    print("Yes" if(all(dfs(v) for v in range(n))) else "No")
resolve()

Fixed submission:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(m):
        u,v,w=map(int,input().split())
        u-=1; v-=1
        E[u].append((v,w))
        E[v].append((u,-w))

    pos=[INF]*n # position
    def dfs(v)->bool:
        if(pos[v]!=INF): return True
        pos[v]=0
        Q=[v]
        while(Q):
            v=Q.pop()
            for nv,w in E[v]:
                if(pos[nv]==INF):
                    pos[nv]=pos[v]+w
                    Q.append(nv)
                else:
                    if(pos[nv]!=pos[v]+w):
                        return False
        return True
    for v in range(n):
        if(not dfs(v)):
            print("No")
            return
    print("Yes")
resolve()