Problem p03634 (python)

Fixed submission:
import sys
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a-=1
    b-=1
    graph[a].append((b, c))
    graph[b].append((a, c))
q, k = map(int, input().split())
k -= 1
xy = []
for _ in range(q):
    x, y = map(int, input().split())
    x-=1
    y-=1
    xy.append((x, y))

def DFS(node, p_node, length):
    l[node] = length
    for i, j in graph[node]:
        if i==p_node:
            continue
        DFS(i, node, length+j)
sys.setrecursionlimit(2 * n)
l = [0]*n
DFS(k, -1, 0)
for x, y in xy:
    print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
import sys
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a-=1
    b-=1
    graph[a].append((b, c))
    graph[b].append((a, c))
q, k = map(int, input().split())
k -= 1
xy = []
for _ in range(q):
    x, y = map(int, input().split())
    x-=1
    y-=1
    xy.append((x, y))

def DFS(node, p_node, length):
    l[node] = length
    for i, j in graph[node]:
        if i==p_node:
            continue
        DFS(i, node, length+j)
sys.setrecursionlimit(2 * N)
l = [0]*n
DFS(k, -1, 0)
for x, y in xy:
    print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
import sys
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a-=1
    b-=1
    graph[a].append((b, c))
    graph[b].append((a, c))
q, k = map(int, input().split())
k -= 1
xy = []
for _ in range(q):
    x, y = map(int, input().split())
    x-=1
    y-=1
    xy.append((x, y))

def DFS(node, p_node, length):
    l[node] = length
    for i, j in graph[node]:
        if i==p_node:
            continue
        DFS(i, node, length+j)

l = [0]*n
DFS(k, -1, 0)
for x, y in xy:
    print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
import sys
n = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x, y))

l = [0]*(n+1)
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    try:
      if i==p_node:
        continue
    except TypeError:
      sys.exit(0)
    DFS(i, node, length+j)

DFS(k, -1, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
import sys
n = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x, y))

l = [0]*(n+1)
def DFS(node, p_node, length):
  try:
    l[node] = length
  except:
    sys.exit(0)
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)

DFS(k, -1, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x, y))

l = [0]*(n+1)
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)
DFS(k, -1, 0)
for x, y in xy:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x, y))

l = [0]*(n+1)
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)
DFS(k, -1, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
k-=1
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x-1, y-1))

l = [0]*(n+1)
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)
DFS(k, -1, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
k-=1
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x-1, y-1))

l = [0]*n
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)
DFS(k, -1, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x-1, y-1))

k-=1
seen = [False]*n
l = [0]*n
def DFS(node, length):
  l[node] = length
  seen[node] = True
  for i, j in graph[node]:
    if seen[i]:
      continue
    DFS(i, length+j)
DFS(k, 0)
for x, y in xy:
  print(l[x]+l[y])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
k-=1
seen = [False]*n
l = [0]*n
def DFS(node, length):
  l[node] = length
  seen[node] = True
  for i, j in graph[node]:
    if seen[i]:
      continue
    DFS(i, length+j)

DFS(k, 0)
for _ in range(q):
  x, y = map(int, input().split())
  print(l[x-1]+l[y-1])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
k-=1
seen = [False]*n
l = [0]*n
def DFS(node, length):
  seen[node] = True
  for i, j in graph[node]:
    if seen[i]:
      continue
    l[i] = length+j
    DFS(i, length+j)

DFS(k, 0)
for _ in range(q):
  x, y = map(int, input().split())
  print(l[x-1]+l[y-1])

Buggy submission (verdict: Runtime Error):
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  a-=1
  b-=1
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
k-=1
seen = [False]*n
l = [0]*n
seen[k] = True
def DFS(node, length):
  for i, j in graph[node]:
    if seen[i]:
      continue
    l[i] = length+j
    seen[i] = True
    DFS(i, length+j)

DFS(k, 0)
for _ in range(q):
  x, y = map(int, input().split())
  print(l[x-1]+l[y-1])