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:
import sys 

temp=input().split(" ")
n=int(temp[0])
m=int(temp[1])
# p=[[-1 for i in range(n)] for j in range(n)]
p=[]
dis=[-1]*(n+5000)
for i in range(m):
    temp=input()
    temp=list(map(int,temp.split(" ")))
    p.append(temp)
if m!=0:
   
    for temp in p:
        x=int(temp[0])
        y=int(temp[1])    
        d=int(temp[2])
        if dis[x]==-1 and dis[y]==-1: #完全な新情報なら
            dis[x]=0
            dis[y]=d
        elif dis[x]==-1: #xだけ不明なら
            dis[x]=dis[y]-d
        elif dis[y]==-1:
            dis[y]=dis[x]+d
        if dis[y]!=dis[x]+d:
            # print (x)
            # print (y)
            # print (d)
            print("No")
            sys.exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
import sys                                                             
l1 = list(map(int, input().split()))                                   
N = l1[0]                                                              
M = l1[1]                                                              
                                                                       
dat = []                                                               
                                                                       
per_dis = [[ 0 for n in range(N+1)] for n in range(N+1)]               
                                                                       
for n in range(M):                                                     
    x = list(map(int, input().split()))                                
    dat.append(x)                                                      
                                                                       
for x in dat:                                                          
    if per_dis[x[0]][x[1]] == 0 and per_dis[x[0]][x[1]] == 0:          
        per_dis[x[0]][x[1]] = x[2]                                     
        per_dis[x[1]][x[0]] = -x[2]                                    
        for i in range(1,N):                                           
            for j in range(i+1,N):                                     
                print("---")                                           
                print(i)                                               
                print(j)                                               
                print("-0-")                                           
                if per_dis[i][j] !=0 and per_dis[i+1][j+1] != 0:       
                    per_dis[i][j+1] = per_dis[i][j] + per_dis[i+1][j+1]
    else:                                                              
        if per_dis[x[0]][x[1]] != x[2]:                                
            print("No")                                                
            sys.exit(0)                                                
                                                                       
    print()                                                            
    [print(i) for i in per_dis]                                        
    print()                                                            
                                                                       
                                                                       
print("Yes")

Submission status: Wrong Answer

Solution code:
def __logging(visited, rest=[]):
    if rest:
        print(' visited:'+str(visited)+'\n'+
              '    rest:'+str(rest)+'\n')
    else:
        print(' visited:'+str(visited))


def dfs(graph, start, visited):
    if start in visited:
        return True
    
    stack = [(start, 0)]
    distance = []
    flag = True
    while stack:
        temp = stack.pop(0)
        label = temp[0]
        dis = temp[1]
        if label not in visited:
            tmp = graph.get(label, [])
            tmp = list(map(lambda x: (x[0], x[1] + dis), tmp))
            stack = tmp + stack
            visited[label] = dis
        else:
            if flag and dis != visited[label]:
                flag = False
        #__logging(visited, stack)
    return flag

N, M = (int(i) for i in input().split())  

m_list = []
graph = {}

for i in range(M):
    L, R, D = (int(i) for i in input().split())
    if L not in graph:
        graph[L] = [(R, D)]
        m_list.append(L)
    else:
        graph[L].append((R, D))
    if R not in graph:
        graph[R] = [(L, -1 * D)]
        m_list.append(R)
    else:
        graph[R].append((L, -1 * D))

visited = {}
flag = True
for i in m_list:
    ans = dfs(graph, i, visited)
    print(visited)
    if not ans:
        flag = False
        break

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

Submission status: Wrong Answer

Solution code:

def __logging(visited, rest=[]):
    if rest:
        print(' visited:'+str(visited)+'\n'+
              '    rest:'+str(rest)+'\n')
    else:
        print(' visited:'+str(visited))


