Official

B - Special Subsets Editorial by evima


What does the condition mean?

The first condition requires that, if \(i \in T\), all of \(i, f(i), f(f(i)), \cdots\) belong to \(T\). Since \(S\) is finite, there is \(k > 0\) such that \(f^k(i)\) equals \(f^l(i) (l < k)\). Let us take the minimum such \(k\) and the corresponding \(l\). Then, after \(f^k(i)\), the sequence is a repetition of \(f^l(i), \cdots, f^{k-1}(i)\). (In terms of graph theory, each (weakly) connected component in the graph with directed edges \((i, f(i))\) is a cycle plus some paths entering it.) Here, if \(l > 0\), we have \(f^{l-1}(i) \neq f^{k-1}(i)\), but \(f^l(i) = f^k(i)\), which contradicts with the second condition. That is, the conditions require that \(i\) belong to a cycle, and if \(i \in T\), all vertices in the cycle belong to \(T\).

Cycles do not affect each other, so the answer is \(2^C-1\), where \(C\) is the number of cycles, which is simply equal to the number of connected components because of the special form of the graph. We can find it in \(\mathrm{O}(N)\) time.

posted:
last update: