有人知道問題在哪裡嗎 (´-ι_-`)
from collections import deque, defaultdict
p, q, r, m = map(int, input().split())
inputs = list(map(int, input().split()))
trtp = input().split()
trreq = list(map(lambda x: 1 if x=='4' else 2, trtp))
trcon = [-1]*q
output = [-1]*r
lines = defaultdict(lambda: [])
for _ in range(m):
tmp = tuple(map(int, input().split()))
lines[tmp[0]-1].append(tmp[1]-1)
queue = deque( (j, 1, inputs[i]) for i in range(p) for j in lines[i])
ms = float('-inf')
while queue:
target, step, val = queue.popleft()
if p+q<=target:
output[target-p-q] = val
ms = max(ms, step-1)
else:
if trtp[target - p] == '4':
for j in lines[target]:
queue.append( (j, step+1, int(not val)) )
else:
trreq[target-p]-=1
if trreq[target-p]:
trcon[target-p] = (val, step)
else:
prev, pres = trcon[target-p]
if trtp[target-p] == '1':
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev&val ) )
elif trtp[target-p] == '2':
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev|val ) )
else:
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev^val ) )
print(ms)
print(*output)
有人知道問題在哪裡嗎 (´-ι_-`)
python code:
from collections import deque, defaultdict
p, q, r, m = map(int, input().split())
inputs = list(map(int, input().split()))
trtp = input().split()
trreq = list(map(lambda x: 1 if x=='4' else 2, trtp))
trcon = [-1]*q
output = [-1]*rlines = defaultdict(lambda: [])
for _ in range(m):
tmp = tuple(map(int, input().split()))
lines[tmp[0]-1].append(tmp[1]-1)queue = deque( (j, 1, inputs[i]) for i in range(p) for j in lines[i])
ms = float('-inf')while queue:
target, step, val = queue.popleft()
if p+q<=target:
output[target-p-q] = val
ms = max(ms, step-1)
else:
if trtp[target - p] == '4':
for j in lines[target]:
queue.append( (j, step+1, int(not val)) )
else:
trreq[target-p]-=1
if trreq[target-p]:
trcon[target-p] = (val, step)
else:
prev, pres = trcon[target-p]
if trtp[target-p] == '1':
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev&val ) )
elif trtp[target-p] == '2':
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev|val ) )
else:
for j in lines[target]:
queue.append( (j, max(step, pres)+1, prev^val ) )
print(ms)
print(*output)
恩..?突然又全部通過了...難道是改測資了嗎