Problem:
 Score : 400 points    Problem Statement 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.      Constraints  1 \leq N \leq 100  000  0 \leq M \leq 200  000  1 \leq L_i, R_i \leq N ( 1 \leq i \leq M )  0 \leq D_i \leq 10  000 ( 1 \leq i \leq M )  L_i \neq R_i ( 1 \leq i \leq M )  If i \neq j , then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j) .  D_i are integers.         Input Input is given from Standard Input in the following format:  N  M  L_1  R_1  D_1  L_2  R_2  D_2  :  L_M  R_M  D_M       Output If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes ; if it does not exist, print No .        Sample Input 1 3 3
1 2 1
2 3 1
1 3 2      Sample Output 1 Yes  Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103) .       Sample Input 2 3 3
1 2 1
2 3 1
1 3 5      Sample Output 2 No  If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.       Sample Input 3 4 3
2 1 1
2 3 5
3 4 2      Sample Output 3 Yes       Sample Input 4 10 3
8 7 100
7 9 100
9 8 100      Sample Output 4 No       Sample Input 5 100 0      Sample Output 5 Yes

Solution (Python):
N,M = map(int,input().split())
LRD = [list(map(int,input().split())) for _ in [0]*M]

E = [{} for _ in [0]*N]
for L,R,D in LRD:
    E[L-1][R-1] = D
    E[R-1][L-1] = -D

V = [None]*N
breakFlg = False
for i in range(N):
    if V[i]!=None:continue
    V[i] = 0
    q = [i]
    while q:
        j = q.pop()
        v = V[j]
        for k,d in E[j].items():
            if V[k] == None:
                V[k] = v+d
                q.append(k)
ans = True
for l,e in enumerate(E):
    for r,d in e.items():
        ans = ans and V[r]-V[l]==d
    if not ans:break
print("Yes" if ans else "No")

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def f(v,p,G,T):
    global ans
    INF = 10**10
    for C in G[v]:
        c,d = C[0],C[1]
        if T[c]==INF:
            T[c] = T[v]+d
            f(c,v,G,T)
        elif T[c]!=T[v]+d:
            ans = "No"
def main():
    global ans
    N,M = map(int,input().split())
    G = [[] for i in range(N+1)]
    for i in range(M):
        L,R,D = map(int,input().split())
        G[L].append([R,D])
        G[R].append([L,-D])
    INF = 10**10
    T = [INF]*(N+1)
    while T.count(INF)!=0:
        for i in range(N+1):
            if T[i] == INF:
                T[i]=0
                f(i,-1,G,T)
        if ans=="No":break
ans = "Yes"
main()
print(ans)

Solution (Python):
def solve(n, query):
    for l, r, d in query:
        if is_same(l, r):
            if d != diff(l, r):
                return 'No'
        else:
            unite(l, r, d)
    return 'Yes'


def unite(x, y, w):
    w += diff_weight[x]-diff_weight[y]
    rt_x = find(x)
    rt_y = find(y)

    if rt_x == rt_y:
        return
    if rank[rt_x] >= rank[rt_y]:
        par[rt_y] = rt_x
        diff_weight[rt_y] = w
        if rank[rt_x] == rank[rt_y]:
            rank[rt_x] += 1
    else:
        par[rt_x] = rt_y
        diff_weight[rt_x] = -w


def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        diff_weight[x] += diff_weight[par[x]]
        par[x] = px
        return px


def weight(x):
    find(x)
    return diff_weight[x]


def is_same(x, y):
    return find(x) == find(y)


def diff(x, y):
    return weight(y)-weight(x)


if __name__ == '__main__':
    n, m = map(int, input().split())
    par, rank = [-1]*(n+1), [0]*(n+1)
    diff_weight = [0]*(n+1)
    query = []
    for _ in range(m):
        l, r, d = map(int, input().split())
        query.append((l, r, d))
    print(solve(n, query))

Solution (Python):
def solve(n, query):
    for l, r, d in query:
        if is_same(l, r):
            if d != diff(l, r):
                return 'No'
        else:
            unite(l, r, d)
    return 'Yes'


