Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    n,m=map(int,input().split())
    g=[[] for i in range(n)]
    for i in range(m):
        a,b,c=map(int,input().split())
        a-=1
        b-=1
        g[a].append([b,c])
        g[b].append([a,-c])
    inf=10**10
    v=[inf]*n
    while max(v)==inf:
        s=v.index(inf)
        q=deque([s])
        v[s]=0
        while q:
            p=q.popleft()
            for i in g[p]:
                if v[i[0]]==inf:
                    v[i[0]]=v[p]+i[1]
                    q.append(i[0])
                else:
                    if v[i[0]]!=v[p]+i[1]:
                        return 'No'
    return 'Yes'
                            
  
print(main())


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    n,m=map(int,input().split())
    g=[[] for i in range(n)]
    for i in range(m):
        a,b,c=map(int,input().split())
        a-=1
        b-=1
        g[a].append([b,c])
        g[b].append([a,-c])
    inf=10**10
    v=[inf]*n
    while max(v)==inf:
        s=v.index(inf)
        q=deque([s])
        v[s]=0
        while q:
            p=q.popleft()
            for i in g[p]:
                if v[i[0]]==inf:
                    v[i[0]]=v[p]+i[1]
                    q.append(i[0])
                else:
                    if v[i[0]]!=v[p]+i[1]:
                        return 'No'
    return 'Yes'
                            
  
print(main())


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Buggy python submission (verdict: Runtime Error):
from collections import deque
def main():
    n,m=map(int,input().split())
    g=[[] for i in range(n)]
    for i in range(m):
        a,b,c=map(int,input().split())
        a-=1
        b-=1
        g[a].append([b,c])
        g[b].append([a,-c])
    inf=10**10
    v=[inf]*n
    while max(v)==inf:
        s=v.index(inf)
        q=deque([s])
        v[s]=0
        while q:
            p=q.pop_left()
            for i in g[p]:
                if v[i[0]]==inf:
                    v[i[0]]=v[p]+i[1]
                    q.append(i[0])
                else:
                    if v[i[0]]!=v[p]+i[1]:
                        return 'No'
    return 'Yes'
                            
  
print(main())


Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')

Buggy python submission (verdict: Runtime Error):
from collections import deque
def main():
    n,m=map(int,input().split())
    g=[[] for i in range(n)]
    for i in range(m):
        a,b,c=map(int,input().split())
        a-=1
        b-=1
        g[a].append([b,c])
        g[b].append([a,-c])
    inf=10**10
    v=[inf]*n
    while max(v)==inf:
        s=v.index(inf)
        q=deque([s])
        v[s]=0
        while q:
            p=q.pop_left()
            for i in g[p]:
                if v[i[0]]==inf:
                    v[i[0]]=v[p]+i[1]
                    p.append(i[0])
                else:
                    if v[i[0]]!=v[p]+i[1]:
                        return 'No'
    return 'Yes'
                            
  
print(main())

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 5+1)


def dfs(p):
    for edge in G[p]:
        if X[edge[0]] is not None:
            if X[edge[0]] - X[p] != edge[1]:
                print('No')
                exit()
        else:
            X[edge[0]] = X[p] + edge[1]
            dfs(edge[0])


N, M = map(int, input().split())
G = [[] for _ in range(N)]

for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append((R, D))
    G[R].append((L, -D))

X = [None] * N
for i in range(N):
    if X[i] is None:
        X[i] = 0
        dfs(i)

print('Yes')