你可以浏览这个题解的中文版本。
The operation in the problem can be interpreted as selecting a subtree (not the whole tree) to delete it. Thus we can translate it into a DAG game, let to be the Sprague-Grundy Number of the subtree rooted at .
It’s reasonable to guess that , and we’ll prove this.
- has no son, the SG Number is .
- has exactly one son, let it be , we need to prove that :
- Delete the subtree rooted at , the SG Number is .
- Delete another subtree, obviously it is within the subtree rooted at . Using mathematical induction, will appear in the SG Numbers of the remaining part of the subtree. Thus, .
- has more than one son, we can break the sons into parts, according to the SG Theorem, equals to the xor sum of the SG Numbers of the parts.
By a simple DFS, we can get the SG Number of the whole tree.