def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        diff_weight[x] += diff_weight[par[x]]
        par[x] = px
        return px


def weight(x):
    find(x)
    return diff_weight[x]


def unite(x, y, w):
    w += diff_weight[x]-diff_weight[y]
    x = find(x)
    y = find(y)
    if x == y:
        return False
    else:
        if par[x] > par[y]:
            x, y = y, x
            w = -w
        par[x] += par[y]
        par[y] = x
        diff_weight[y] = w
        return True


def is_same(x, y):
    return find(x) == find(y)


def diff(x, y):
    return weight(y)-weight(x)


if __name__ == '__main__':
    n, m = map(int, input().split())
    par = [-1]*(n+1)
    diff_weight = [0]*(n+1)
    query = []
    for _ in range(m):
        l, r, d = map(int, input().split())
        query.append((l, r, d))
    print(solve(n, query))

Solution (Python):
def main():
	N, M = map(int,input().split())
	if M == 0:
		print("Yes")
		return
	x = [[] for _ in range(N)]
	ans = [0 for _ in range(N)]
	for i in range(M):
		l, r, d = map(int, input().split())
		l, r = l-1, r-1
		x[l].append([r, d])
		x[r].append([l, -d])

	visited = [False for _ in range(N)]

	def dfs(i):
		vs = [i]
		while vs:
			v = vs.pop()
			for nex in x[v]:
				if not visited[nex[0]]:
					visited[nex[0]] = True
					ans[nex[0]] = ans[v]+nex[1]
					vs.append(nex[0])
				else:
					if ans[nex[0]] != ans[v]+nex[1]:
						return False
		return True

	ans_exist = True
	for i in range(N):
		if not visited[i]:
			visited[i] = True
			ans_exist = dfs(i)
		if not ans_exist:
			break

	if ans_exist:
		print("Yes")
	else:
		print("No")


if __name__ == '__main__':
    main()

Solution (Python):
N,M=map(int,input().split())
LRD=[list(map(int,input().split())) for i in range(M)]
RDW=[[i,0,0] for i in range(N)]
ff=[0]
def compare(p,q,l):
    l_temp1=0
    l_temp2=0
    flag=0
    while(True):
        if q==p:
            flag=1
            break
        temp=RDW[q][0]
        if temp==q:
            break
        else:
            l_temp1+=RDW[q][2]
            q=temp
    if flag==0:
        while(True):
            if p==q:
                flag=1
                break
            temp=RDW[p][0]
            if temp==p:
                break
            else:
                l_temp2+=RDW[p][2]
                p=temp
    if flag==0:
        if RDW[p][1]>=RDW[q][1]:
            RDW[p][1]=max(RDW[p][1],RDW[q][1]+1)
            RDW[q][0]=p
            RDW[q][2]=l_temp2+l-l_temp1
        else:
            RDW[p][0]=q
            RDW[p][2]=-l_temp2-l+l_temp1
            l_temp2+l-l_temp1
    else:
        ll=l_temp1-l_temp2
        if l!=ll:
            ff[0]=1
for i in range(M):
    a,b,c=LRD[i]
    compare(a-1,b-1,c)
    if ff[0]==1:
        print("No")
        break
else:
    print("Yes")

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)

def main():
    N,M = map(int,input().split())
    e = [[] for i in range(N)]
    t = 0
    a = []
    for i in range(M):
        r,l,d = map(int,input().split())
        e[r-1].append((l-1,d))
        e[l-1].append((r-1,-d))
        a.append(r-1)
        a.append(l-1)
        t = r-1

    col = [ float("inf") for i in range(N) ]
    def dfs(i,c):
        for node in e[i]:
            if col[node[0]] == float("inf"):
                col[node[0]] = c + node[1]
                dfs(node[0],c+node[1])
            elif col[node[0]] != c + node[1]:
                print("No")
                exit()
    dfs(t,0)
    a = list(set(a))
    for i in a:
        if col[i] == float("inf"):
            dfs(i,0)
    print("Yes")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)

def main():
    N,M = map(int,input().split())
    e = [[] for i in range(N)]
    t = 0
    a = []
    for i in range(M):
        r,l,d = map(int,input().split())
        e[r-1].append((l-1,d))
        e[l-1].append((r-1,-d))
        a.append(r-1)
        a.append(l-1)
        t = r-1

    col = [ float("inf") for i in range(N) ]
    def dfs(i,c):
        for node in e[i]:
            if col[node[0]] == float("inf"):
                col[node[0]] = c + node[1]
                dfs(node[0],c+node[1])
            elif col[node[0]] != c + node[1]:
                print("No")
                exit()


    dfs(t,0)
    a = list(set(a))
    for i in a:
        if col[i] == float("inf"):
            dfs(i,0)


    print("Yes")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)

def main():
    N,M = map(int,input().split())
    e = [[] for i in range(N)]
    t = 0
    a = []
    for i in range(M):
        r,l,d = map(int,input().split())
        e[r-1].append((l-1,d))
        e[l-1].append((r-1,-d))
        a.append(r-1)
        a.append(l-1)
        t = r-1

    col = [ float("inf") for i in range(N) ]
    def dfs(i,c):
        for node in e[i]:
            if col[node[0]] == float("inf"):
                col[node[0]] = c + node[1]
                dfs(node[0],c+node[1])
            elif col[node[0]] != c + node[1]:
                print("No")
                exit()


    dfs(t,0)
    a = list(set(a))
    for i in a:
        if col[i] == float("inf"):
            dfs(i,0)


    print("Yes")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(100000000)

def main():
    N,M = map(int,input().split())
    e = [[] for i in range(N)]
    t = 0
    a = []
    for i in range(M):
        r,l,d = map(int,input().split())
        e[r-1].append((l-1,d))
        e[l-1].append((r-1,-d))
        a.append(r-1)
        a.append(l-1)
        t = r-1

    col = [ float("inf") for i in range(N) ]
    def dfs(i,c):
        for node in e[i]:
            if col[node[0]] == float("inf"):
                col[node[0]] = c + node[1]
                dfs(node[0],c+node[1])
            elif col[node[0]] != c + node[1]:
                print("No")
                exit()


    dfs(t,0)
    a = list(set(a))
    for i in a:
        if col[i] == float("inf"):
            dfs(i,0)


    print("Yes")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(100000000)

def main():
    N,M = map(int,input().split())
    e = [[] for i in range(N)]
    t = 0
    a = []
    for i in range(M):
        r,l,d = map(int,input().split())
        e[r-1].append([l-1,d])
        e[l-1].append([r-1,-d])
        a.append(r-1)
        a.append(l-1)
        t = r-1

    col = [ float("inf") for i in range(N) ]
    def dfs(i,c):

        for node in e[i]:
            if col[node[0]] == float("inf"):
                col[node[0]] = c + node[1]
                dfs(node[0],c+node[1])
            elif col[node[0]] != c + node[1]:
                print("No")
                exit()


    dfs(t,0)
    a = list(set(a))
    for i in a:
        if col[i] == float("inf"):
            dfs(i,0)


    print("Yes")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(100000)

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

graph = [{} for _ in range(N)]
for l, r, d in LRD:
	l, r = l - 1, r - 1
	graph[l][r] = d
	graph[r][l] = -d

visited = [False] * N
depths = [0] * N
def dfs(v):
	q = [v]
	while q:
		cu = q.pop()
		for nx, d in graph[cu].items():
			if visited[nx]:
				if depths[nx] != depths[cu] + d:
					return False
			else:
				visited[nx] = True
				depths[nx] = depths[cu] + d
				q += [nx]
	return True

for n in range(N):
	if not visited[n]:
		if not dfs(n):
			print("No")
			break
else:
	print("Yes")

Solution (Python):
import sys
sys.setrecursionlimit(100000)

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

graph = [{} for _ in range(N)]
for l, r, d in LRD:
	l, r = l - 1, r - 1
	graph[l][r] = d
	graph[r][l] = -d

visited = [False] * N
depths = [-1] * N
def dfs(v, p, d):
	if visited[v]:
		return True
	visited[v] = True
	depths[v] = d

	for n in graph[v]:
		if n == p:
			continue
		n_depth = d + graph[v][n]
		if depths[n] != -1 and depths[n] != n_depth:
			return False
		result = dfs(n, v, n_depth)
		if not result:
			return False
	return True

for n in range(N):
	if not dfs(n, -1, 0):
		print("No")
		break
else:
	print("Yes")

Solution (Python):
N, M = map(int,input().split())

connect = [[] for _ in range(N)]
visited = [False for _ in range(N)]
cord = [0 for _ in range(N)]

for _ in range(M):
    l,r,d = map(int,input().split())
    l -= 1
    r -= 1
    connect[l].append((r,d))
    connect[r].append((l,-d))

def dfs(i):
    q = [i]
    
    while q:
        x = q.pop()
        for nx, d in connect[x]:
            if not visited[nx]:
                visited[nx] = True
                cord[nx] = cord[x] + d
                q.append(nx)
            else:
                if cord[nx] != cord[x] + d:
                    return False
            
    return True

for i in range(N):
    if not visited[i]:
        visited[i] = True
        flag = dfs(i)
    if not flag:
        break

if flag:
    print('Yes')
else:
    print('No')

Solution (Python):
class WeightedUnionFind:

    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.level = [0] * n
        self.weight = [0] * n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            px = self.find(self.par[x])
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = px
            return px

    def union(self, x, y, w):
        px = self.find(x)
        py = self.find(y)
        if self.level[px] < self.level[py]:
            self.par[px] = py
            self.weight[px] = -self.weight[x] + self.weight[y] + w
        else:
            self.par[py] = px
            self.weight[py] = + self.weight[x] - self.weight[y] - w
            if self.level[px] == self.level[py]:
                self.level[px] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

def solve(N, M):
    wuf = WeightedUnionFind(N+1)
    for i in range(M):
        L, R, D = map(int, input().split())
        if wuf.same(L, R):
            if wuf.diff(L, R) != D:
                return False
        else:
            wuf.union(L, R, D)
    return True

N, M = map(int, input().split())
if solve(N, M):
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
sys.setrecursionlimit(100001)
def input():
    return sys.stdin.readline()[:-1]

def dfs(v):
    ret = True
    for u, d in es[v]:
        if x[u] == None:
            x[u] = x[v] + d
            ret = ret and dfs(u)
        elif x[u] != x[v] + d:
            return False
    return ret

N, M = map(int, input().split())
es = [[] for i in range(N+1)]
for i in range(M):
    L, R, D = map(int, input().split())
    es[L].append((R, D))
    es[R].append((L, -D))
x = [None for i in range(N+1)]
flag = True
for v in range(1, N+1):
    if x[v] == None:
        x[v] = 0
        if dfs(v) == False:
            flag = False
            break
if flag:
    print("Yes")
else:
    print("No")

Solution (Python):
class WeightedUnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0 for _ in range(n + 1)]
        self.weight = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            y = self.find(self.parent[x])
            self.weight[x] += self.weight[self.parent[x]]
            self.parent[x] = y
            return y

    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)

        if self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        else:
            self.parent[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)

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]


N, M = map(int, input().split())
items = []
for i in range(M):
    items.append(tuple(map(int, input().split())))

wu = WeightedUnionFind(N)

for i in range(M):
    l, r, d = items[i]
    if wu.same(l, r):
        if d == wu.diff(l, r):
            continue
        else:
            print("No")
            exit()
    else:
        wu.union(l, r, d)

print("Yes")

Solution (Python):
class UnionFindTree:
    def __init__(self, size):
        self.parent = list(range(size))
        self.dist = [0] * size

    def find_root(self, x):
        if self.parent[x] == x:
            return x, 0
        else:
            self.parent[x], d = self.find_root(self.parent[x])
            self.dist[x] += d
            return self.parent[x], self.dist[x]

    def union(self, x, y, d):
        r1, d1 = self.find_root(x)
        r2, d2 = self.find_root(y)
        if r1 != r2:
            self.parent[r2] = r1
            self.dist[r2] = (d1 - d2) + d
        else:
            if (d2 - d1) != d:
                return False
        return True

def main():
    n, m = map(int, input().split())
    tree = UnionFindTree(n+1)
    for i in range(m):
        l, r, d = map(int, input().split())
        if not tree.union(l, r, d):
            print('No')
            return
    print('Yes')

main()

Solution (Python):
import sys
n,m=map(int,input().split())
graph=[[] for i in range(n)]
for i in range(m):
    l,r,d=map(int,input().split())
    graph[r-1].append((l-1,-d))
    graph[l-1].append((r-1,d))
INF=10**9
x_list=[INF]*n
stack=[]
for i in range(n):
    if x_list[i]==INF:
        x_list[i]=0
        stack.append(i)
        while len(stack)>0:
            now=stack.pop()
            for j in graph[now]:
                if x_list[j[0]]==INF:
                    x_list[j[0]]=x_list[now]+j[1]
                    stack.append(j[0])
                else:
                    if x_list[j[0]]==x_list[now]+j[1]:
                        pass
                    else:
                        print('No')
                        sys.exit()
print('Yes')

Solution (Python):
n, m = [int(_) for _ in input().split()]
c = []
flag = True
g = [[-1, -1] for _ in range(n)]
g_num = 0
for i in range(m):
    l, r, d = [int(_) for _ in input().split()]
    l -= 1
    r -= 1
    if g[l][0] != -1 and g[r][0] != -1 and g[l][0] == g[r][0]:
        if c[g[l][0]][g[r][1]][1] - c[g[l][0]][g[l][1]][1] != d:
            flag = False
            break
    elif g[l][0] != -1 and g[r][0] != -1:
        l_pos = c[g[l][0]][g[l][1]][1]
        r_pos = c[g[r][0]][g[r][1]][1]
        if r_pos > l_pos+d:
            for j in c[g[l][0]]:
                j[1] += r_pos - d - l_pos
                if j[1] > 10**9:
                    flag = False
                    break
                g[j[0]][1] += g[c[g[r][0]][-1][0]][1] + 1
            c[g[r][0]].extend(c[g[l][0]])
            for j in c[g[l][0]]:
                g[j[0]][0] = g[r][0]

        else:
            for j in c[g[r][0]]:
                j[1] += l_pos + d - r_pos
                if j[1] > 10**9:
                    flag = False
                    break
                g[j[0]][1] += g[c[g[l][0]][-1][0]][1] + 1
            c[g[l][0]].extend(c[g[r][0]])
            for j in c[g[r][0]]:
                g[j[0]][0] = g[l][0]

    elif g[l][0] != -1:
        l_pos = c[g[l][0]][g[l][1]][1]
        c[g[l][0]].append([r, l_pos+d])
        g[r][0] = g[l][0]
        g[r][1] = g[c[g[l][0]][-2][0]][1]+1
    elif g[r][0] != -1:
        r_pos = c[g[r][0]][g[r][1]][1]
        c[g[r][0]].append([l, r_pos-d])
        g[l][0] = g[r][0]
        g[l][1] = g[c[g[r][0]][-2][0]][1]+1
    else:
        c.append([[l, 0], [r, d]])
        g[l][0] = g_num
        g[r][0] = g[l][0]
        g[l][1] = 0
        g[r][1] = 1
        g_num += 1
    if not flag:
        break
if flag:
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())


