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 defaultdict as dd
N, M = map(int, input().split())
e = dd(list)
inf = 10 ** 9
pos = [inf] * (N + 1)
for i in range(M):
  a, b, d = map(int, input().split())
  e[a].append((b, d))
  e[b].append((a, d))
visited = [0] * (N + 1)
s = []
for i in range(1, N + 1):
  if visited[i] == 0:
    s.append(i)
    pos[i] = 0
  while len(s):
    p = s.pop()
    for q in e[p]:
      if pos[q[0]] == inf:
        pos[q[0]] = pos[p] + q[1]
        visited[q[0]] = 1
        s.append(q[0])
      else:
        if abs(pos[q[0]] - pos[p]) != q[1]:
          print("No")
          exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(M)]
for i in range(M):
  Q[i] = sorted(Q[i][: -1]) + [Q[i][-1]]
Q.sort()
p = [-1 for _ in range(N + 1)]
for i in range(M):
  a, b, d = Q[i][0], Q[i][1], Q[i][2]
  if p[b] == -1:
    p[b] = p[a] + d
  elif p[a] == -1:
    p[a] = p[b] - d
  else:
    if abs(p[b] - p[a]) != d:
      print("No")
      exit(0)
print("Yes")
#print(p, Q)

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    t,w={},{}
    p,q=set(),set()
    #way=[[0]*100001 for i in range(100001)]
    if n>10000:
        print("Yo")
        exit()
    way=[[0]*10001 for i in range(10001)]
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w: w[l].add((r,d))
        else: w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    chk=set()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        chk.add((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if way[i][j[0]]!=0 and way[i][j[0]]!=way[i][x[0]]+j[1]:
                    #if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                    ##if (x[0],j[0]) in t and t[(i,j[0])]!=
                        print("No")
                        exit()
                    elif way[i][j[0]]==0:
                        way[i][j[0]]=x[1]+j[1]
                        chk.add((j[0],x[1]+j[1]))
    print("Yes")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    t,w={},{}
    p,q=set(),set()
    #way=[[0]*100001 for i in range(100001)]
    if n>1000:
        print("Yo")
        exit()
    way=[[0]*1001 for i in range(1001)]
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w: w[l].add((r,d))
        else: w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    chk=set()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        chk.add((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if way[i][j[0]]!=0 and way[i][j[0]]!=way[i][x[0]]+j[1]:
                    #if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                    ##if (x[0],j[0]) in t and t[(i,j[0])]!=
                        print("No")
                        exit()
                    elif way[i][j[0]]==0:
                        way[i][j[0]]=x[1]+j[1]
                        chk.add((j[0],x[1]+j[1]))
    print("Yes")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    t,w={},{}
    p,q=set(),set()
    #way=[[0]*100001 for i in range(100001)]
    if n>100:
        print("Yo")
        exit()
    way=[[0]*101 for i in range(101)]
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w: w[l].add((r,d))
        else: w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    chk=set()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        chk.add((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if way[i][j[0]]!=0 and way[i][j[0]]!=way[i][x[0]]+j[1]:
                    #if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                    ##if (x[0],j[0]) in t and t[(i,j[0])]!=
                        print("No")
                        exit()
                    elif way[i][j[0]]==0:
                        way[i][j[0]]=x[1]+j[1]
                        chk.add((j[0],x[1]+j[1]))
    print("Yes")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    t={}
    w={}
    p,q=set(),set()
    #way=[[0]*100001 for i in range(100001)]
    if n>100:
        print("Yo")
        exit()
    way=[[0]*101 for i in range(101)]
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        #t[(l,r)]=d
        way[l][r]=d
        if l in w: w[l].add((r,d))
        else: w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    chk=set()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        chk.add((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if way[i][j[0]]!=0 and way[i][j[0]]!=way[i][x[0]]+j[1]:
                    #if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                    ##if (x[0],j[0]) in t and t[(i,j[0])]!=
                        print("No")
                        exit()
                    elif way[i][j[0]]==0:
                        way[i][j[0]]=x[1]+j[1]
                        chk.add((j[0],x[1]+j[1]))
    print("Yes")    
                    
if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)
 
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y
 
    # 併合
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[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)
 
    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]
    
def main():
    import sys
    input = sys.stdin.readline
    
    N,M = map(int,input().split())
    flag = 0
    A = WeightedUnionFind(N)
    LRD = []
    for i in range(M):
        L,R,D = map(int,input().split())
        A.union(L,R,D)
        LRD.append([L,R,D])    
        
    a = {}
    X = [-1]*(N+1)
    for i in range(M):
        L,R,D = LRD[i]
        if not A.find(L) in a:
            a[A.find(L)] = A.find(L)
            X[A.find(L)] = 0
            X[L] = A.diff(A.find(L),L)
            X[R] = A.diff(A.find(L),R)
        else:
            tmpL = A.diff(A.find(L),L)
            tmpR = A.diff(A.find(R),R)
            if X[L] == -1:
                X[L] = tmpL
            elif X[L] != tmpL:
                flag = 1
                break
            if X[R] == -1:
                X[R] = tmpR
            elif X[R] != tmpR:
                flag = 1
                break
            
    if flag == 1:
        print("No")
    else:
        print("Yes")
    
    
main()

Submission status: Wrong Answer

Solution code:
class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y

    # 併合
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[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)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]
    
def main():
    import sys
    input = sys.stdin.readline
    
    N,M = map(int,input().split())
    flag = 0
    A = WeightedUnionFind(N)
    LRD = []
    for i in range(M):
        L,R,D = map(int,input().split())
        A.union(L,R,D)
        LRD.append([L,R,D])    
        
    a = {}
    X = [-1]*(N+1)
    for i in range(M):
        L,R,D = LRD[i]
        if not A.find(L) in a:
            a[A.find(L)] = L
            X[L] = 0
            X[R] = A.diff(L,R)
        else:
            parentX = X[a[A.find(L)]]
            tmpL = A.diff(a[A.find(L)],L)
            tmpR = A.diff(a[A.find(R)],R)
            if X[L] == -1:
                X[L] = parentX + tmpL
            elif X[L] != parentX + tmpL:
                flag = 1
                break
            if X[R] == -1:
                X[R] = parentX + tmpR
            elif X[R] != parentX + tmpR:
                flag = 1
                break
            
    if flag == 1:
        print("No")
    else:
        print("Yes")
    
    
main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def sol():
    n,m=map(int,input().split())
    t={}
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        t[(l,r)]=d
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    #chk=set()
    chk=deque()
    if len(s)==0:
        print(["No","Yes"][m==0])
        exit()
    for i in s:
        #chk.add((i,0))
        chk.append((i,0))
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for j in w[x[0]]:
                    if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                    #if (x[0],j[0]) in t and t[(i,j[0])]!=
                        print("No")
                        exit()
                    elif (i,j[0]) not in t:
                        t[(i,j[0])]=x[1]+j[1]
                        #chk.add((j[0],x[1]+j[1]))
                        chk.append((j[0],x[1]+j[1]))
                    else:
                        #chk.add((j[0],x[1]+j[1]))
                        chk.append((j[0],x[1]+j[1]))
    print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0:
    print(["No","Yes"][m==0])
    exit()
for i in s:
    chk.add((i,0))
    while len(chk):
        x=chk.pop()
        if x[0] in w:
            for j in w[x[0]]:
                if (i,j[0]) in t and t[(i,j[0])]!=x[1]+j[1]:
                #if (x[0],j[0]) in t and t[(i,j[0])]!=
                    print("No")
                    exit()
                elif (i,j[0]) not in t:
                    t[(i,j[0])]=x[1]+j[1]
                    chk.add((j[0],x[1]+j[1]))
print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0:
    print(["No","Yes"][m==0])
    exit()
print("Yo")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0 and m>0:
    print("No")
    exit()
for i in s:
    if i in w:
        for j in w[i]:
            chk.add(j)
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for k in w[x[0]]:
                    if (i,k[0]) in t and x[1]+k[1]!=t[(i,k[0])]:
                        #print("#",x[0],k[0],x[1],k[1])
                       # print(i,x,k)
                        print("No")
                        exit()
                    elif (i,k[0]) not in t:
                        t[(i,k[0])]=x[1]+k[1]
                        chk.add(k)

#print(t)

print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0 and m>0:
    print("No")
    exit()
for i in s:
    if i in w:
        for j in w[i]:
            chk.add(j)
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for k in w[x[0]]:
                    if (i,k[0]) in t and x[1]+k[1]!=t[(i,k[0])]:
                        #print("#",x[0],k[0],x[1],k[1])
                       # print(i,x,k)
                        print("No")
                        exit()
                    elif (i,k[0]) not in t:
                        t[(i,k[0])]=x[1]+k[1]
#print(t)

print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0 and m>0:
    print("No")
    exit()
for i in s:
    if i in w:
        for j in w[i]:
            chk.add(j)
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for k in w[x[0]]:
                    if (i,k[0]) in t and x[1]+k[1]!=t[(i,k[0])]:
                        #print("#",x[0],k[0],x[1],k[1])
                        print(i,x,k)
                        print("No")
                        exit()
                    elif (i,k[0]) not in t:
                        t[(i,k[0])]=x[1]+k[1]
#print(t)

print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
if len(s)==0 and m>0:
    print("No")
    exit()
for i in s:
    if i in w:
        for j in w[i]:
            chk.add(j)
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for k in w[x[0]]:
                    if (i,k[0]) in t and x[1]+k[1]!=t[(x[0],k[0])]:
                        print("#",x[0],k[0],x[1],k[1])
                        print(x,k)
                        print("No")
                        exit()
                    elif (i,k[0]) not in t:
                        t[(i,k[0])]=x[1]+k[1]
#print(t)

print("Yes")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
t={}
w={}
p,q=set(),set()
for i in range(m):
    l,r,d=[int(i) for i in input().split()]
    t[(l,r)]=d
    if l in w:
        w[l].add((r,d))
    else:
        w[l]={(r,d)}
    p.add(l)
    q.add(r)
s=p-q
chk=set()
for i in s:
    if i in w:
        for j in w[i]:
            chk.add(j)
        while len(chk):
            x=chk.pop()
            if x[0] in w:
                for k in w[x[0]]:
                    if (x[0],k[0]) in t and x[1]+k[1]!=t[(x[0],k[0])]:
                        print("No")
                        exit()
                    elif (x[0],k[0]) not in t:
                        t[(x[0],k[0])]=x[1]+k[1]

if len(s)==0 and m>0:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    s=p
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if x[0]==i or cnt==200000:
                print("No")
                exit()

            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0])
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    s=p
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if x[0]==i or cnt==200000:
                print("No")
            #    exit()

            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0])
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    s=p
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==200000:
                print("No")
            #    exit()

            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0])
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==600000:
                print("Yes")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==600000:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==400000:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==300000:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==200000:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==121212:
                print("No")
                exit()

            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0])
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==85858:
                print("Yes")
                exit()

            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0])
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==85858:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")
 
if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==54545:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")
 
if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==25252:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")
 
if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,m=map(int,input().split())
    w={}
    p,q=set(),set()
    for i in range(m):
        l,r,d=[int(i) for i in input().split()]
        if l in w:
            w[l].add((r,d))
        else:
            w[l]={(r,d)}
        p.add(l)
        q.add(r)
    s=p-q
    for i in s:
        k={j for j in w[i]}
        g=set()
        cnt=0
        while len(k):
            x=k.pop()
            cnt+=1
            if cnt==11111:
                print("No")
                exit()
                
            if x[0] not in w:
                g.add(x)
            else:
                for j in w[x[0]]:
                    k.add((j[0],j[1]+x[1]))
        y=set()
        for j in g:
            if j[0] in y or j[0]==i:
                print("No")
                exit()
            else:
                y.add(j[0]) 
    print("Yes" if len(s) or m==0 else "No")

if __name__=="__main__":
    sol()