Official

B - Pair Guessing Editorial by evima


If \(N = 1\), the answer is Yes. When \(N = 2\), the answer is Yes if and only if there is at least one 0. Below, assume \(N \geq 3\).

First, in the first question, we need to choose \((A, B)\) such that there exists \((r, c)\) satisfying \(r = A\) or \(c = B\) and \(S_{r,c} = \) 0 (we will call this a “good question” from now on). If we choose \(A, B\) such that all \(S_{r,c}\) are 1 and receive Yes, Aoki must find the correct \((i, j)\) from the candidates satisfying \(i = A\) or \(j = B\) in \(N - 1\) questions, which one can verify is not always achievable. On the other hand, if we ask a good question and receive Yes, we can determine the answer as follows:

  1. Select one pair \((r, c)\) satisfying \(r = A\) or \(c = B\) and \(S_{r,c} = \) 1.
  2. Let \(R = \{1, 2, \ldots, N\} \setminus \{A\}\) and \(C = \{1, 2, \ldots, N\} \setminus \{B\}\).
  3. Repeat the following \(N - 2\) times: choose \(x \in R\), \(y \in C\) such that \(x \neq r\), \(y \neq c\), ask about \((x, y)\), and remove \(x\) and \(y\) from \(R\) and \(C\), respectively.
  4. At this point, there are at most two candidates for \((i, j)\), so we can determine the answer with one more question.

If the answer to the good question is No, we need to determine the correct \((i, j)\) from the \((N - 1)^2\) candidates excluding those satisfying \(i = A\) or \(j = B\), using \(N - 1\) questions. Let \(R\) and \(C\) be the set of unused \(i'\) and \(j'\). When using \((x,y)\) such that \(x \in R, y \in C\) for a question, we need to make a good choice similar to before such that (\(r = x\) and \(c \in C\)) or (\(r \in R\) and \(c = y\)).

If it is possible to perform \(N - 2\) good questions without duplicates of \(i',j'\), the answer is Yes. If all \(N - 2\) answers to the questions are No, we can determine the answer by considering the remaining candidates \(i_1,i_2,j_1,j_2\) and asking about \((i',j')=(i_1,A), (B, j_1)\), where \(A\) and \(B\) are already used \(i', j'\).

Conversely, we can prove that if we cannot perform \(N - 2\) good questions, the answer is No. Consider the sequence of initial \(N - 1\) questions when all answers are No. Let \(R, C\) be the sets of \(i', j'\) unused in the \(N-1\) questions. Then, we can assume \(S_{x,y} = \) 1 for any \(x \in R, y \in C\). This is because if there exist \(x, y\) satisfying \(S_{x,y} = \) 0, we can replace the first non-good question in the \(N-1\) question with a good question involving \(x, y\) without loss. Also, since we can perform at most \(N - 3\) good questions, we have \(|R| + |C| \geq 4\) and \(S_{x,y}=\)1 for any \(x \in R, y \in C\), in which case it is impossible to determine \(i,j\) with one question, so the answer is No.

To determine whether we can make \(N - 2\) good choices, we consider an undirected graph with \(2N\) vertices where for each \(1 \leq i, j \leq N\), there is an edge between \(i\) and \(N + j\) if and only if \(S_{i,j} = \) 0, that is, a bipartite graph where each vertex corresponds to an \(i'\) or \(j'\). If there exists an acyclic subgraph with \(N - 2\) edges in this graph, we can perform \(N - 2\) good operations by continuously choosing \((i', j')\) corresponding to a leaf vertex and another appropriate remaining vertex. If a cycle always forms no matter how we choose \(N - 2\) edges, attempting to perform operations on that cycle would result in removing two or more edges at once, meaning we cannot perform \(N - 2\) operations.

Since we can greedily determine the maximum number of edges in an acyclic subgraph of the graph, we can solve this problem in \(\mathrm{O}(N^2)\) time.

posted:
last update: