Chess Club Color-Flip Returns

Earliest insertion allowing an odd closed walk from each vertex

HardGraphsUnion FindBipartite Graphs

A chess club has players numbered from 0 to n - 1. The club gradually opens practice links between pairs of players. Each link can be traveled in either direction and remains open permanently.

The links in links open in order: links[0] opens at step 1, links[1] at step 2, and so on. Repeated links between the same pair are allowed.

A player can carry a two-sided marker along any walk through the open links. Traversing one link flips the marker between its black and white sides. Players and links may be visited more than once during a walk.

For each player, find the earliest step at which a walk can start at that player, return to the same player, and leave the marker showing the opposite side from its starting side. Equivalently, the closed walk must traverse an odd number of links.

Return an array of length n in player-number order. Its entry for a player is that earliest step, or -1 if no such walk is possible even after every link has opened.

Examples

Example 1

Input: n = 6, links = [[0,1],[1,2],[2,0],[3,4],[2,3],[4,5]]
Output: [3,3,3,5,5,6]

The first three links form a triangle among players 0, 1, and 2. Player 3 and then player 4 gain access to this triangle when the fifth link opens. Player 5 gains access when the sixth link opens. A player outside the triangle can walk to it, traverse it, and retrace the route home.

Example 2

Input: n = 4, links = [[0,1],[1,2],[2,3],[3,0]]
Output: [-1,-1,-1,-1]

The links form a four-player cycle. Every closed walk in this graph uses an even number of links, so no player can return with the marker flipped.

Example 3

Input: n = 1, links = []
Output: [-1]

The only player has no open links, so an odd-length closed walk is impossible.

Constraints

  • 1 <= n <= 100000
  • 0 <= links.length <= 100000
  • Each links[i] contains exactly two integers u and v.
  • 0 <= u, v < n and u != v
  • The same unordered pair may occur multiple times.

The intended solution takes O((n + links.length) * alpha(n)) time and O(n) auxiliary space. A component's unresolved vertices can be concatenated in constant time with linked lists, and each vertex receives an answer only once.

Hints

Show hint 1

A vertex admits an odd-length closed walk exactly when its connected component is not bipartite. Can you detect the first violation of bipartiteness while inserting links?

Show hint 2

Use union-find with parity to a component representative. Keep a linked list of vertices whose answers are still unknown, and resolve that entire list when its component becomes non-bipartite.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you answer online queries asking whether two players are connected by both an even-length walk and an odd-length walk?
  • How would you adapt the method if each link explicitly specified whether traversing it flips the marker?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem