B - Missing Number in Graph Editorial by evima
Let \(c_v\) be the integer on the card placed on vertex \(v\).
Let \(x = c_1\). Since the graph is connected, (by computing BFS-like) \(c_v\) can be expressed as \(x \oplus a_v\) for some non-negative integer \(a_v\).
The conclusion is as follows:
- If \(N\) is even, the answer is uniquely determined as \(\left(\bigoplus_{i=0}^{N} i\right)\ \oplus\ \left(\bigoplus_{v=1}^{N} a_v\right)\)
- If \(N\) is odd, the answer is never uniquely determined →
-1
\(N\) is even
Taking the total xor of \(c_v\) cancels out \(x\) and yields \(\bigoplus_{v=1}^{N} a_v\), which is a value independent of \(x\).
The value obtained by xor-ing the answer with this value should equal the total xor of \(0 \dots N\), and the answer is uniquely determined by the above formula.
\(N\) is odd
For a configuration satisfying the condition, the configuration obtained by flipping the least significant bit of all card integers also satisfies the condition, so there are two or more possible answers.
posted:
last update: