Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
s = s[::-1]

dp = [0]*13
dp[0] = 1

for ind, i in enumerate(s):
    m = 10**ind%13
    tmp = [0]*13
    for j in range(10):
        if i!='?' and int(i)!=j:
            continue
        for k in range(13):
            tmp[k] += dp[(k-j*m%13)%13]
    dp = tmp
print(dp[5]%(10**9+7))


Fixed submission:
import numpy as np
s = input()

A = []
for i in range(6):
    m = [-(j*10**i%13)%13 for j in range(10)]
    tmp = []
    for j in m:
        tmp.append([[1 if (k-l)%13==j else 0 for k in range(13)] for l in range(13)])
    tmp.append([[1 if (k-l)%13 in m else 0 for k in range(13)] for l in range(13)])
    A.append(tmp)
A = np.array(A)

dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    i = int(i) if i!='?' else 10
    tmp = np.dot(A[ind%6, i, :, :], dp)
    dp = tmp%(10**9+7)
print(dp[5])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
s = input()
dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    tmp = np.zeros(13, dtype=int)
    for j in range(10):
        if i!='?' and int(i)!=j:
            continue
        tmp += np.roll(dp, j*m%13)
    dp = tmp%(10**9+7)
    m = 10*m%13
print(dp[5])


Fixed submission:
import numpy as np
s = input()

A = []
for i in range(6):
    m = [-(j*10**i%13)%13 for j in range(10)]
    tmp = []
    for j in m:
        tmp.append([[1 if (k-l)%13==j else 0 for k in range(13)] for l in range(13)])
    tmp.append([[1 if (k-l)%13 in m else 0 for k in range(13)] for l in range(13)])
    A.append(tmp)
A = np.array(A)

dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    i = int(i) if i!='?' else 10
    tmp = np.dot(A[ind%6, i, :, :], dp)
    dp = tmp%(10**9+7)
print(dp[5])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
s = s[::-1]

dp = [0]*13
dp[0] = 1

for ind, i in enumerate(s):
    m = 10**ind%13
    tmp = [0]*13
    for j in range(10):
        if i!='?' and int(i)!=j:
            continue
        for k in range(13):
            tmp[k] += dp[(k-j*m%13)%13]
    dp = tmp
print(dp[5]%(10**9+7))

Fixed submission:
import numpy as np
s = input()

A = []
for i in range(6):
    m = [-(j*10**i%13)%13 for j in range(10)]
    tmp = []
    for j in m:
        tmp.append([[1 if (k-l)%13==j else 0 for k in range(13)] for l in range(13)])
    tmp.append([[1 if (k-l)%13 in m else 0 for k in range(13)] for l in range(13)])
    A.append(tmp)
A = np.array(A)

dp = np.zeros(13, dtype=int)
dp[0] = 1
m = 1
for ind, i in enumerate(reversed(s)):
    i = int(i) if i!='?' else 10
    tmp = np.dot(A[ind%6, i, :, :], dp)
    dp = tmp%(10**9+7)
print(dp[5])
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
di = {}
for i in a:
    if i in di:
        di[i] += 1
    else:
        di[i] = 1
k = list(di.keys())
v = list(di.values())
if n%3!=0 and len(di)>1:
    print('No')
elif len(di)==3 and v[0]==v[1]==v[2] and k[0]^k[1]^k[2]==0:
    print('Yes')
