Solution: AT_agc017_d [AGC017D] Game on Tree

· · 题解

你可以浏览这个题解的中文版本。

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 \operatorname{SG}(x) to be the Sprague-Grundy Number of the subtree rooted at x.

It's reasonable to guess that \operatorname{SG}(x) = \begin{cases} 0 & x \text{ has no son}\\ \operatorname{xor}_{y \text{ is a son of } x} \operatorname{SG}(y) + 1 & \text{otherwise} \end{cases}, and we'll prove this.

  1. - Delete the subtree rooted at $y$, the SG Number is $0$. - Delete another subtree, obviously it is within the subtree rooted at $y$. Using mathematical induction, $0, 1, \dots, \operatorname{SG}(y) - 1$ will appear in the SG Numbers of the remaining part of the subtree. Thus, $\operatorname{SG}(x)=\operatorname{mex}(1, 2, \dots, \operatorname{SG}(y))=\operatorname{SG}(y)+1$.

By a simple DFS, we can get the SG Number of the whole tree.