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



Sample input:
3 3
1 2 1
2 3 1
1 3 2


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
G=[[] for i in range(n)]
dist=[None]*n
for i in range(m):
    l,r,d=map(int,input().split())
    G[l-1].append([r-1,d])
    G[r-1].append([l-1,-d])

for i in range(n):

    q=deque()
    if dist[i]==None:
        dist[i]=0
    q.append(i)
    while q:
        cur=q.popleft()
        for nx,d in G[cur]:
            if dist[nx]==None:
                dist[nx]=dist[cur]+d
                q.append(nx)
            elif dist[nx]!=dist[cur]+d:
                print('No')
print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
G=[[] for i in range(n)]

for i in range(m):
    l,r,d=map(int,input().split())
    G[l-1].append([r-1,d])
    G[r-1].append([l-1,-d])

for i in range(n):
    dist=[None]*n
    q=deque()
    if dist[i]==None:
        dist[i]=0
    q.append(i)
    while q:
        cur=q.popleft()
        for nx,d in G[cur]:
            if dist[nx]==None:
                dist[nx]=dist[cur]+d
                q.append(nx)
            elif dist[nx]!=dist[cur]+d:
                print('No')
print('Yes')

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
LRD = [list(map(int,input().split())) for i in range(M)]
p = [[] for i in range(N)]
for l,r,d in LRD:
    l -= 1
    r -= 1
    p[l].append((r,d))
    p[r].append((l,d))
stack = [0]
alr = [[-1 for i in range(N)] for j in range(N)]
alr[0][0] = 0
ans = 'Yes'
while stack:
    now = stack.pop()
    for i,d in p[now]:
        if alr[now][i] == -1:
            alr[now][i] = d
            stack.append(i)
        else:
            if alr[now][i] != d:
                ans = 'No'
                break
    if ans == 'No':
        break
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
connection=[[] for i in range(n)]
for i in range(m):
  connection[l[i][0]-1].append((l[i][1]-1,l[i][2]))
  connection[l[i][1]-1].append((l[i][0]-1,-l[i][2]))

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=[v]
  next2=set()
  visited=[-1]*n
  visitct=0
  while len(next)!=0 and visitct!=n:
    for i in range(len(next)):
      if visited[next[i]]==-1:
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j][0]]==-1:
            distance[connection[next[i]][j][0]]=distance[next[i]]+connection[next[i]][j][1]
            next2.add(connection[next[i]][j][0])
    next=list(next2)
    next2=set()
  return distance

print(bfs(0))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l=[]
r=[]
d=[]
xp = [0]*(n)
xp[0] = 0
for i in range(m):
  x,y,z = map(int,input().split())
  l.append(x-1)
  r.append(y-1)
  d.append(z)
  if xp[r[i]]==0:
    xp[r[i]]=xp[l[i]]+d[i]
  
for i in range(m):
  if xp[r[i]]-xp[l[i]]!=d[i]:
    print("No")
    exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l=[]
r=[]
d=[]
xp = [0]*(n)
xp[0] = 0
for i in range(m):
  x,y,z = map(int,input().split())
  l.append(x-1)
  r.append(y-1)
  d.append(z)
  xp[r[i]]=xp[l[i]]+d[i]
  
for i in range(m):
  if xp[r[i]]-xp[l[i]]!=d[i]:
    print("No")
    exit()
print("Yes")

Submission status: Wrong Answer

Solution code:

import queue

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

dis = [[] for i in range(n)]
for i in range(m):
    g1,g2,L =  map(int,input().split())
    dis[g1-1].append((g2-1,L))
    dis[g2-1].append((g1-1,-L))

q = queue.LifoQueue()
geo = [0.5] * n

for i in range(n):
    if not dis[i] == []:
        q.put(i)
        geo[i] = 0 
        now,L = dis[i].pop()
        here = 0

        while not q.empty():

            if not dis[now] == []:
                if geo[now] != 0.5:
                    if here + L != geo[now]:
                        print("No")
                        exit()
                
                q.put(now)
                geo[now] = here + L
                here = geo[now]
                now,L = dis[now].pop()
                
            else:
                now = q.get()
                L = 0
print("Yes")

Submission status: Wrong Answer

Solution code:
import queue

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

dis = [[] for i in range(n)]

for i in range(m):
    g1,g2,L =  map(int,input().split())
    dis[g1-1].append((g2-1,L))
    dis[g2-1].append((g1-1,-L))
print(dis)



geo = dict()
geo[0] = 0

q = queue.Queue()
q.put(0)

#fig = [0]*n
#fig[0] = 1
while not q.empty():
    g1 = q.get()
    #if fig[g1] == 1:
    for g2,L in dis[g1]:
        print(geo)
        if g2 not in geo:
            geo[g2] = geo[g1] + L
            #fig[g2] = 1
            q.put(g2)
        else:
            if geo[g2] != geo[g1] + L:
                print("no")
                exit()
    #fig[g1] = 2
print("yes")



"""
q = queue.Queue(0)
for i in range(1,5):
    q.put(i)
q.put(4)
while not q.empty():
    print(q.get())
"""

Submission status: Wrong Answer

Solution code:
import collections
from collections import deque
import sys
input = sys.stdin.readline

def tree(s):
    flag=True
    INF=-10**9
    dis[s]=0
    def bfs():
        d = deque()
        d.append(s)
        while len(d):
            x = d.popleft()
            for i,j in vec[x]:
                if dis[i] == INF:
                    dis[i]=dis[x]+j
                    d.append(i)
                else:
                    if dis[i]==dis[x]+j:
                        continue
                    else:
                        print('No')
                        exit()
        return flag
    return bfs()


n,m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]
vec= [[] for i in range(n)]

for i in range(m):
    vec[a[i][0]-1].append((a[i][1]-1,a[i][2]))
    vec[a[i][1]-1].append((a[i][0]-1,-a[i][2]))

INF=float('inf')
dis=[INF]*n

for i in range(n):
    if dis[i]==INF:
        tree(i)
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(m):
  l, r, d = map(int, input().split())
  l -= 1
  r -= 1
  G[l].append((r, d))
  G[r].append((l, -d))
dist = [float("inf")]*n
def dfs(v):
  for nv, d in G[v]:
    if dist[nv] == float("inf"):
      dist[nv] = dist[v]+d
      dfs(nv)
    else:
      if dist[nv] != dist[v]+d:
        return False
  return True
for i in range(n):
  if dist[i] == float("inf"):
    dist[i] = 0
    if not dfs(i):
      print("No")
      break
else:
  print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


N,M=map(int,input().split())
adj=[[] for _ in range(N)]
inf = 10**9+1
x=[inf]*N

L=[0]*M
R=[0]*M
D=[0]*M

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

queue=[]
#距離の設定
for root in range(N):
    if x[root]==inf:
        queue.append([root,-1])#node,pare
        x[root]=0
        while queue:
            node,par=queue.pop()
            num=len(adj[node])
            for i in range(num):
                to,cost=adj[node][i]
                if x[to]==inf:#見たことがなければ
                    x[to]=x[node]+cost
                    queue.append([to,node])
            
    
f=1
#判定
for i in range(M):
    d=x[R[i]]-x[L[i]]
    if d!=D[i]:
        f=0
            
    
    
            
            
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


N,M=map(int,input().split())
adj=[[] for _ in range(N)]
inf = 10*10
x=[inf]*N

L=[0]*M
R=[0]*M
D=[0]*M

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

queue=[]
#距離の設定
for root in range(N):
    if x[root]==inf:
        queue.append([root,-1])#node,pare
        x[root]=0
        while queue:
            node,par=queue.pop()
            num=len(adj[node])
            for i in range(num):
                to,cost=adj[node][i]
                if x[to]==inf:#見たことがなければ
                    x[to]=x[node]+cost
                    queue.append([to,node])
            
    
f=1
#判定
for i in range(M):
    d=x[R[i]]-x[L[i]]
    if d!=D[i]:
        f=0
                    
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


N,M=map(int,input().split())
adj=[[] for _ in range(N)]
inf = 10*10
x=[inf]*N

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

L=[]
#距離の設定
for root in range(N):
    if x[root]==inf:
        L.append([root,-1])#node,pare
        x[root]=0
        while L:
            node,par=L.pop()
            num=len(adj[node])
            for i in range(num):
                to,cost=adj[node][i]
                if x[to]==inf:#見たことがなければ
                    x[to]=x[node]+cost
                    L.append([to,node])
            
    
f=1
#判定
for i in range(N):
    num=len(adj[i])
    fro=x[i]
    for j in range(num):
        to=x[adj[i][j][0]]
        if to-fro == adj[i][j][1]:
            continue
        else:
            f=0
            
    
    
            
            
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
################
class UnionFind():
    """
    parents : 親要素(findしない場合は根ではないことの注意)，根の場合は"-(要素数）"
    find(x):要素xの属するグループの根を返す
    size(x):要素xの属するグループの要素数を返す
    same(x,y):x,yが同じグループに属しているか返す
    members(x):要素xが属するグループに属する要素をリストで返す
    roots:全ての根の要素を返す
    group_count():グループの数を返す
    all_group_members():{根要素：[そのグループに含まれる要素のリスト]}の辞書を返す
    """
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
    
################

from collections import defaultdict
import sys
input = sys.stdin.readline

inf=10**10

N,M=map(int,input().split())
uf = UnionFind(N)
L=[0]*M
R=[0]*M
D=[0]*M
x=[inf]*N

f=1

for i in range(M):
    L[i],R[i],D[i]=map(int,input().split())
    L[i]-=1
    R[i]-=1
    uf.union(L[i],R[i])
    if x[L[i]]==inf and x[R[i]]==inf:
        x[L[i]]=0
        x[R[i]]=D[i]
    elif x[L[i]]==inf:
        x[L[i]]=x[R[i]]-D[i]
    elif x[R[i]]==inf:
        x[R[i]]=x[L[i]]+D[i]
    else:
        if x[R[i]]-x[L[i]]!=D[i]:
            f=0
    
#範囲の判定

Max = defaultdict(int)
Min  = defaultdict(int)


for i in range(N):
    gi=uf.find(i)
    if x[i]==inf:
        x[i]=0
    Max[gi]=max(Max[gi],x[i])
    Min[gi]=min(Min[gi],x[i])



roots=uf.roots()
lim=10**9

for i in roots:
    l=Max[i]-Min[i]
    if l>lim:
        f=0
    
    
            
            
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
################
class UnionFind():
    """
    parents : 親要素(findしない場合は根ではないことの注意)，根の場合は"-(要素数）"
    find(x):要素xの属するグループの根を返す
    size(x):要素xの属するグループの要素数を返す
    same(x,y):x,yが同じグループに属しているか返す
    members(x):要素xが属するグループに属する要素をリストで返す
    roots:全ての根の要素を返す
    group_count():グループの数を返す
    all_group_members():{根要素：[そのグループに含まれる要素のリスト]}の辞書を返す
    """
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
    
################

from collections import defaultdict

N,M=map(int,input().split())
uf = UnionFind(N)
L=[0]*M
R=[0]*M
D=[0]*M
x=[-1]*N

f=1

for i in range(M):
    L[i],R[i],D[i]=map(int,input().split())
    L[i]-=1
    R[i]-=1
    uf.union(L[i],R[i])
    if x[L[i]]==-1 and x[R[i]]==-1:
        x[L[i]]=0
        x[R[i]]=D[i]
    elif x[L[i]]==-1:
        x[L[i]]=x[R[i]]-D[i]
    elif x[R[i]]==-1:
        x[R[i]]=x[L[i]]+D[i]
    else:
        if x[R[i]]-x[L[i]]!=D[i]:
            f=0
    
#範囲の判定

Max = defaultdict(int)
Min  = defaultdict(int)


for i in range(N):
    gi=uf.find(i)
    xx=x[i]
    if xx==-1:
        xx=0
    Max[gi]=max(Max[gi],xx)
    Min[gi]=min(Min[gi],xx)
    
inf=10**9    


roots=uf.roots()

for i in roots:
    l=Max[i]-Min[i]
    if l>inf:
        f=0
    
    
            
            
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
################
class UnionFind():
    """
    parents : 親要素(findしない場合は根ではないことの注意)，根の場合は"-(要素数）"
    find(x):要素xの属するグループの根を返す
    size(x):要素xの属するグループの要素数を返す
    same(x,y):x,yが同じグループに属しているか返す
    members(x):要素xが属するグループに属する要素をリストで返す
    roots:全ての根の要素を返す
    group_count():グループの数を返す
    all_group_members():{根要素：[そのグループに含まれる要素のリスト]}の辞書を返す
    """
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
    
################

from collections import defaultdict

N,M=map(int,input().split())
uf = UnionFind(N)
L=[0]*M
R=[0]*M
D=[0]*M
x=[-1]*N

f=1

for i in range(M):
    L[i],R[i],D[i]=map(int,input().split())
    L[i]-=1
    R[i]-=1
    uf.union(L[i],R[i])
    if x[L[i]]==-1 and x[R[i]]==-1:
        x[L[i]]=0
        x[R[i]]=D[i]
    elif x[L[i]]==-1:
        x[L[i]]=x[R[i]]-D[i]
    elif x[R[i]]==-1:
        x[R[i]]=x[L[i]]+D[i]
    else:
        if x[R[i]]-x[L[i]]!=D[i]:
            f=0
    
#範囲の判定

Max = defaultdict(int)
Min  = defaultdict(int)


for i in range(N):
    gi=uf.find(i)
    xx=x[i]
    Max[gi]=max(Max[gi],xx)
    Min[gi]=min(Min[gi],xx)
    
inf=10**9    


roots=uf.roots()

for i in roots:
    l=Max[i]-Min[i]
    if l>inf:
        f=0
    
    
            
            
            
if f==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


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

edges = defaultdict(list)

for i in range(m):
    l, r, d = map(int, input().split())
    edges[l-1].append([r-1, d])
    edges[r-1].append([l-1, -d])

seen = [float('INF')] * n

for start in range(n):
    if seen[start] != float('INF'):
        continue

    seen[start] = 0
    todo = []
    while edges[start]:
        to, dist = edges[start].pop()
        seen[to] = dist
        todo.append([to, dist])

    while todo:
        node, dist = todo.pop()

        while edges[node]:
            to, add_dis = edges[node].pop()

            if seen[to] == float('INF') or seen[to] == dist:
                seen[to] = dist + add_dis
                todo.append([to, dist+add_dis])

            else:
                print('No')
                exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


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

edges = defaultdict(list)
nodes = set()

for i in range(m):
    l, r, d = map(int, input().split())
    edges[l-1].append([r-1, d])
    nodes.add(l-1)

seen = [-1] * n
nodes = list(nodes)

for start in nodes:
    if seen[start] != -1:
        continue

    seen[start] = 0
    todo = edges[start]

    while todo:
        node, dist = todo.pop()

        if seen[node] == -1 or seen[node] == dist:
            seen[node] = dist
            for to, add_d in edges[node]:
                todo.append([to, dist+add_d])

        else:
            print('No')
            exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000000)
N, M = [int(_) for _ in input().split()]

LRD = []
path = [[] for _ in range(N + 1)]
for _ in range(M):
    l, r, d = [int(_) for _ in input().split()]
    path[l].append((r, d))
    path[r].append((l, -d))
    LRD.append((l, r, d))

INF = -1e12
memo = [INF] * (N + 1)


def dfs(n):
    for ni, d in path[n]:
        if memo[ni] != INF: continue
        memo[ni] = memo[n] + d
        dfs(ni)


for i in range(1, N + 1):
    if memo[i] != INF: continue
    memo[i] = 0
    dfs(i)
for l, r, d in LRD:
    if not memo[l] + d == r:
        print('No')
        exit()
print('Yes')

Submission status: Wrong Answer

Solution code:
class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.weight = [0]*N

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

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y, w):
        w-=self.getWeight(x)
        w+=self.getWeight(y)
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.weight[x] = self.weight[y]+w
            return
    def getWeight(self,x):
      x-=1
      self.root(x)
      return(self.weight[x])
N,M=map(int,input().split())
G=unionFind(N)
LRD=[tuple(map(int,input().split())) for i in range(M)]
for l,r,d in LRD:
  #print(G.parent)
  #print(G.weight)
  if G.same(l,r):
    if G.getWeight(r)-G.getWeight(l) == d:
      continue
    else:
      #print(G.parent)
      #print(G.weight)
      print("No")
      exit(0)
  else:
    G.unite(r,l,d)

print("Yes")

Submission status: Wrong Answer

Solution code:
class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.weight = [0]*N

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

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y, w):
        w-=self.getWeight(x)
        w+=self.getWeight(y)
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.weight[x] = self.weight[y]+w
            return
    def getWeight(self,x):
      x-=1
      self.root(x)
      return(self.weight[x])
N,M=map(int,input().split())
G=unionFind(N)
LRD=[tuple(map(int,input().split())) for i in range(M)]
for l,r,d in LRD:
  #print(G.parent)
  #print(G.weight)
  if G.same(l,r):
    if G.getWeight(r)-G.getWeight(l) == d:
      continue
    else:
      print(G.parent)
      print(G.weight)
      print("No")
      exit(0)
  else:
    G.unite(r,l,d)

print("Yes")

Submission status: Wrong Answer

Solution code:
class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.weight = [0]*N

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

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y, w):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.weight[x] = self.weight[y]+w
            return
    def getWeight(self,x):
      x-=1
      self.root(x)
      return(self.weight[x])
N,M=map(int,input().split())
G=unionFind(N)
LRD=[tuple(map(int,input().split())) for i in range(M)]
for l,r,d in LRD:
  if G.same(l,r):
    if G.getWeight(r)-G.getWeight(l) == d:
      continue
    else:
      print("No")
      exit(0)
  else:
    G.unite(r,l,d)

print("Yes")

Submission status: Wrong Answer

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)
def fix(x):
    q = []
    while root[x]!=x:
        q.append(x)
        x = root[x]
    rt = x
    while  len(q)>0:
        x = q.pop()
        dist[x] += dist[root[x]]
        root[x] = rt
    return rt

n,m = inm()
root = list(range(n))
dist = [0]*n
for i in range(m):
    l,r,d = inm()
    l -= 1
    r -= 1
    rtl = fix(l)
    rtr = fix(r)
    if rtl != rtr:
        if dist[l]+d > dist[r]:
            dist[rtr] = dist[l]+d-dist[r]
            dist[r] = dist[l]+d
            root[r] = root[rtr] = rtl
        else:
            dist[rtl] = dist[r]-dist[l]-d
            dist[l] += dist[rtl]
            root[l] = root[rtl] = rtr
    else:
        if dist[l]+d != dist[r]:
            print('No')
            exit()
print('Yes')

Submission status: Wrong Answer

Solution code:
from collections import deque
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
appear = set()
x = [inf for i in range(n+1)]
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in appear and r not in appear:
        x[l] = 0
    elif r in appear:
        l,r = r,l
        d *= -1
    appear.add(l)
    appear.add(r)
    data.append([l,r,d])
count = 0
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import deque
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
appear = set()
x = [inf for i in range(n+1)]
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in appear:
        x[l] = 0
    appear.add(l)
    appear.add(r)
    data.append([l,r,d])
count = 0
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import deque
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
appear = set()
x = [inf for i in range(n+1)]
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in appear:
        x[l] = 0
    appear.add(l)
    appear.add(r)
    data.append([l,r,d])
count = 0
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print(x)
            print("No")
            exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
from collections import deque
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
appear = set()
x = [inf for i in range(n+1)]
for i in range(m):
    l,r,d = map(int, input().split())
    if l not in appear:
        x[l] = 0
    appear.add(l)
    appear.add(r)
    data.append([l,r,d])
q = deque(data)
count = 0
while q:
    k = q.pop()
    l,r,d = k
    if x[l] == inf:
        q.appendleft(k)
    else:
        if x[r] == inf:
            x[r] = x[l] + d
        else:
            if x[r] != x[l] + d:
                print("No")
                exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
x = [inf for i in range(n+1)]
for i in range(m):
    data.append(list(map(int, input().split())))
    if i == 0:
        x[data[0][0]] = 0
data.sort(key = lambda a:(a[0],a[1]))
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
data = []
inf = -1 * (10**2)
x = [inf for i in range(n+1)]
for i in range(m):
    data.append(list(map(int, input().split())))
    if i == 0:
        x[data[0][0]] = 0
data.sort(key = lambda a:(a[0],a[1]))
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
data = []
inf = -1 * (10**12)
x = [inf for i in range(n+1)]
for i in range(m):
    data.append(list(map(int, input().split())))
    if i == 0:
        x[data[0][0]] = 0
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
data = []
inf =-1 * (10**9)
x = [inf for i in range(n+1)]
for i in range(m):
    data.append(list(map(int, input().split())))
    if i == 0:
        x[data[0][0]] = 0
for i in range(m):
    l,r,d = data[i]
    if x[r] == inf:
        x[r] = x[l] + d
    else:
        if x[r] != x[l] + d:
            print("No")
            exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
# D - People on a Line

import sys

N, M = map(int, sys.stdin.buffer.readline().split())
LRD = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

LRD = [[LRD[i][0]-1, LRD[i][1]-1, LRD[i][2]] for i in range(M)]

if N == 1 or M == 0:
    print("Yes")
    exit()

x = [0.1] * N

for i in range(M):
    if x[LRD[i][0]] == 0.1 and x[LRD[i][1]] == 0.1:
        x[LRD[i][0]] = 0
        x[LRD[i][1]] = LRD[i][2]

    elif x[LRD[i][0]] == 0.1 and x[LRD[i][1]] != 0.1:
        x[LRD[i][0]] = x[LRD[i][1]] - LRD[i][2]

    elif x[LRD[i][0]] != 0.1 and x[LRD[i][1]] == 0.1:
        x[LRD[i][1]] = x[LRD[i][0]] + LRD[i][2]

    elif x[LRD[i][1]] - x[LRD[i][0]] != LRD[i][2]:
        print("No")
        exit()

print("Yes")

Submission status: Wrong Answer

Solution code:
# D - People on a Line

import sys

N, M = map(int, sys.stdin.buffer.readline().split())
LRD = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

LRD = [[LRD[i][0]-1, LRD[i][1]-1, LRD[i][2]] for i in range(M)]

if N == 1 or M == 0:
    print("Yes")
    exit()

x = [0.1] * N

for i in range(M):
    if x[LRD[i][0]] == 0.1 and x[LRD[i][1]] == 0.1:
        x[LRD[i][0]] = 0
        x[LRD[i][1]] = LRD[i][2]

    elif x[LRD[i][0]] == 0.1 and x[LRD[i][1]] != 0.1:
        x[LRD[i][0]] = x[LRD[i][1]] - LRD[i][2]

    elif x[LRD[i][0]] != 0.1 and x[LRD[i][1]] == 0.1:
        x[LRD[i][1]] = x[LRD[i][0]] + LRD[i][2]

    elif x[LRD[i][1]] - x[LRD[i][0]] != LRD[i][2]:
        print("No")
        exit()

x = [x[i] - min(x) for i in range(N)]

if max(x) > 10**9:
    print("No")
else:
    print("Yes")