QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#314002#5763. Cheating a Boolean Treemfshi030 0ms0kbPython31.9kb2024-01-25 11:09:122024-01-25 11:09:12

Judging History

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

  • [2024-01-25 11:09:12]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [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

result: