QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#314002 | #5763. Cheating a Boolean Tree | mfshi03 | 0 | 0ms | 0kb | Python3 | 1.9kb | 2024-01-25 11:09:12 | 2024-01-25 11:09:12 |
answer
n = int(input())
def compute():
M, V = tuple(map(int, input().split()))
gates = []
change = []
nodes = []
for j in range(1, (M-1)//2 + 1):
b, c = tuple(map(int, input().split()))
gates.append(b)
change.append(c)
for j in range((M-1)//2 + 1, M + 1):
gates.append(-1)
nodes.append(int(input()))
cache = dict()
def dfs(i, v):
if gates[i] < 0: # if it is a leaf node
return 0 if nodes[i] == v else float("inf")
if (i,v) in cache:
return cache[(i,v)]
if nodes[i] == v:
cache[(i,v)] = 0
return cache[(i,v)]
res = float("inf")
if gates[i] == 1 or change[i]:
if v == 1:
res = min(res, dfs(2*i, 1) + dfs(2*i + 1, 1) + (0 if gates[i] == 1 else 1))
else:
res = min(res, dfs(2*i, 1) + dfs(2*i + 1, 0) + (0 if gates[i] == 1 else 1))
res = min(res, dfs(2*i, 0) + dfs(2*i + 1, 1) + (0 if gates[i] == 1 else 1))
res = min(res, dfs(2*i, 0) + dfs(2*i + 1, 0) + (0 if gates[i] == 1 else 1))
if gates[i] == 0 or change[i]:
if v == 1:
res = min(res, dfs(2*i, 0) + dfs(2*i + 1, 1) + (0 if gates[i] == 0 else 1))
res = min(res, dfs(2*i, 1) + dfs(2*i + 1, 0) + (0 if gates[i] == 0 else 1))
res = min(res, dfs(2*i, 1) + dfs(2*i + 1, 1) + (0 if gates[i] == 0 else 1))
else:
res = min(res, dfs(2*i, 0) + dfs(2*i + 1, 0) + (0 if gates[i] == 0 else 1))
cache[(i,v)] = res
return cache[(i,v)]
val = dfs(0, V)
if val != float("inf"):
return val
return "IMPOSSIBLE"
for i in range(n):
val = compute()
print(f"Case #{i+1}: {val}")
详细
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
20 11 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 0 1 9 0 0 0 1 1 1 1 0 1 1 0 1 0 1 9 1 0 0 0 1 0 1 1 0 1 0 1 1 1 15 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 0 1 19 1 1 1 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 1 0 1 0 0 0 0 25 1 1 0 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 0 0 0 1 1 0 27 0 0 0 0 ...
output:
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #2:
score: 0
Dangerous Syscalls
input:
20 1247 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 ...
output:
Case #1: 0