Problem p03450 (python)

Fixed submission:
#!/usr/bin/env python3

def main():
    na = list(map(int, input().split()))
    N = na[0]
    M = na[1]
    lrd_list = []
    for i in range(M):
        na = list(map(int, input().split()))
        lrd_list.append((na[0], na[1], na[2]))

    l_list = [None for i in range(N+1)]
    r_list = [None for i in range(N+1)]
    for lrd in lrd_list:
        l = lrd[0]
        r = lrd[1]
        if l_list[l] is None:
            l_list[l] = [lrd]
        else:
            l_list[l].append(lrd)
        if r_list[r] is None:
            r_list[r] = [lrd]
        else:
            r_list[r].append(lrd)

    positions = [None for i in range(N+1)]
    consistent = True
    i = 1
    while consistent and i <= N:
        s = []
        p = 0
        positions[i] = p
        s.append(i)
        while consistent and 0 < len(s):
            j = s.pop()
            p = positions[j]
            if not l_list[j] is None:
                for lrd in l_list[j]:
                    l, r, d = lrd
                    if positions[r] is None:
                        positions[r] = p + d
                        s.append(r)
                    elif positions[r] != p + d:
                        consistent = False
                        break
                if not consistent:
                    break
            if not r_list[j] is None:
                for lrd in r_list[j]:
                    l, r, d = lrd
                    if positions[l] is None:
                        positions[l] = p - d
                        s.append(l)
                    elif positions[l] != p - d:
                        consistent = False
                        break
        while consistent and i <= N and (not positions[i] is None):
            i += 1


    print('Yes' if consistent else 'No')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

def main():
    na = list(map(int, input().split()))
    N = na[0]
    M = na[1]
    lrd_list = []
    for i in range(M):
        na = list(map(int, input().split()))
        lrd_list.append((na[0], na[1], na[2]))

    tpl_list = []
    for lrd in lrd_list:
        l = lrd[0]
        r = lrd[1]
        l_idx = r_idx = -1
        for i, tpl in enumerate(tpl_list):
            if l_idx == -1 and l in tpl[0]:
                l_idx = i
            if r_idx == -1 and r in tpl[0]:
                r_idx = i
            if l_idx != -1 and r_idx != -1:
                break
        if 0 <= l_idx and 0 <= r_idx:
            if l_idx == r_idx:
                tpl_list[l_idx][1].append(lrd)
            else:
                tpl_l = tpl_list[l_idx]
                tpl_r = tpl_list.pop(r_idx)
                tpl_l[0] |= tpl_r[0]
                tpl_l[1].append(lrd)
                tpl_l[1] += tpl_r[1]
        elif 0 <= l_idx:
            tpl_list[l_idx][0].add(r)
            tpl_list[l_idx][1].append(lrd)
        elif 0 <= r_idx:
            tpl_list[r_idx][0].add(l)
            tpl_list[r_idx][1].append(lrd)
        else:
            tpl_list.append((set([l, r]), [lrd]))

    positions = [None for i in range(N+1)]
    consistent = True
    for tpl in tpl_list:
        for lrd in tpl[1]:
            l, r, d = lrd
            if positions[l] is None and positions[r] is None:
                positions[l] = 0
                positions[r] = d
            elif positions[l] is None:
                positions[l] = positions[r] - d
            elif positions[r] is None:
                positions[r] = positions[l] + d
            else:
                if positions[l] + d != positions[r]:
                    consistent = False
                    break
        if not consistent:
            break


    print('Yes' if consistent else 'No')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

def main():
    na = list(map(int, input().split()))
    N = na[0]
    M = na[1]
    lrd_list = []
    for i in range(M):
        na = list(map(int, input().split()))
        lrd_list.append((na[0], na[1], na[2]))

    tpl_list = []
    for i, lrd in enumerate(lrd_list):
        l = lrd[0]
        r = lrd[1]
        l_idx = r_idx = -1
        for i, tpl in enumerate(tpl_list):
            if l_idx == -1 and l in tpl[0]:
                l_idx = i
            if r_idx == -1 and r in tpl[0]:
                r_idx = i
            if l_idx != -1 and r_idx != -1:
                break
        if 0 <= l_idx and 0 <= r_idx:
            if l_idx == r_idx:
                tpl_list[l_idx][1].append(lrd)
            else:
                tpl_l = tpl_list[l_idx]
                tpl_r = tpl_list.pop(r_idx)
                tpl_l[0] |= tpl_r[0]
                tpl_l[1].append(lrd)
                tpl_l[1] += tpl_r[1]
        elif 0 <= l_idx:
            tpl_list[l_idx][0].add(r)
            tpl_list[l_idx][1].append(lrd)
        elif 0 <= r_idx:
            tpl_list[r_idx][0].add(l)
            tpl_list[r_idx][1].append(lrd)
        else:
            tpl_list.append((set([l, r]), [lrd]))

    positions = [None for i in range(N+1)]
    consistent = True
    for tpl in tpl_list:
        for lrd in tpl[1]:
            l, r, d = lrd
            if positions[l] is None and positions[r] is None:
                positions[l] = 0
                positions[r] = d
            elif positions[l] is None:
                positions[l] = positions[r] - d
            elif positions[r] is None:
                positions[r] = positions[l] + d
            else:
                if positions[l] + d != positions[r]:
                    consistent = False
                    break
        if not consistent:
            break


    print('Yes' if consistent else 'No')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

def main():
    na = list(map(int, input().split()))
    N = na[0]
    M = na[1]
    lrd_list = []
    for i in range(M):
        na = list(map(int, input().split()))
        lrd_list.append((na[0], na[1], na[2]))

    tpl_list = []
    for i, lrd in enumerate(lrd_list):
        l = lrd[0]
        r = lrd[1]
        l_idx = r_idx = -1
        for i, tpl in enumerate(tpl_list):
            if l_idx == -1 and l in tpl[0]:
                l_idx = i
            if r_idx == -1 and r in tpl[0]:
                r_idx = i
            if l_idx != -1 and r_idx != -1:
                break
        if 0 <= l_idx and 0 <= r_idx:
            if l_idx == r_idx:
                tpl_list[l_idx][1].append(lrd)
            else:
                tpl = tpl_list.pop(r_idx)
                tpl_list[l_idx][0] |= tpl[0]
                tpl_list[l_idx][1].append(lrd)
                tpl_list[l_idx][1] += tpl[1]
        elif 0 <= l_idx:
            tpl_list[l_idx][0].add(r)
            tpl_list[l_idx][1].append(lrd)
        elif 0 <= r_idx:
            tpl_list[r_idx][0].add(l)
            tpl_list[r_idx][1].append(lrd)
        else:
            tpl_list.append((set([l, r]), [lrd]))

    positions = [None for i in range(N+1)]
    consistent = True
    for tpl in tpl_list:
        for lrd in tpl[1]:
            l, r, d = lrd
            if positions[l] is None and positions[r] is None:
                positions[l] = 0
                positions[r] = d
            elif positions[l] is None:
                positions[l] = positions[r] - d
            elif positions[r] is None:
                positions[r] = positions[l] + d
            else:
                if positions[l] + d != positions[r]:
                    consistent = False
                    break
        if not consistent:
            break


    print('Yes' if consistent else 'No')


if __name__ == '__main__':
    main()