Ezvor

3973. Distinct Gate Paths to LCA

HardArray Preparing judge… LeetCode
ArrayMathDynamic ProgrammingBit ManipulationTreeDepth-First Search

Hints

Hint 1
For one node, the transition between card colors can be represented by a 2 x 2 matrix: red and blue gates keep the same color, while white gates switch the color.
Hint 2
Use binary lifting for ancestors, and store the product of transition matrices for each upward jump.
Hint 3
For a query, find the LCA, compute the number of ways for Alice and Bob to move from their nodes to it, sum over both possible ending card colors, multiply the two values modulo 109 + 7, then XOR it into the answer.