做题记录 25.2.7

· · 个人记录

\purple\odot [AGC035C] Skolem XOR Tree

令 1 为根,对于每个偶数 x(1<x< n),将 x 和 x+1 一起考虑

可构造 x-(x+1)-1-(x+n)-(x+n+1),显然 x\oplus (x+1)\oplus 1\oplus x=x,(x+1)\oplus 1\oplus x\oplus (x+1)=x+1,即 x 和 x+1 都满足要求

然后考虑节点 1,令 u 为任意一个叶子(取 [1,n) 的偶数即可),则可令 n+1 接在这个叶子下方,1\oplus u\oplus (u+1)\oplus 1=1,符合要求

当 n 为偶数时,上述过程没有加入 n

令 t 为 n 的任意一个二进制位(实现时取 \operatorname{lowbit} 即可),则可将 n 和 n+n 分别接在 t+1 和 n-t+n 下,n\oplus (t+1)\oplus 1\oplus (n-t)\oplus n=n,符合要求

时间复杂度 O(n)

代码

参考