跳到正文

题解

Solution: AT_agc017_d [AGC017D] Game on Tree

silvecor约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年8月8日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 3 分钟
获赞
4

版本

  1. v1当前

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

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

It’s reasonable to guess that SG⁡(x)={0x has no sonxor⁡y is a son of xSG⁡(y)+1otherwise\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. xx has no son, the SG Number is 00.
  2. xx has exactly one son, let it be yy, we need to prove that SG⁡(x)=SG⁡(y)+1\operatorname{SG}(x)=\operatorname{SG}(y)+1:
    • Delete the subtree rooted at yy, the SG Number is 00.
    • Delete another subtree, obviously it is within the subtree rooted at yy. Using mathematical induction, 0,1,…,SG⁡(y)−10, 1, \dots, \operatorname{SG}(y) - 1 will appear in the SG Numbers of the remaining part of the subtree. Thus, SG⁡(x)=mex⁡(1,2,…,SG⁡(y))=SG⁡(y)+1\operatorname{SG}(x)=\operatorname{mex}(1, 2, \dots, \operatorname{SG}(y))=\operatorname{SG}(y)+1.
  3. xx has more than one son, we can break the sons into parts, according to the SG Theorem, SG⁡(x)\operatorname{SG}(x) 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.