Problem p03634 (python)

Fixed submission:
n=int(input())-1

edges = {}
lens = {}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    edges.setdefault(a, []).append(b)
    edges.setdefault(b, []).append(a)
    lens[(a,b)] = c

q, k = map(int, input().split())

dists = {}
def build(x, d=0):
    ss = [(x, 0)]
    while ss:
        x, d = ss.pop()
        if x not in dists:
            dists[x] = d
            for c in edges.get(x, []):
                ss.append((c, d + lens[(min(x,c), max(x,c))]))

build(k)

for _ in range(q):
    [x, y] = map(int, input().split())
    print(dists[x] + dists[y])

Buggy submission (verdict: Runtime Error):
n=int(input())-1

edges = {}
lens = {}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    edges.setdefault(a, []).append(b)
    edges.setdefault(b, []).append(a)
    lens[(a,b)] = c

q, k = map(int, input().split())

dists = {}
def build(x, d=0):
    dists[x] = d
    for c in edges.get(x, []):
        if c not in dists:
            if (min(x,c), max(x,c)) not in  lens:
                import time
                time.sleep(3)
            build(c, d + lens[(min(x,c), max(x,c))])

build(k)

for _ in range(q):
    [x, y] = map(int, input().split())
    print(dists[x] + dists[y])

Buggy submission (verdict: Runtime Error):
n=int(input())-1

edges = {}
lens = {}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    edges.setdefault(a, []).append(b)
    edges.setdefault(b, []).append(a)
    lens[(a,b)] = c

q, k = map(int, input().split())

dists = {}
def build(x, d=0):
    dists[x] = d
    for c in edges[x]:
        if c not in dists:
            build(c, d + lens[(min(x,c), max(x,c))])

build(k)


for _ in range(q):
    [x, y] = map(int, input().split())
    print(dists[x] + dists[y])

Buggy submission (verdict: Runtime Error):
n=int(input())-1

edges = {}
lens = {}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    edges.setdefault(a, []).append(b)
    edges.setdefault(b, []).append(a)
    lens[(a,b)] = c

q, k = map(int, input().split())

used = set()
dists = {}
def build(x, d=0):
    dists[x] = d
    for c in edges[x]:
        if c not in used:
            used.add(c)
            build(c, d + lens[(min(x,c), max(x,c))])

build(k)


for _ in range(q):
    [x, y] = map(int, input().split())
    print(dists[x] + dists[y])

Buggy submission (verdict: Runtime Error):
n=int(input())-1

nodes = {1: (None, 0, 0)}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    if a not in nodes:
        import time
        time.sleep(3)
    nodes[b] = nodes[a], c, c + nodes[a][2]

def dist(a, b):
    if a == b:
        return 0
    if a[2] < b[2]:
        return b[1] + dist(a, b[0])
    if a[2] > b[2]:
        return a[1] + dist(a[0], b)

q, k = map(int, input().split())

for _ in range(q):
    [x, y] = map(int, input().split())
    print(dist(nodes[x], nodes[k]) + dist(nodes[k], nodes[y]))

Buggy submission (verdict: Runtime Error):
n=int(input())-1

nodes = {1: (None, 0, 0)}
for _ in range(n):
    [a, b, c] = map(int, input().split())
    if a > b:
        a,b=b,a
    nodes[b] = nodes[a], c, c + nodes[a][2]

def dist(a, b):
    if a == b:
        return 0
    if a[2] < b[2]:
        return b[1] + dist(a, b[0])
    if a[2] > b[2]:
        return a[1] + dist(a[0], b)

q, k = map(int, input().split())

for _ in range(q):
    [x, y] = map(int, input().split())
    print(dist(nodes[x], nodes[k]) + dist(nodes[k], nodes[y]))