class PotentialUnionFind:
    def __init__(self,N): 
        self.table = [i for i in range(N)]    
        self.rank  = [1 for i in range(N)]    
        self.size  = [1 for i in range(N)]    
        self.diffweight = [0 for i in range(N)]

    def Find(self,x):    
        if self.table[x] == x:
            return x
        else:
            root = self.Find(self.table[x]) 
            self.size[x] = self.size[self.table[x]]
            self.diffweight[x] += self.diffweight[self.table[x]]
            self.table[x] = root
            return self.table[x]

    def Unite(self,x,y,w): 
        w = w - self.Weight(y) + self.Weight(x)
        x,y = self.Find(x), self.Find(y)
        sx,sy = self.Size(x), self.Size(y)
        if x == y: return
        if self.rank[x] > self.rank[y]:
            self.table[y] = x
            self.size[x] = sx + sy
            self.diffweight[y] = w
        else:
            self.table[x] = y
            self.size[y] = sx + sy
            self.diffweight[x] = -w
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def Check(self,x,y):
        return self.Find(x) == self.Find(y)

    def Size(self,x):
        return self.size[self.Find(x)]

    def Weight(self,x): 
        self.Find(x)
        return self.diffweight[x]

    def Diff(self,x,y): 
        return self.Weight(y) - self.Weight(x)


N,M = inpl()
PUF = PotentialUnionFind(N)

for _ in range(M):
    l,r,d = inpl()
    l,r = l-1,r-1
    if PUF.Check(l,r):
        if PUF.Diff(l,r) == d:
            continue
        else:
            print('No')
            exit()
    else:
        PUF.Unite(l,r,d)

print('Yes')

Solution (Python):
class UnionFind():
    
    
    def __init__(self, n):
        self.n = n
        
        
        self.root = [-1]*(n+1)
        
        self.rnk = [0]*(n+1)
        self.potential=[0]*(n+1)
 
    
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            
            r = self.Find_Root(self.root[x])
            self.potential[x] += self.potential[self.root[x]]
            self.root[x]=r
            return self.root[x]
 
    
    def Unite(self, x, y, d):
        d+=self.potential[x]-self.potential[y]
        
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        
        if(x == y):
            return 
        
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
            self.potential[y]=d
 
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            self.potential[x]=-d
            
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
 
    
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
 
    
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
    
    def dist(self,x,y):
        if self.isSameGroup(x,y):
            return self.potential[y]-self.potential[x]
        else: return -10**10

n,m=map(int,input().split())
if m==0:
  print('Yes')
else:
  t=UnionFind(n)
  arr=[list(map(int,input().split())) for _ in range(m)]
  for l,r,d in arr:
    if t.isSameGroup(l,r) and t.dist(l,r)!=d:
      print('No')
      break
    t.Unite(l,r,d)
  else:
    print('Yes')

Solution (Python):
from sys import exit, stdin

class WeightedUnionFind:

    def __init__(self, n=1, sum_unity=0):

        self.par = [i for i in range(n)]

        self.rank = [0]*n

        self.diff_weight = [sum_unity]*n

    

    def find_root(self, x):

        if self.par[x] == x:

            return x

        else:

            y = self.find_root(self.par[x])

            self.diff_weight[x] += self.diff_weight[self.par[x]]

            self.par[x] = y

            return y

    def merge(self, x, y, w):

        rx, ry = self.find_root(x), self.find_root(y)

        if self.rank[rx] < self.rank[ry]:

            self.par[rx] = ry

            self.diff_weight[rx] = w - self.diff_weight[x] + self.diff_weight[y]

        else:

            self.par[ry] = rx

            self.diff_weight[ry] = -w - self.diff_weight[y] + self.diff_weight[x]

            if self.rank[rx] == self.rank[ry]:

                self.rank[rx] += 1

    def issame(self, x, y):

        return self.find_root(x) == self.find_root(y)

    def diff(self, x, y):

        return self.diff_weight[x] -  self.diff_weight[y]

N, M = [int(_) for _ in stdin.readline().rstrip().split()]

wuf = WeightedUnionFind(N)

for i in range(M):

    l, r, d = [int(_) for _ in stdin.readline().rstrip().split()]

    l -= 1

    r -= 1

    if wuf.issame(l, r):

        diff = wuf.diff(l, r)

        if diff != d:

            print("No")

            exit()

    else:

        wuf.merge(l, r, d)

print("Yes")

Solution (Python):
from sys import exit, stdin

