Problem p03634 (python)

Fixed submission:
import sys
sys.setrecursionlimit(int(1e5+10))

def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] is not None:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    for i in range(N+1):
        g[i] = []
    for _ in range(N-1):
        a, b, c = list(map(int, input().split(' ')))
        g[a] += [(b, c)]
        g[b] += [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [None]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    cood = []
    for _ in range(Q):
        cood.append(list(map(int, input().split(' '))))

    for x, y in cood:
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] is not None:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    abc = []
    for _ in range(N-1):
        abc.append(list(map(int, input().split(' '))))
    for a, b, c in abc:
        g[a] = g.get(a, []) + [(b, c)]
        g[b] = g.get(b, []) + [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [None]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    cood = []
    for _ in range(Q):
        cood.append(list(map(int, input().split(' '))))

    for x, y in cood:
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] is not None:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    for _ in range(N-1):
        a, b, c = list(map(int, input().split(' ')))
        g[a] = g.get(a, []) + [(b, c)]
        g[b] = g.get(b, []) + [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [None]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    cood = []
    for _ in range(Q):
        cood.append(list(map(int, input().split(' '))))

    for x, y in cood:
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] < 10**16:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    for _ in range(N-1):
        a, b, c = list(map(int, input().split(' ')))
        g[a] = g.get(a, []) + [(b, c)]
        g[b] = g.get(b, []) + [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [10**16]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    cood = []
    for _ in range(Q):
        cood.append(list(map(int, input().split(' '))))

    for x, y in cood:
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] < 10**16:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    for _ in range(N-1):
        a, b, c = list(map(int, input().split(' ')))
        g[a] = g.get(a, []) + [(b, c)]
        g[b] = g.get(b, []) + [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [10**16]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    for _ in range(Q):
        x, y = list(map(int, input().split(' ')))
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

def search_dist(g, dist, e):
    for b, c in g.get(e):
        if dist[b] < 10**15:
            continue
        dist[b] = dist[e] + c
        search_dist(g, dist, b)

def main():
    g = {}
    N = int(input())
    for _ in range(N-1):
        a, b, c = list(map(int, input().split(' ')))
        g[a] = g.get(a, []) + [(b, c)]
        g[b] = g.get(b, []) + [(a, c)]
    Q, K = list(map(int, input().split(' ')))
    dist = [10**15]*(N+1)
    dist[K] = 0
    search_dist(g, dist, K)
    for _ in range(Q):
        x, y = list(map(int, input().split(' ')))
        print(dist[x]+dist[y])

if __name__ == '__main__':
    main()