A wedding planner has two long tables and a list of guest pairs who must not sit together. Can every guest be seated so that no such pair shares a table? In graph terms: can the nodes be coloured with two colours so that every edge joins different colours (the graph is bipartite)? When the answer is no, the planner wants proof: a loop of rivals with an odd number of people.
Nodes are 0 to n - 1, and edges is an array of undirected [a, b] pairs. Write two functions:
twoColour(n, edges) returns an array colour of length n with colour[v] equal to 0 or 1, such that every edge joins two different colours. Return null when no such colouring exists.
findOddCycle(n, edges) returns null when the graph is bipartite. Otherwise it returns one cycle of odd length as an array of distinct nodes [c0, c1, ..., ck], where each consecutive pair and the closing pair [ck, c0] are edges. Any odd cycle is accepted.
Rules:
Within each connected group, the smallest node gets colour 0. That makes the colouring unique, since every other colour in the group is then forced. Isolated nodes get 0.
Parallel edges are allowed. A self-loop [v, v] makes the graph non-bipartite, and [v] is its odd cycle.
n = 0 returns [] from twoColour and null from findOddCycle.
Throw a TypeError when n is not a non-negative integer, edges is not an array, or an edge is not a pair of integers from 0 to n - 1.
Graphs can have 200,000 nodes and 300,000 edges, including one long path. Aim for O(n + edges) and avoid recursion.