def dfs(graph, start):
    stack = [(start, 0)]
    visited = {}
    visited_list = []
    distance = []
    while stack:
        temp = stack.pop(0)
        label = temp[0]
        dis = temp[1]
        if label not in visited:
            tmp = graph.get(label, [])
            tmp = list(map(lambda x: (x[0], x[1] + dis), tmp))
            stack = tmp + stack
            visited[label] = dis
            visited_list.append(label)
        else:
            if dis != visited[label]:
                return False, visited_list
        #__logging(visited, stack)
    return True, visited_list

N, M = (int(i) for i in input().split())  

m_list = []
graph = {}

for i in range(M):
    L, R, D = (int(i) for i in input().split())
    if L not in graph:
        graph[L] = [(R, D)]
        m_list.append(L)
    else:
        graph[L].append((R, D))
    if R not in graph:
        graph[R] = [(L, -1 * D)]
        m_list.append(R)
    else:
        graph[R].append((L, -1 * D))

flag = True
while m_list != []:
    ans, visited_list = dfs(graph, m_list[0])
    if not ans:
        flag = False
        break
    m_list = list(set(visited_list) - (set(visited_list) & set(m_list)))

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

Submission status: Wrong Answer

Solution code:
import numpy as np

n, m = map(int, input().split(" "))
l = np.zeros(m, dtype=np.int)
r = np.zeros(m, dtype=np.int)
d = np.zeros(m, dtype=np.int)
for i in range(m):
    l[i], r[i], d[i] = map(int, input().split(" "))
    l[i] -= 1
    r[i] -= 1

pos = np.zeros(n, dtype=np.int)
flag = np.ones(n, dtype=np.int)

for j in range(2):
    first = 0
    for i in range(m):
        if(pos[l[i]] == 0 and pos[r[i]] == 0):
            if(first == 0):
                pos[l[i]] = 1
                pos[r[i]] = pos[l[i]] + d[i]
                first += 1
        elif(pos[l[i]] != 0 and pos[r[i]] == 0):
            pos[r[i]] = pos[l[i]] + d[i]
        elif(pos[l[i]] == 0 and pos[r[i]] != 0):
            pos[l[i]] = pos[r[i]] - d[i]
        else:
            if(pos[r[i]] - pos[l[i]] != d[i]):
                print("No")
                exit()

print("Yes")

Submission status: Wrong Answer

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

    def find_root(self, x):
        while self.parent[self.parent[x]] != self.parent[x]:
            self.parent[x] = self.parent[self.parent[x]]
            self.dist[x] = self.dist[self.parent[x]]
        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()

Submission status: Wrong Answer

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

pars = [None for _ in range(N)]
dists = [0 for _ in range(N)]


def find_root(x, d):
    if pars[x] is None:
        return None, None
    elif pars[x] == x:
        return x, dists[x] + d
    else:
        root, dist_to_root = find_root(pars[x], dists[x])
        pars[x] = root
        dists[x] = dist_to_root + d
        return pars[x], dists[x]


for _ in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1

    if pars[L] is not None and pars[R] is not None:
        rootL, distL = find_root(L, 0)
        rootR, distR = find_root(R, 0)

        if rootL == rootR:
            if distL + D != distR:
                print('No')
                exit(0)
        else:
            rootFromR = abs((distL + D) - distR)
            if distL + D > distR:
                pars[rootR] = rootL
                dists[rootR] = rootFromR
            else:
                pars[rootL] = rootR
                dists[rootL] = rootFromR

    elif pars[L] is not None:
        pars[R] = pars[L]
        dists[R] = D + dists[L]

    elif pars[R] is not None:
        rootR, distR = find_root(R, 0)
        if L < rootR:
            pars[R] = L
            dists[R] = D
            pars[L] = L
            dists[L] = 0
        else:
            pars[L] = rootR
            dists[L] = distR - D

    else:
        pars[R] = L
        dists[R] = D

        pars[L] = L
        dists[L] = 0

    # print(pars)
    # print(dists)
    # print()

print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,input().split())
if M==0:
  print('Yes')
  sys.exit()
people=N*[1145141919]
mlist=[]
for i in range(M):
  L,R,D=map(int,input().split())
  if L<R:
    mlist.append([L,R,D])
  else:
    mlist.append([R,L,(-1)*D])
mlist.sort()
people[mlist[0][0]-1]=0
people[mlist[0][1]-1]=mlist[0][2]
del mlist[0]
for i in mlist:
  if people[i[0]-1]!=1145141919 and people[i[1]-1]==1145141919:
    people[i[1]-1]=people[i[0]-1]+i[2]
  elif people[i[1]-1]!=1145141919 and people[i[0]-1]==1145141919:
    people[i[0]-1]=people[i[1]-1]-i[2]
  elif people[i[0]-1]!=1145141919 and people[i[1]-1]!=1145141919:
    if people[i[1]-1]-people[i[0]-1]!=i[2]:
      print('No')
      sys.exit()
  else:
    people[i[0]-1]=0
    people[i[1]-1]=i[2]
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,input().split())
if M==0:
  print('Yes')
  sys.exit()
people=N*[1145141919]
mlist=[]
for i in range(M):
  L,R,D=map(int,input().split())
  if L<R:
    mlist.append([L,R,D])
  else:
    mlist.append([R,L,(-1)*D])
mlist.sort()
print(mlist)
people[mlist[0][0]-1]=0
people[mlist[0][1]-1]=mlist[0][2]
del mlist[0]
for i in mlist:
  if people[i[0]-1]!=1145141919 and people[i[1]-1]==1145141919:
    people[i[1]-1]=people[i[0]-1]+i[2]
  elif people[i[1]-1]!=1145141919 and people[i[0]-1]==1145141919:
    people[i[0]-1]=people[i[1]-1]-i[2]
  elif people[i[0]-1]!=1145141919 and people[i[1]-1]!=1145141919:
    if people[i[1]-1]-people[i[0]-1]!=i[2]:
      print('No')
      sys.exit()
  else:
    people[i[0]-1]=0
    people[i[1]-1]=i[2]
print('Yes')

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, parent, value):
        self.parent = parent
        self.value = value

class UnionFindTree:
    def __init__(self, size):
        self.table = [Node(i, 0) for i in range(size)]
        self.rank = [0] * size

    def hoge(self, x):
        while self.table[x].parent != self.table[self.table[x].parent].parent:
            parent = self.table[x].parent
            self.table[x].value += self.table[parent].value
            self.table[x].parent = self.table[parent].parent

    def find_root(self, x):
        self.hoge(x)
        return self.table[x].parent

    def find_value(self, x):
        self.hoge(x)
        return self.table[x].value

    def union(self, x, y, d):
        r1 = self.find_root(x)
        r2 = self.find_root(y)
        v1 = self.find_value(x)
        v2 = self.find_value(y)
        if r1 != r2:
            if self.rank[r1] < self.rank[r2]:
                self.table[r2].parent = r1
                self.table[r2].value = (v1 - v2) + d
            else:
                self.table[r1].parent = r2
                self.table[r1].value = (v2 - v1) + d
                if self.rank[r1] == self.rank[r2]:
                    self.rank[r1] += 1
        else:
            if (v2 - v1) != d:
                return False
        return True
    
    def __repr__(self):
        return repr(self.table)

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

main()

Submission status: Wrong Answer

Solution code:
# x = {}
# n, m = map(int, raw_input().split())
# flag = True
# for _ in xrange(m):
#     l, r, d = map(int, raw_input().split())
#     if l in x:
#         if r in x:
#             if x[l][1] + d != x[r][1]:
#                 if x[l][0] == x[r][0]:
#                     flag = False
#                 else:
#                     for k, v in x.iteritems():
#                         if v[0] == r:
#                             x[k] = (x[l][0], v[1] + x[l][1] + d)
#         else:
#             x[r] = (x[l][0], x[l][1] + d)
#     else:
#         x[l], x[r] = (l, 0), (l, d)
# print 'Yes' if flag else 'No'

x = {}
n, m = map(int, raw_input().split())
flag = True
dat = sorted([map(int, raw_input().split()) for _ in xrange(m)], key = lambda x: x[0])
for l, r, d in dat:
    if l not in x and r not in x:
        x[l], x[r] = (l, 0), (l, d)
    elif r in x and l not in x:
        x[l] = (x[r][0], x[r][1] - d)
    elif l in x and r not in x:
        x[r] = (x[l][0], x[l][1] + d)
    else:
        (kl, vl), (kr, vr) = x[l], x[r]
        while x[kl][0] != kl:
            vl += x[kl][1]
            kl = x[kl][0]
        while x[kr][0] != kr:
            vr += x[kr][1]
            kr = x[kr][0]
        if vr - vl != d:
            flag = False
            break
        x[l], x[r] = (kl, vl), (kr, vr)
print 'Yes' if flag else 'No'

Submission status: Wrong Answer

Solution code:
x = {}
n, m = map(int, raw_input().split())
flag = True
dat = sorted([map(int, raw_input().split()) for _ in xrange(m)], key = lambda x: x[0])
for l, r, d in dat:
    if l not in x and r not in x:
        x[l], x[r] = (l, 0), (l, d)
    elif r in x and l not in x:
        x[l] = (x[r][0], x[r][1] - d)
    elif l in x and r not in x:
        x[r] = (x[l][0], x[l][1] + d)
    else:
        (kl, vl), (kr, vr) = x[l], x[r]
        while x[kl][0] != kl:
            vl += x[kl][1]
            kl = x[kl][0]
        while x[kr][0] != kr:
            vr += x[kr][1]
            kr = x[kr][0]
        if vr - vl != d:
            flag = False
            break
        x[l], x[r] = (kl, vl), (kr, vr)
print x
print 'Yes' if flag else 'No'

Submission status: Wrong Answer

Solution code:
nm = input()
NM = nm.split()
N = int(NM[0])
M = int(NM[1])

i = 0
a = []
while i < N:
    a. append(0)
    i += 1

i = 0
b = True
while i < M:
    lrd = input()
    LRD = lrd.split()
    l = int(LRD[0])
    r = int(LRD[1])
    d = int(LRD[2])
    if a[l-1] == 0:
        a[l-1] = 1
    if a[r-1] == 0 or a[r-1] == a[l-1] + d:
        a[r-1] = a[l-1] + d
    else:
        b = False
    i += 1
if b:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,input().split())
if M==0:
  print('Yes')
  sys.exit()
people=N*[1145141919]
mlist=[]
for i in range(M):
  L,R,D=map(int,input().split())
  if L<R:
    mlist.append([L,R,D])
  else:
    mlist.append([R,L,(-1)*D])
mlist.sort()
people[mlist[0][0]-1]=0
people[mlist[0][1]-1]=mlist[0][2]
del mlist[0]
while True:
  if len(mlist)==0:break
  for i in mlist:
    if people[i[0]-1]!=1145141919 and people[i[1]-1]==1145141919:
      people[i[1]-1]=people[i[0]-1]+i[2]
      mlist.remove([i[0],i[1],i[2]])
    elif people[i[1]-1]!=1145141919 and people[i[0]-1]==1145141919:
      people[i[0]-1]=people[i[1]-1]-i[2]
      mlist.remove([i[0],i[1],i[2]])
    elif people[i[0]-1]!=1145141919 and people[i[1]-1]!=1145141919:
      if people[i[1]-1]-people[i[0]-1]!=i[2]:
        print('No')
        sys.exit()
      mlist.remove([i[0],i[1],i[2]])
    else:
      people[i[0]-1]=0
      people[i[1]-1]=i[2]
      mlist.remove([i[0],i[1],i[2]])
print('Yes')

Submission status: Wrong Answer

Solution code:
nm = input()
NM = nm.split()
N = int(NM[0])
M = int(NM[1])

i = 0
a = []
while i < N:
    a. append(0)
    i += 1

i = 0
b = True
while i < M:
    lrd = input()
    LRD = lrd.split()
    l = int(LRD[0])
    r = int(LRD[1])
    d = int(LRD[2])
    if a[l-1] == 0:
        a[l-1] = 1
    if a[r-1] == 0 or a[r-1] == a[l-1] + d:
        a[r-1] = a[l-1] + d
    else:
        b = False
    i += 1
    print(a)
if b:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
nm = input()
NM = nm.split()
N = int(NM[0])
M = int(NM[1])

i = 0
a = []
while i < N:
    a. append(0)
    i += 1

i = 0
b = True
while i < M:
    lrd = input()
    LRD = lrd.split()
    l = int(LRD[0])
    r = int(LRD[1])
    d = int(LRD[2])
    a[l-1] = 1
    if a[r-1] == 0 or a[r-1] == a[l-1] + d:
        a[r-1] = a[l-1] + d
    else:
        b = False
    i += 1
if b:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def main():
  N_PEOPLE, N_INFO = list(map(int, input().split()))

  info_list = []
  for i in range(N_INFO):
    l, r, d = list(map(int, input().split()))
    info_list.append({
      "left":  l-1,
      "right": r-1,
      "dist":  d
    })

  # 各人の位置(不定の場合はNone)
  locations = [None] * N_PEOPLE

  # 各人が登場したか
  appeared = []

  if len(info_list) == 0:
    print("Yes")
    return

  for info in info_list:
    # 左右の人物が共に初登場なら，とりあえずleftを0において，rightを距離の分だけ離す
    if info["left"] not in appeared and info["right"] not in appeared:
      locations[info["left"]] = 0
      locations[info["right"]] = info["dist"]

      appeared.append(info["left"])
      appeared.append(info["right"])

      continue

    # 左の人物だけが初登場なら，rightの人物の位置から見てdist分左におく
    if info["left"] not in appeared and info["right"] in appeared:
      locations[info["left"]] = locations[info["right"]] - info["dist"]

      appeared.append(info["left"])

      continue


    # 右の人物だけが初登場なら，leftの人物から見てdist分右におく
    if info["left"] in appeared and info["right"] not in appeared:
      locations[info["right"]] = locations[info["left"]] + info["dist"]

      appeared.append(info["right"])

      continue

    # 両方の人物が既出なら，距離が正しいかチェックする
    # 正しくなければアウト
    if info["left"] in appeared and info["right"] in appeared:
      if locations[info["right"]] - locations[info["left"]] != info["dist"]:
        print("No")
        return

  print("Yes")

main()

Submission status: Wrong Answer

Solution code:
import numpy as np

n, m = list(map(int, input().split()))
data = []
for i in range(m):
    l, r, d = list(map(int, input().split()))
    row = [0] * (n + 1)
    row[l - 1] = -1
    row[r - 1] = 1
    row[-1] = d
    data.append(row)


if m == 0:
    print('Yes')
else:
    extended = np.array(data, dtype='int64')

    #print(extended)

    coeffs = extended[:,0:2]

    r_ex = np.linalg.matrix_rank(extended)
    r_cf = np.linalg.matrix_rank(coeffs)

    #print("r_ex = {}".format(r_ex))
    #print("r_cf = {}".format(r_cf))

    print('Yes' if r_ex == r_cf else 'No')

Submission status: Wrong Answer

Solution code:
import sys
N, M = list(map(int, input().split()))

a = []
m = 1000000
for i in range(M):
    l, r, d = list(map(int, input().split()))
    if l > r:
        d = -d
        l,r = r,l
    m = min(m, l)
    a.append(tuple([l,r,d]))
a = sorted(a, key=lambda t: t[0])
d = {}
d[m] = True
p = [0 for _ in range(N+1)]

for i in range(M):
    if (a[i][0] in d) and (a[i][1] in d):
        if ((p[a[i][1]] - p[a[i][0]]) != a[i][2]):
            print('No')
            sys.exit()
    elif (a[i][0] not in d) and (a[i][1] in d):
        np = p[a[i][1]] - a[i][2]
        p[a[i][0]] = np
        d[a[i][0]] = True
    elif (a[i][0] in d) and (a[i][1] not in d):
        np = p[a[i][0]] + a[i][2]
        p[a[i][1]] = np
        d[a[i][1]] = True
    else:
        p[a[i][1]] = 0
        d[a[i][1]] = True

        np = p[a[i][1]] - a[i][2]
        p[a[i][0]] = np
        d[a[i][0]] = True
print('Yes')

Submission status: Wrong Answer

Solution code:
N,M= map(int, input().split())
Restriction = []

for i in range(0, M):
    L,R,D = map(int, input().split())
    if L < R :
        Restriction.append((L, R, D))
    else:
        Restriction.append((R, L, -D))

if not Restriction:
    print("Yes")
    exit()

candidate = {min([ rel[0] for rel in Restriction]):0}
Restriction.sort()
tmp = []
for rel in Restriction:
    if rel[0] not in candidate and rel[1] not in candidate:
        tmp.append(rel)
    elif rel[0] in candidate and rel[1] in candidate:
        if candidate[rel[1]] - candidate[rel[0]] != rel[2]:
            print("No")
            break
    elif rel[0] in candidate:
        candidate[rel[1]] = candidate[rel[0]] + rel[2]
    elif rel[1] in candidate:
        candidate[rel[0]] = candidate[rel[1]] - rel[2]
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
N,M= map(int, input().split())
Restriction = []

for i in range(0, M):
    L,R,D = map(int, input().split())
    if L < R :
        Restriction.append((L, R, D))
    else:
        Restriction.append((R, L, -D))

if not Restriction:
    print("Yes")
    exit()

candidate = {min([ rel[0] for rel in Restriction]):0}
Restriction.sort()
tmp = []
for rel in Restriction:
    print(rel)
    if rel[0] not in candidate and rel[1] not in candidate:
        tmp.append(rel)
    elif rel[0] in candidate and rel[1] in candidate:
        if candidate[rel[1]] - candidate[rel[0]] != rel[2]:
            print("No")
            break
    elif rel[0] in candidate:
        candidate[rel[1]] = candidate[rel[0]] + rel[2]
    elif rel[1] in candidate:
        candidate[rel[0]] = candidate[rel[1]] - rel[2]
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
X=[]
for _ in range(M):
    L,R,D=map(int,input().split())
    j = k = None
    for i in range(len(X)):
        if L in X[i]:
            print("L")
            j = i
        if R in X[i]:
            print("R")
            k = i
    if j is None and k is None:
        print(1)
        X.append({L:0, R:D})
    elif j is not None and k is None:
        print(2)
        X[j][R] = X[j][L] + D
    elif j is None:
        print(3)
        X[k][L] = X[k][R] - D
    elif j != k:
        print(4)
        X[j].update(dict([(i,X[k][i]-X[k][R]+X[j][L]+D) for i in X[k].keys()]))
        del X[k]
    elif X[j][R] - X[k][L] != D:
        print("No")
        exit()
    print(X)
print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
N,M=map(int,input().split())
if M==0:
  print('Yes')
  sys.exit()
people=N*[1145141919]
mlist=[]
for i in range(M):
  L,R,D=map(int,input().split())
  if L<R:
    mlist.append([L,R,D])
  else:
    mlist.append([R,L,(-1)*D])
mlist.sort()
people[mlist[0][0]-1]=0
people[mlist[0][1]-1]=mlist[0][2]
del mlist[0]
tmp1=-1
tmp2=-2
while True:
  if len(mlist)==0:break
  elif tmp1==tmp2:
    people[mlist[0][0]-1]=0
    people[mlist[0][1]-1]=mlist[0][2]
    del mlist[0]
  for i in mlist:
    if people[i[0]-1]!=1145141919 and people[i[1]-1]==1145141919:
      people[i[1]-1]=people[i[0]-1]+i[2]
      mlist.remove([i[0],i[1],i[2]])
    elif people[i[1]-1]!=1145141919 and people[i[0]-1]==1145141919:
      people[i[0]-1]=people[i[1]-1]-i[2]
      mlist.remove([i[0],i[1],i[2]])
    elif people[i[0]-1]!=1145141919 and people[i[1]-1]!=1145141919:
      if people[i[1]-1]-people[i[0]-1]!=i[2]:
        print('No')
        sys.exit()
        mlist.remove([i[0],i[1],i[2]])
    else:
      people[i[0]-1]=0
      people[i[1]-1]=i[2]
      mlist.remove([i[0],i[1],i[2]])
  tmp1=tmp2
  tmp2=len(mlist)
print('Yes')

Submission status: Wrong Answer

Solution code:
def getPoint(humans,human):
    if human[0]==None:
        return human[1]
    else:
        return getPoint(humans,humans[human[0]-1])+human[1]

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

result = "Yes"

for _ in range(M):
    L,R,D = map(int,input().split())
    if humans[L-1][1] == None:
        if humans[R-1][1] == None:
            humans[L-1][1] = 0
        else:
            humans[L-1][1] = -D
            humans[L-1][0] = R

    if humans[R-1] == None:
        humans[R+1][0] = L
        humans[R-1][1] = D
    else:
        if getPoint(humans,humans[R-1]) != getPoint(humans,humans[L-1])+D:
            result = "No"

print(result)

Submission status: Wrong Answer

Solution code:
nm = input()
NM = nm.split()
N = int(NM[0])
M = int(NM[1])

i = 0
a = []
while i < N:
    a. append(0)
    i += 1

i = 0
b = True
while i < M:
    lrd = input()
    LRD = lrd.split()
    l = int(LRD[0])
    r = int(LRD[1])
    d = int(LRD[2])
    if a[r-1] == 0 or a[r-1] == a[l-1] + d:
        a[r-1] = a[l-1] + d
    else:
        b = False
    i += 1
if b:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
x=[]
a=[0]*N
for i in range(M):
    x.append(list(map(int,input().split())))
x=sorted(x, key=lambda x:x[0])
if M==0 or M==1:
    print("Yes")
else:
    flag=0
    for j in range(M-1):
        a[x[j][1]-1]=a[x[j][0]-1]+x[j][2]
    if a[x[j+1][1]-1]==a[x[j+1][0]-1]+x[j+1][2]:
        flag=0
    else:
        flag=1
    if flag==1:
        print("No")
    else:
        print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
n, m = map(int, sys.stdin.readline().split())
dat = [ list(map(int, sys.stdin.readline().split())) for _ in range(m) ]
dat.sort()
pos = [ None for _ in range(n) ]
while dat:
    l, r, d = dat.pop()
    pos[l-1] = 0
    pos[r-1] = d
    dat1 = []
    while dat:
        l, r, d = dat.pop()
        if pos[l-1] is not None and pos[r-1] is not None:
            if pos[r-1] - pos[l-1] != d:
                print("No")
                quit()
        elif pos[l-1] is not None:
            pos[r-1] = pos[l-1] + d
        elif pos[r-1] is not None:
            pos[l-1] = pos[r-1] - d
        else:
            dat1.append([l, r, d])
    dat = dat1
print("Yes")

Submission status: Wrong Answer

Solution code:
n,m = map(int,raw_input().split(" "))

data = []
if m == 0:
  print "Yes"
  exit(0)

for a in range(m):
  l,r,d = map(int,raw_input().split(" "))
  if l > r:
    data.append([r,l,-d])
  else:
    data.append([l,r,d])
data.sort()

p = {}

for q in range(len(data)):
  if data[q][0] not in p:
    p[data[q][0]] = -1
    r = q
    while r != len(data) and data[r][0] == data[q][0]:
      if data[r][1] in p:
        if p[ data[q][0] ] != -1:
          if p[ data[q][0] ] != p[data[r][1]] - data[r][2]:
            print "No"
            exit(0)
        p[ data[q][0] ] = p[data[r][1]] - data[r][2]
      r += 1

    if p[data[q][0]] == -1:
      p[data[q][0]] = 0
    
  if data[q][1] in p:
    if p[ data[q][1] ] != p[ data[q][0] ] + data[q][2]:
      print "No"
      exit(0)
  p[ data[q][1] ] = p[ data[q][0] ] + data[q][2]

print "Yes"