Problem p03634 (python)

Fixed submission:
from collections import defaultdict,deque
def main():
    N = int(input())
    depth = [-1 for _ in range(N)]
    tree = defaultdict(list)
    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]
    Q,K = map(int,input().split())
    K -= 1
    stack = deque([K])
    depth[K] = 0
    while stack:
        v = stack.popleft()
        for e in tree[v]:
            u,c = e
            if u == K:
                continue
            if depth[u] == -1:
                stack.appendleft(u)
                depth[u] = depth[v] + c

    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def main():
    N = int(input())
    depth = [0 for _ in range(N)]
    tree = defaultdict(list)

    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]

    def dfs(v,p,d):
        depth[v] = d
        for u in tree[v]:
            if u[0] == p:continue
            if tree[u[0]]:
                dfs(u[0],v,d+u[1])

    Q,K = map(int,input().split())
    K -= 1
    dfs(K,-1,0)
    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def main():
    N = int(input())
    depth = [0 for _ in range(N)]
    def dfs(v,p,d):
        depth[v] = d
        if tree[v]:
            for u in tree[v]:
                if u[0] == p:continue
                dfs(u[0],v,d+u[1])
    tree = defaultdict(list)
    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]
    Q,K = map(int,input().split())
    K -= 1
    dfs(K,-1,0)
    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def main():
    N = int(input())
    depth = [0 for _ in range(N)]
    def dfs(v,p,d):
        depth[v] = d
        for u in tree[v]:
            if u[0] == p:continue
            dfs(u[0],v,d+u[1])
    tree = defaultdict(list)
    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]
    Q,K = map(int,input().split())
    K -= 1
    dfs(K,-1,0)
    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
depth = [0 for _ in range(10**5+10)]
tree = defaultdict(list)
def dfs(v,p,d):
    depth[v] = d
    for u in tree[v]:
        if u[0] == p:continue
        dfs(u[0],v,d+u[1])
def main():
    N = int(input())
    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]
    Q,K = map(int,input().split())
    K -= 1
    dfs(K,-1,0)
    Ans = []
    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        Ans.append(ans)
    for ans in Ans:
        print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def main():
    N = int(input())
    depth = [0 for _ in range(N)]
    def dfs(v,p,d):
        depth[v] = d
        for u in tree[v]:
            if u[0] == p:continue
            dfs(u[0],v,d+u[1])
    tree = defaultdict(list)
    for _ in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        tree[a] += [[b,c]]
        tree[b] += [[a,c]]
    Q,K = map(int,input().split())
    K -= 1
    dfs(K,-1,0)
    Ans = []
    for _ in range(Q):
        x,y = map(int,input().split())
        x -= 1
        y -= 1
        ans = depth[x]+depth[y]
        Ans.append(ans)
    for ans in Ans:
        print(ans)


if __name__ == "__main__":
    main()