elif len(di)==2 and (v[0]<v[1] and v[0]==n//3 and k[0]==0) or (v[0]>v[1] and v[1]==n//3 and k[1]==0):
    print('Yes')
elif len(di)==1 and k[0]==0:
    print('Yes')
else:
    print('No')

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
di = {}
for i in a:
    if i in di:
        di[i] += 1
    else:
        di[i] = 1
k = list(di.keys())
v = list(di.values())
if n%3!=0 and len(di)>1:
    print('No')
elif len(di)==3 and v[0]==v[1]==v[2] and k[0]^k[1]^k[2]==0:
    print('Yes')
elif len(di)==2 and ((v[0]<v[1] and v[0]==n//3 and k[0]==0) or (v[0]>v[1] and v[1]==n//3 and k[1]==0)):
    print('Yes')
elif len(di)==1 and k[0]==0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
xy = []
for _ in range(n):
    x, y = map(int, input().split())
    xy.append((x, y))
dxdy = {}
for i in range(n-1):
    for j in range(i+1, n):
        dx = xy[i][0] - xy[j][0]
        dy = xy[i][1] - xy[j][1]
        if dx<0:
            dx, dy = -dx, -dy
        if (dx, dy) in dxdy:
            dxdy[(dx, dy)] += 1
        else:
            dxdy[(dx, dy)] = 1
print(n-max(dxdy.values()))

Fixed submission:
n = int(input())
xy = []
for _ in range(n):
    x, y = map(int, input().split())
    xy.append((x, y))
dxdy = {}
if n==1:
    print(1)
    exit()
for i in range(n-1):
    for j in range(i+1, n):
        dx = xy[i][0] - xy[j][0]
        dy = xy[i][1] - xy[j][1]
        if dx<0:
            dx, dy = -dx, -dy
        elif dx==0:
            dx, dy = dx, abs(dy)
        if (dx, dy) in dxdy:
            dxdy[(dx, dy)] += 1
        else:
            dxdy[(dx, dy)] = 1
print(n-max(dxdy.values()))
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = []
for _ in range(m):
    a.append(int(input()))
dp = [0]*(n+1)
dp[0] = 1
for i in range(n):
    if i+1 not in a:
        dp[i+1] += dp[i]
    if i<n-1 and i+2 not in a:
        dp[i+2] += dp[i]
print(dp[-1]%(10**9+7))


Fixed submission:
n, m = map(int, input().split())
a = []
for _ in range(m):
    a.append(int(input()))
a = set(a)
dp = [0]*(n+1)
dp[0] = 1
for i in range(n):
    if i+1 not in a:
        dp[i+1] += dp[i]
    if i<n-1 and i+2 not in a:
        dp[i+2] += dp[i]
print(dp[-1]%(10**9+7))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = []
for _ in range(m):
    a.append(int(input()))
dp = [0]*(n+1)
dp[0] = 1
for i in range(n):
    if i+1 not in a:
        dp[i+1] += dp[i]
    if i<n-1 and i+2 not in a:
        dp[i+2] += dp[i]
print(dp[-1]%(10**9+7))

Fixed submission:
n, m = map(int, input().split())
a = []
for _ in range(m):
    a.append(int(input()))
a = set(a)
dp = [0]*(n+1)
dp[0] = 1
for i in range(n):
    if i+1 not in a:
        dp[i+1] += dp[i]
    if i<n-1 and i+2 not in a:
        dp[i+2] += dp[i]
print(dp[-1]%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
r = [[0]*w for _ in range(h)]
for i in range(h):
    flg = False
    row = s[i]
    row2 = r[i]
    for j in range(w):
        if row[j]=='.':
            if not flg:
                tmp = 0
                begin = j
                flg = True
            tmp += 1
        if flg and row[j]=='#':
            for k in range(begin, j):
                row2[k] = tmp
            flg = False
    if flg:
        for k in range(begin, w):
            row2[k] = tmp
c = [[0]*w for _ in range(h)]
for i in range(w):
    flg = False
    for j in range(h):
        if s[j][i]=='.':
            if not flg:
                tmp = 0
                begin = j
                flg = True
            tmp += 1
        if flg and s[j][i]=='#':
            for k in range(begin, j):
                c[k][i] = tmp
            flg = False
    if flg:
        for k in range(begin, h):
            c[k][i] = tmp
ans = 0
for row1, row2 in zip(r, c):
    for e1, e2 in zip(row1, row2):
        if ans<e1+e2-1:
            ans = e1+e2-1
print(ans)

Fixed submission:
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
r = [[0]*w for _ in range(h)]
for i in range(h):
    flg = False
    row = s[i]
    row2 = r[i]
    for j in range(w):
        if row[j]=='.':
            if not flg:
                tmp = 0
                begin = j
                flg = True
            tmp += 1
        if flg and row[j]=='#':
            for k in range(begin, j):
                row2[k] = tmp
            flg = False
    if flg:
        for k in range(begin, w):
            row2[k] = tmp
c = [[0]*w for _ in range(h)]
for i in range(w):
    flg = False
    for j in range(h):
        if s[j][i]=='.':
            if not flg:
                tmp = 0
                begin = j
                flg = True
            tmp += 1
        if flg and s[j][i]=='#':
            for k in range(begin, j):
                c[k][i] = tmp
            flg = False
    if flg:
        for k in range(begin, h):
            c[k][i] = tmp
ans = 0
for row1, row2 in zip(r, c):
    for e1, e2 in zip(row1, row2):
        if ans<e1+e2-1:
            ans = e1+e2-1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
stat = 0
a = 0
bc = 0
abc = 0
ans = 0
def count():
    global ans, a, bc, abc
    newa = a+abc
    cnt = abc
    while cnt>0:
        ans += cnt
        cnt -= 1
        if a>0:
            a -= 1
            cnt += 1
        if bc>0:
            bc -= 1
            cnt += 1
    a = newa
    bc = 0
    abc = 0
i = 0
while i<n:
    if stat==0:
        if s[i:i+3]=='ABC':
            a = 0
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a = 1
            stat = 1
            i += 1
        else:
            i += 1
    elif stat==1:
        if s[i:i+3]=='ABC':
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a += 1
            i += 1
        else:
            stat = 0
    elif stat==2:
        if s[i:i+3]=='ABC':
            abc += 1
            i += 3
        elif s[i:i+2]=='BC':
            bc = 1
            stat = 3
            i += 2
        else:
            count()
            stat = 1
    else:
        if s[i:i+2]=='BC':
            bc += 1
            i += 2
        else:
            count()
            stat = 1
if stat>=2:
    count()
print(ans)

Fixed submission:
s = input()
n = len(s)
stat = 0
a = 0
bc = 0
abc = 0
ans = 0
def count():
    global ans, a, bc, abc
    newa = a+abc
    cnt = abc
    while cnt>0:
        ans += cnt
        cnt -= 1
        if a>0:
            a -= 1
            cnt += 1
        if bc>0:
            bc -= 1
            cnt += 1
    a = newa
    bc = 0
    abc = 0
i = 0
while i<n:
    if stat==0:
        if s[i:i+3]=='ABC':
            a = 0
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a = 1
            stat = 1
            i += 1
        else:
            i += 1
    elif stat==1:
        if s[i:i+3]=='ABC':
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a += 1
            i += 1
        else:
            stat = 0
    elif stat==2:
        if s[i:i+3]=='ABC':
            abc += 1
            i += 3
        elif s[i:i+2]=='BC':
            bc = 1
            stat = 3
            i += 2
        else:
            count()
            stat = 1
    else:
        if s[i:i+2]=='BC':
            bc += 1
            i += 2
        else:
            count()
            stat = 1
if stat>=2:
    count()
print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
xy = []
for _ in range(m):
    x, y, _ = map(int, input().split())
    xy.append((x-1, y-1))

class UnionFind():
    def __init__(self, n):
        self.li = list(range(n))
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return
        self.li[ry] = rx
    def same(self, x, y):
        return self.root(x)==self.root(y)

uf = UnionFind(n)
for x, y in xy:
    uf.unite(x, y)
for i in range(n):
    uf.root(i)
print(len(set(uf.li)))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n, m = map(int, input().split())
xy = []
for _ in range(m):
    x, y, _ = map(int, input().split())
    xy.append((x-1, y-1))

class UnionFind():
    def __init__(self, n):
        self.li = list(range(n))
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return
        self.li[ry] = rx
    def same(self, x, y):
        return self.root(x)==self.root(y)

uf = UnionFind(n)
for x, y in xy:
    uf.unite(x, y)
for i in range(n):
    uf.root(i)
print(len(set(uf.li)))
Buggy python submission (verdict: Runtime Error):
n = int(input())
ans = 0
for i in range(1, int(n**0.5)+1):
    if n%i==0:
        if i>1 and n//(i-1)==n%(i-1):
            ans += i-1
        if n//(n//i-1)==n%(n//i-1):
            ans += n//i-1
print(ans)

Fixed submission:
n = int(input())
factors = []
for i in range(1, int(n**0.5)+1):
    if n%i==0:
        if n//i*2<(4*n+1)**0.5-1:
            factors.append(i)
        if i*2<(4*n+1)**0.5-1:
            factors.append(n//i)
print(sum(factors)-len(factors))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = []
for _ in range(h):
    a.append(list(input()))
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            q.append((i, j, 0))
dxdy = [(-1, 0), (0, -1), (0, 1), (1, 0)]
ans = 0
while q:
    y, x, cnt = q.popleft()
    for dx, dy in dxdy:
        if not(0<=x+dx<w and 0<=y+dy<h):
            continue
        if a[y+dy][x+dx]=='.':
            a[y+dy][x+dx] = '#'
            q.append((y+dy, x+dx, cnt+1))
            ans = cnt+1
print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = []
for _ in range(h):
    a.append(list(input()))
q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
            q.append((i, j, 0))
dxdy = [(-1, 0), (0, -1), (0, 1), (1, 0)]
ans = 0
while q:
    y, x, cnt = q.popleft()
    for dx, dy in dxdy:
        if not(0<=x+dx<w and 0<=y+dy<h):
            continue
        if a[y+dy][x+dx]=='.':
            a[y+dy][x+dx] = '#'
            q.append((y+dy, x+dx, cnt+1))
            ans = cnt+1
print(ans)
Buggy python submission (verdict: Runtime Error):
a, b, t = map(int, input.split())
ans = 0
for i in range(a, t+1):
  ans += b
print(ans)

Fixed submission:
a, b, t = map(int, input().split())
print(t//a*b)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
ab = []
for _ in range(m):
    a, b = map(int, input().split())
    ab.append((a-1, b-1))

class UnionFind():
    def __init__(self, li):
        self.li = li
        self.cnt = [1]*len(li)
        self.ans = 0
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return self.ans
        self.li[ry] = rx
        self.ans -= self.cnt[rx]*(self.cnt[rx]-1)//2
        self.ans -= self.cnt[ry]*(self.cnt[ry]-1)//2
        self.cnt[rx] += self.cnt[ry]
        self.ans += self.cnt[rx]*(self.cnt[rx]-1)//2
        return self.ans
uf = UnionFind(list(range(n)))
ans = [n*(n-1)//2]
for a, b in ab[::-1][:-1]:
    ans.append(n*(n-1)//2-uf.unite(a, b))
for i in reversed(ans):
    print(i)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n, m = map(int, input().split())
ab = []
for _ in range(m):
    a, b = map(int, input().split())
    ab.append((a-1, b-1))

class UnionFind():
    def __init__(self, li):
        self.li = li
        self.cnt = [1]*len(li)
        self.ans = 0
    def root(self, x):
        if self.li[x]==x:
            return x
        self.li[x] = self.root(self.li[x])
        return self.li[x]
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx==ry:
            return self.ans
        self.li[ry] = rx
        self.ans -= self.cnt[rx]*(self.cnt[rx]-1)//2
        self.ans -= self.cnt[ry]*(self.cnt[ry]-1)//2
        self.cnt[rx] += self.cnt[ry]
        self.ans += self.cnt[rx]*(self.cnt[rx]-1)//2
        return self.ans
uf = UnionFind(list(range(n)))
ans = [n*(n-1)//2]
for a, b in ab[::-1][:-1]:
    ans.append(n*(n-1)//2-uf.unite(a, b))
for i in reversed(ans):
    print(i)
Buggy python submission (verdict: Runtime Error):
import sys
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    graph[x].append(y)

dp = [-1]*n

def DFS(node):
    ma = -1
    for c_node in graph[node]:
        if dp[c_node]>=0:
            tmp = dp[c_node]
        else:
            tmp = DFS(c_node)
        if tmp > ma:
            ma = tmp
    dp[node] = ma+1
    return ma+1

sys.setrecursionlimit(2*n)

for i in range(n):
    if dp[i]>=0:
        continue
    DFS(i)
print(max(dp))

Fixed submission:
import sys
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    graph[x].append(y)

dp = [-1]*n

def DFS(node):
    ma = -1
    for c_node in graph[node]:
        if dp[c_node]>=0:
            tmp = dp[c_node]
        else:
            tmp = DFS(c_node)
        if tmp > ma:
            ma = tmp
    dp[node] = ma+1
    return ma+1

sys.setrecursionlimit(10**9)

for i in range(n):
    if dp[i]>=0:
        continue
    DFS(i)
print(max(dp))
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
for i in range(m//n, 0, -1):
    rem = m-i*n
    if rem%i==0:
        print(i)
        break

Fixed submission:
n, m = map(int, input().split())
for i in range(m//n, 0, -1):
    rem = m-i*n
    if rem%i==0:
        print(i)
        break
Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
for i, c in enumerate(s):
    if int(c)>1:
        ind = i+1
        n = int(c)
        break
else:
    ind = len(s)

if k<ind:
    print(1)
else:
    print(n)

Fixed submission:
s = input()
k = int(input())
for i, c in enumerate(s):
    if int(c)>1:
        if k<i+1:
            print(1)
        else:
            print(int(c))
        break
else:
    print(1)

Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
for i, c in enumerate(s):
    if int(c)>1:
        ind = i+1
        n = int(c)
        break

if k<ind:
    print(1)
else:
    print(n)

Fixed submission:
s = input()
k = int(input())
for i, c in enumerate(s):
    if int(c)>1:
        if k<i+1:
            print(1)
        else:
            print(int(c))
        break
else:
    print(1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
s = [[0]*20]
for i in a:
    tmp = []
    for j in range(20):
        tmp.append((i>>j&1)+s[-1][j])
    s.append(tmp)
l = 0
r = 0
ans = 0
while l < n:
    for j in range(20):
        if s[r+1][j]-s[l][j]>1:
            ans += r-l
            l += 1
            break
    else:
        if r < n-1:
            r += 1
        else:
            ans += n-l
            l += 1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
s = [[0]*20]
for i in a:
    tmp = []
    for j in range(20):
        tmp.append((i>>j&1)+s[-1][j])
    s.append(tmp)
l = 0
r = 0
ans = 0
while l < n:
    for j in range(20):
        if s[r+1][j]-s[l][j]>1:
            ans += r-l
            l += 1
            break
    else:
        if r < n-1:
            r += 1
        else:
            ans += n-l
            l += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
accu = [0]*20
ans = 0
r = 0
for l in range(n):
    while r<n:
        tmp = accu[:]
        for i in range(20):
            tmp[i] += a[r]>>i&1
        if max(tmp)==2:
            break
        accu = tmp
        r += 1
    ans += r-l
    for i in range(20):
        accu[i] -= a[l]>>i&1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
accu = [0]*20
ans = 0
r = 0
for l in range(n):
    while r<n:
        tmp = accu[:]
        for i in range(20):
            tmp[i] += a[r]>>i&1
        if max(tmp)==2:
            break
        accu = tmp
        r += 1
    ans += r-l
    for i in range(20):
        accu[i] -= a[l]>>i&1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve(bi, ei, bs):
    mid = (bi+ei)//2
    print(mid, flush=True)
    s = input()
    if s=='Vacant':
        exit(0)
    elif (s==bs)^((mid-bi)%2):
        solve(mid, ei, s)
    else:
        solve(bi, mid, bs)

n = int(input())
print(0, flush=True)
s = input()
if s=='Vacant':
    exit(0)
solve(0, n, s)

Fixed submission:
def solve(bi, ei, bs):
    mid = (bi+ei)//2
    print(mid, flush=True)
    s = input()
    if s=='Vacant':
        exit(0)
    elif (s==bs)^((mid-bi)%2):
        solve(mid, ei, s)
    else:
        solve(bi, mid, bs)

n = int(input())
print(0, flush=True)
s = input()
if s=='Vacant':
    exit(0)
solve(0, n, s)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    graph[l-1].append((r-1, d))
    graph[r-1].append((l-1, -d))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')

Fixed submission:
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    graph[l-1].append((r-1, d))
    graph[r-1].append((l-1, -d))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        if seen[node]:
            continue
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    graph[l-1].append((r-1, d))
    graph[r-1].append((l-1, -d))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')

Fixed submission:
from collections import deque
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    graph[l-1].append((r-1, d))
    graph[r-1].append((l-1, -d))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        if seen[node]:
            continue
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')