Official

B - Balanced Neighbors 2 Editorial by evima


When \(1<N<6\), there does not exist a graph satisfying the condition.
When \(N=6\), we can satisfy the condition by creating a cycle of length \(6\) such that the distance between \(i\) and \(N+1-i\) is \(3\).

Considering this example, we see that when \(N\) is even, it is convenient if we can construct a graph where the distance between \(i\) and \(N+1-i\) is \(3\), and the distance between any other two points is \(1\) or \(2\).
Similarly, when \(N\) is odd, we should construct a graph where the distance between \(i\) and \(N-i\) is \(3\) and the distance between any other pair of vertices is \(1\) or \(2\).

By creating a complete bipartite graph with \(U=\{1,2,3,\ldots, \lfloor \frac{N}{2} \rfloor \}, V=\{ \lfloor \frac{N}{2} \rfloor+1,\ldots,N \}\) and removing edges corresponding to the above \(\lfloor \frac{N}{2} \rfloor\) pairs, we can construct such a graph and satisfy the problem’s condition.

The above graph can also be obtained bottom-up by repeating the following operations starting from the \(N=6\) example.
Assume the graph is colored black and white such that vertices of the same color are not adjacent.

  • On odd-numbered operations, prepare a new white vertex and connect edges to all black vertices
  • On even-numbered operations, prepare a new black vertex and connect edges to all white vertices except the white vertex created in the previous operation

posted:
last update: