Problem p03452 (python)

Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math

def calc(A, B, C, now_t):
    ans = A * now_t + B * math.sin(C * now_t * math.pi)
    return ans


def main():
    num, hint_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(hint_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)

    for i in range(hint_num):
        l, r , d = data[i]
        graph_data[l].add(r)
        graph_data[r].add(l)
        dist_data[(l, r)] = d
        dist_data[(r, l)] = -1 * d


    position = ['x' for i in range(num + 1)]
    flg = 1
    for i in range(1, num + 1):
        if position[i] != 'x':
            continue
        position[i] = 0
        now_set = set()
        next_set = set()
        now_set.add(i)

        while len(now_set) > 0:
            for now_ele in now_set:
                for next_ele in graph_data[now_ele]:
                    length = dist_data[(now_ele, next_ele)]
                    if position[next_ele] == 'x':
                        position[next_ele] = position[now_ele] + length
                        next_set.add(next_ele)
                    else:
                        if position[next_ele] != position[now_ele] + length:
                            flg = 0
                            break
            now_set = next_set
            next_set = set()

    # print(position)

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





if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
from timeout_decorator import timeout, TimeoutError



@timeout(1.8)
def main():
    num, hint_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(hint_num)]

    graph_data = defaultdict(set)
    dist_data = defaultdict(int)
    dist_ind = defaultdict(int)
    for i in range(hint_num):
        l, r , d = data[i]
        graph_data[l].add(r)
        graph_data[r].add(l)
        dist_data[(l, r)] = d
        dist_data[(r, l)] = -1 * d
        dist_ind[(l, r)] = i
        dist_ind[(r, l)] = i

    position = ['x' for i in range(num + 1)]
    dist_visit = [0 for i in range(hint_num)]
    check_point = [0 for i in range(num + 1)]

    flg = 1
    for i in range(hint_num):
        if dist_visit[i]:
            continue
        # dist_visit[i] = 1

        l, r, d = data[i]
        now_set = set()
        next_set = set()
        now_set.add(l)
        now_set.add(r)

        while len(now_set) > 0:
            for now_ele in now_set:
                if position[now_ele] == 'x':
                    position[now_ele] = 0

                for next_ele in graph_data[now_ele]:
                    if check_point[next_ele]:
                        continue
                    length = dist_data[(now_ele, next_ele)]
                    len_ind = dist_ind[(now_ele, next_ele)]
                    if dist_visit[len_ind]:
                        continue
                    dist_visit[len_ind] = 1
                    if position[next_ele] == 'x':
                        position[next_ele] = position[now_ele] + length
                        next_set.add(next_ele)
                    else:
                        if position[next_ele] != position[now_ele] + length:
                            flg = 0
                            break
                check_point[now_ele] = 1
            now_set = next_set
            next_set = set()

    # print(position)

    max_data = -10 ** 10
    min_data = 10 ** 10
    for i in range(1, num + 1):
        if position[i] == 'x':
            continue
        max_data = max(max_data, position[i])
        min_data = min(min_data, position[i])

    if max_data - min_data > 10 ** 9:
        flg = 0

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





if __name__ == '__main__':
    try:
        main()
    except:
        print('No')