Buggy python submission (verdict: Time Limit Exceeded):
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 visited[start] != -99999:
        return True
    
    stack = [(start, 0)]
    distance = []
    flag = True
    while stack:
        temp = stack.pop(0)
        label = temp[0]
        dis = temp[1]
        if visited[label] == -99999:
            tmp = graph[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())  

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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


Fixed submission:


def dfs(graph, start, visited):
    if visited[start] != -99999:
        return True
    
    stack = [start]
    visited[start] = 0
    while stack:
        label = stack.pop()
        for nextnode, distance in graph[label]:
            if visited[nextnode] == -99999:
                stack.append(nextnode)
                visited[nextnode] = visited[label] + distance
            elif visited[nextnode] != visited[label] + distance:
                return False
    return True

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

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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

Buggy python submission (verdict: Time Limit Exceeded):
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 range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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


Fixed submission:


def dfs(graph, start, visited):
    if visited[start] != -99999:
        return True
    
    stack = [start]
    visited[start] = 0
    while stack:
        label = stack.pop()
        for nextnode, distance in graph[label]:
            if visited[nextnode] == -99999:
                stack.append(nextnode)
                visited[nextnode] = visited[label] + distance
            elif visited[nextnode] != visited[label] + distance:
                return False
    return True

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

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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

Buggy python submission (verdict: Time Limit Exceeded):
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)]
    visited = {}
    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 range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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


Fixed submission:


def dfs(graph, start, visited):
    if visited[start] != -99999:
        return True
    
    stack = [start]
    visited[start] = 0
    while stack:
        label = stack.pop()
        for nextnode, distance in graph[label]:
            if visited[nextnode] == -99999:
                stack.append(nextnode)
                visited[nextnode] = visited[label] + distance
            elif visited[nextnode] != visited[label] + distance:
                return False
    return True

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

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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

Buggy python submission (verdict: Time Limit Exceeded):

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 = range(N)
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)]
    else:
        graph[L].append((R, D))
    if R not in graph:
        graph[R] = [(L, -1 * D)]
    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(m_list))

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

Fixed submission:


def dfs(graph, start, visited):
    if visited[start] != -99999:
        return True
    
    stack = [start]
    visited[start] = 0
    while stack:
        label = stack.pop()
        for nextnode, distance in graph[label]:
            if visited[nextnode] == -99999:
                stack.append(nextnode)
                visited[nextnode] = visited[label] + distance
            elif visited[nextnode] != visited[label] + distance:
                return False
    return True

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

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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

Buggy python submission (verdict: Runtime Error):


import heapq

def dijkstra(nodes, N):
    dist = [float('inf') for i in range(N + 2)]
    prev = [float('inf') for i in range(N + 2)]

    dist[0] = 0
    q = []
    heapq.heappush(q, (0, 0))

    while len(q) != 0:
        prov_cost, src = heapq.heappop(q)
        if dist[src] < prov_cost:
            continue

        for (nextnum, dis) in nodes[src].nextnode:
            cost = dis
            if dist[nextnum] >= dist[src] + cost:
                dist[nextnum] = dist[src] + cost
                heapq.heappush(q, (dist[nextnum], nextnum))
                prev[nextnum] = src
        
    return get_path(N + 1, prev)

def get_path(goal, prev):
    path = [goal]
    dest = goal

    while prev[dest] != float('inf'):
        path.append(prev[dest])
        dest = prev[dest]

    return list(reversed(path))

class People:
    def __init__(self, num):
        self.nodenum = num
        self.nextnode = []

    def set_node(self, num, dis):
        self.nextnode.append((num, dis))

    def node_adj(self):
        if self.nextnode == []:
            self.nextnode.append((self.nodenum + 1, 0))


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

nodes = []
for i in range(N+2):
    nodes.append(People(i))

for i in range(M):
    L, R, D = (int(i) for i in input().split())
    nodes[L].set_node(R, D)

for i in range(N+1):
    nodes[i].node_adj()

ans = dijkstra(nodes, N)

flag = True
for i in range(N+1):
    if ans[i] != i:
        flag = False
if flag:
    print('Yes')
else:
    print('No')

Fixed submission:


def dfs(graph, start, visited):
    if visited[start] != -99999:
        return True
    
    stack = [start]
    visited[start] = 0
    while stack:
        label = stack.pop()
        for nextnode, distance in graph[label]:
            if visited[nextnode] == -99999:
                stack.append(nextnode)
                visited[nextnode] = visited[label] + distance
            elif visited[nextnode] != visited[label] + distance:
                return False
    return True

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

graph = [[] for i in range(N+1)]

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


visited = [-99999 for i in range(N+1)]

flag = True
for i in range(1, N + 1):
    ans = dfs(graph, i, visited)
    if not ans:
        flag = False
        break

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