Approach
Depth-first search
For ABC407 D — Domino Covering XOR, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 71 lines of Python from the credited upstream file abc407_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def main():5 import sys6 7 sys.setrecursionlimit(10**8)8 9 input = sys.stdin.readline10 11 h, w = map(int, input().split())12 a = [list(map(int, input().split())) for _ in range(h)]13 xor_all = 014 used = [[False] * w for _ in range(h)]15 dominos = list()16 ans = 017 18 for i in range(h):19 for j in range(w):20 xor_all ^= a[i][j]21 22 def dfs(row, col):23 if row >= h:24 candidate = xor_all25 26 for r1, c1, r2, c2 in dominos:27 candidate ^= a[r1][c1]28 candidate ^= a[r2][c2]29 30 nonlocal ans31 ans = max(ans, candidate)32 33 return34 35 36 nr, nc = row, col + 137 38 if nc >= w:39 nr, nc = nr + 1, 040 41 42 if used[row][col]:43 dfs(nr, nc)44 return45 46 47 dfs(nr, nc)48 49 50 if (col + 1 < w) and not used[row][col + 1]:51 used[row][col] = used[row][col + 1] = True52 dominos.append((row, col, row, col + 1))53 dfs(nr, nc)54 dominos.pop()55 used[row][col] = used[row][col + 1] = False56 57 58 if (row + 1 < h) and not used[row + 1][col]:59 used[row][col] = used[row + 1][col] = True60 dominos.append((row, col, row + 1, col))61 dfs(nr, nc)62 dominos.pop()63 used[row][col] = used[row + 1][col] = False64 65 dfs(0, 0)66 print(ans)67 68 69if __name__ == "__main__":70 main()71