class WeightedUnionFind:
    def __init__(self, n=1, sum_unity=0):
        self.par = [i for i in range(n)]
        self.rank = [0]*n
        self.diff_weight = [sum_unity]*n

    
    def find_root(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find_root(self.par[x])
            self.diff_weight[x] += self.diff_weight[self.par[x]]
            self.par[x] = y
            return y


    def merge(self, x, y, w):
        rx, ry = self.find_root(x), self.find_root(y)
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.diff_weight[rx] = w - self.diff_weight[x] + self.diff_weight[y]
        else:
            self.par[ry] = rx
            self.diff_weight[ry] = -w - self.diff_weight[y] + self.diff_weight[x]
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1


    def issame(self, x, y):
        return self.find_root(x) == self.find_root(y)


    def diff(self, x, y):
        return self.diff_weight[x] -  self.diff_weight[y]

N, M = [int(_) for _ in stdin.readline().rstrip().split()]

wuf = WeightedUnionFind(N)
for i in range(M):
    l, r, d = [int(_) for _ in stdin.readline().rstrip().split()]
    l -= 1
    r -= 1
    if wuf.issame(l, r):
        diff = wuf.diff(l, r)
        if diff != d:
            print("No")
            exit()
    else:
        wuf.merge(l, r, d)
print("Yes")

Solution (Python):
n,m = map(int,input().split())
l = dict()
tl = []
for i in range(n+1):
  l[i] = []
for _ in range(m):
  a,b,c = map(int,input().split())
  l[a].append((b,c))
  l[b].append((a,-c))
  tl.append((a,b,c))
pos = [None]*(n+1)

def dfs(v,p,pos):
  q = [(v,p)]
  while q:
    cv,cp = q.pop()
    if pos[cv] != None:
      continue
    pos[cv] = cp
    for i,d in l[cv]:
      if pos[i] == None:
        q.append((i,cp+d))
  return

for i in range(1,n+1):
  if pos[i] == None:
    dfs(i,0,pos)
for a,b,c in tl:
  if pos[b] - pos[a] != c:
    print("No")
    break
else:
  print("Yes")

Solution (Python):



def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px


def weight(x):
    find(x)
    return wei[x]



def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True


def same(x,y):
    return find(x) == find(y)


def size(x):
    return -par[find(x)]


def diff(x,y):
    return weight(y)-weight(x)

n,m = map(int,input().split())




par = [-1]*n
wei = [0]*n

for i in range(m):
    l,r,d = map(int,input().split())
    l,r = l-1,r-1
    if same(l,r):
        if d != diff(l,r):
            print('No')
            exit()
    else:
        unite(l,r,d)
print('Yes')

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N, M = (int(_) for _ in input().split())
    L = []
    R = []
    D = []
    for i in range(M):
        s1,s2,s3 = (int(_) for _ in input().split())
        L.append(s1)
        R.append(s2)
        D.append(s3)
    if M ==0:
        print('Yes')
        return
    adj = [[] for _ in range(N)]
    for i in range(M):
        adj[L[i]-1].append([R[i]-1,D[i]])
        adj[R[i]-1].append([L[i]-1,-1*D[i]])

    dist = [None for _ in range(N)]
    for i in range(N):
        if dist[i] is not None:
            continue
        stack = [i]
        dist[i] = 0
        while stack:
            v=stack.pop()
            for to,cost in adj[v]:
                if dist[to] is None:
                    stack.append(to)
                    dist[to] = dist[v]+cost
                elif dist[to] != dist[v] +cost:
                    print('No')
                    return
        
    print('Yes')
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(100001)
def input():
    return sys.stdin.readline()[:-1]
N, M = map(int,input().split())
if M == 0:
    print("Yes")
    exit(0)

INF = float("inf")
G = [[] for k in range(N)]
D = [INF for k in range(N)]
def dfs(x):
    if len(G[x]) == 0:
        D[x] = x
    else:
        for e in G[x]:
            if D[e[0]] == INF:
                D[e[0]] = D[x] + e[1]
                dfs(e[0])
            else:
                if D[e[0]] != D[x] + e[1]:
                    print("No")
                    exit(0)

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

for k in range(N):
    if D[k] == INF:
        D[k] = k
        dfs(k)
print("Yes")