Problem: There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.  You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.  It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Submission attempts (in order):
n, m = map(int, input().split())
par = [i for i in range(n)]
diff = [0]*n

def find(x):
    if par[x] == x:
        return x, 0
    else:
        a, b = find(par[x])
        par[x] = a
        diff[x] += b
        return a, diff[x]


def unite(x,y,z):
    s, ws = find(x)
    t, wt = find(y)
    #print(ws, wt)
    if s==t:
        if ws - wt == z:
            pass
        else:
            print('No')
            exit(0)
    else:
        par[s] = t
        diff[s] = z

unites =[]

for _ in range(m):
    l,r,d = map(int, input().split())
    unites.append((l,r,d))
    unite(l-1,r-1,d)
#print(unites)

for x, y, z in unites:
    unite(x-1, y-1, d)

print('Yes')

n, m = map(int, input().split())
par = [i for i in range(n)]
diff = [0]*n

def find(x):
    if par[x] == x:
        return x, 0
    else:
        a, b = find(par[x])
        par[x] = a
        diff[x] += b
        return a, diff[x]


def unite(x,y,z):
    s, ws = find(x)
    t, wt = find(y)
    #print(ws, wt)
    if s==t:
        if ws - wt == z:
            pass
        else:
            print('Yes')
            exit(0)
    else:
        par[s] = t
        diff[s] = z

for _ in range(m):
    l,r,d = map(int, input().split())
    unite(l-1,r-1,d)

print('No')

n, m = map(int, input().split())
par = [i for i in range(n)]
diff = [0]*n

def find(x):
    if par[x] == x:
        return x, 0
    else:
        a, b = find(par[x])
        par[x] = a
        diff[x] += b
        return a, diff[x]


def unite(x,y,z):
    s, ws = find(x)
    t, wt = find(y)
    #print(ws, wt)
    if s==t:
        if ws - wt == z:
            pass
        else:
            print('No')
            exit(0)
    else:
        par[s] = t
        diff[s] = z

unites =[]
for _ in range(m):
    l,r,d = map(int, input().split())
    unites.append((l,r,d))
    unite(l-1,r-1,d)
for x, y, z in unites:
    unites(x-1, y-1, d)

print('Yes')

n, m = map(int, input().split())
par = [i for i in range(n)]
diff = [0] * n


def find(x):
    if par[x] == x:
        return x, 0
    else:
        a, b = find(par[x])
        par[x] = a
        diff[x] += b
        return a, diff[x]


def unite(x, y, z):
    s, ws = find(x)
    t, wt = find(y)
    # print(ws, wt)
    if s == t:
        if ws - wt == z:
            pass
        else:
            print('No')
            exit(0)
    else:
        if ws < z + wt:
            par[s] = t
            diff[s] = z + wt - ws
        else:
            par[t] = s
            diff[t] = ws - z - wt


for _ in range(m):
    l, r, d = map(int, input().split())
    unite(l - 1, r - 1, d)

print('Yes')

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted