Editorial
Define an integer for each vertex as follows.
- If is a leaf, then .
- If a child with even exists, take the minimum such value and add .
- Otherwise, take the maximum among all children and add .
These values can be computed from the leaves upward. If is positive and even, all children have odd values and their maximum is . If is odd, there is an even-valued child with value , and every even-valued child has value at least .
When the tokens are at , the player to move loses exactly when is even. Prove this by induction as both tokens move downward.
Suppose the minimum is an even value . If , one token has no legal move. If , every child of a token with value has an odd value smaller than . An even-valued child of the other token has value at least ; if that child is odd-valued instead, the minimum of the two child values is still odd. Thus every move reaches a position winning for the opponent.
Now suppose the minimum is odd. A token with value can move to an even-valued child with value . If the other token has an odd value, it can move to an even-valued child with value at least . If it has an even value, it can move to an odd-valued child with value at least . Hence a move to a position with an even minimum always exists.
Bob moves first after Alice chooses a pair. Therefore we count pairs with an even minimum of their values. Count vertices by value and process these values in decreasing order. At an even value, add the pairs within its frequency group and the pairs between this group and all larger values. Store the answer in a -bit integer.
The time and space complexity are both per case.
Solution written by GPT6