QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91504#5102. Dungeon CrawlerSerdahsufyanCompile Error//Python31.6kb2023-03-28 23:30:452023-03-28 23:30:45

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-28 23:30:45]
  • 评测
  • [2023-03-28 23:30:45]
  • 提交

answer

import sys

# Read input
n, q = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    graph[u-1].append((v-1, w))
    graph[v-1].append((u-1, w))

scenarios = []
for _ in range(q):
    s, k, t = map(int, input().split())
    scenarios.append((s-1, k-1, t-1))

# Depth-first search to explore the dungeon
def dfs(node, visited, key, trap):
    visited[node] = True
    found_key = (node == key)
    found_trap = (node == trap)
    for neighbor, weight in graph[node]:
        if not visited[neighbor]:
            nk, nt = dfs(neighbor, visited, key, trap)
            found_key |= nk
            found_trap |= nt
    return found_key, found_trap

# Compute the minimum time to explore the entire dungeon for each scenario
for s, k, t in scenarios:
    visited = [False] * n
    _, has_trap = dfs(s, visited, k, t)
    if has_trap:
        print("impossible")
    else:
        visited = [False] * n
        _, has_key = dfs(k, visited, s, t)
        if not has_key:
            print("impossible")
        else:
            visited = [False] * n
            total_time = 0
            def explore(node, visited):
                visited[node] = True
                nonlocal total_time
                for neighbor, weight in graph[node]:
                    if not visited[neighbor]:
                        explore(neighbor, visited)
                        total_time += weight
            explore(s, visited)
            if all(visited):
                print(total_time)
            else:
                print("impossible")

Details

  File "answer.code", line 44
    nonlocal total_time
    ^^^^^^^^^^^^^^^^^^^
SyntaxError: no binding for nonlocal 'total_time' found