A - Communicate Topological Order Editorial by evima
Let \(C\) be the answer we seek.
Define \(Q\) such that \(Q_{P_i}=i\) for \(i=1,2,\dots,N\).
For \(1,2,\dots,N\), define \(f(i)\) recursively as follows:
- \(f(1)=1\)
- When \(i \geq 2\), if there exists an integer \(j\) between \(1\) and \(N\) satisfying the following conditions, then \(f(i)=f(i-1)+1\); otherwise, \(f(i)=f(i-1)\)
- Vertex \(Q_i\) can be reached from vertex \(Q_j\) by following directed edges
- \(f(j)=f(i-1)\)
We prove that \(C=N-f(N)\).
Takahashi will tell Aoki that \(P_{Q_i}=i\) for \(i\) \((2 \leq i \leq N)\) where \(f(i)=f(i-1)\). In this case, Takahashi tells Aoki \(N-f(N)\) elements. Assume there exists a special permutation \(P'\) other than \(P\) that satisfies the told conditions. Since \(P\neq P'\), there exists \(k\) such that:
- \(f(k) = f(k-1)+1\)
- \(k>P'_{Q_k}\)
Take the minimum such \(k\). By the definition of \(f\), there exists \(k'\) satisfying:
- \(k'<k\)
- \(f(k')=f(k)-1\)
- Vertex \(Q_k\) can be reached from vertex \(Q_{k'}\) by following directed edges
Since the positions \(k'+1,k'+2,\dots,k-1\) are told to Aoki, \(k' \geq P'_{Q_k}\). Also, from the third condition and the fact that \(P'\) is a special permutation, \(P'_{Q_{k'}}<P'_{Q_k}\).
When the position \(k'\) is not told to Aoki, \(f(k')=f(k'-1)+1\), so \(k' \leq P'_{Q_{k'}}\) by the minimality of \(k\).
In this case, \(k' \leq P'_{Q_{k'}}<P'_{Q_k} \leq k'\), which is a contradiction.
When the position \(k'\) is told to Aoki, \(k'=P'_{Q_{k'}}<P'_{Q_k} \leq k'\), which is a contradiction.
Therefore, no such \(P'\) exists, and Aoki can uniquely determine \(P\), so we have \(C \leq N-f(N)\).
Suppose Takahashi tells Aoki fewer than \(N-f(N)\) terms. Then, by the pigeonhole principle, there exist \(i_1,i_2\) satisfying:
- \(f(P_{i_1})=f(P_{i_2})\)
- \(P_{i_1}\) and \(P_{i_2}\) are not told by Takahashi to Aoki
In this case, Aoki cannot distinguish between \(P_{i_1}\) and \(P_{i_2}\) and cannot determine each term of \(P\). Therefore, \(C \geq N-f(N)\).
Thus, we have proved that \(C=N-f(N)\). By computing \(f\) according to its definition, this problem can be solved in \(O(N+M)\) time.
posted:
last update: