Seat guests at two tables with no feud at either

Graphs Mid-level Python 3.13+ Reviewed By Best Latest Apps

Pick the fix

Guests 0, 1 and 2 all feud with each other. Why is a two-table split impossible?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A They form a cycle of odd length, so two colours cannot alternate around it

Right: around a triangle the colours would have to repeat.

Alternating two colours around a loop only works when the loop has an even number of guests.

Why not the others?
  • B There are more guests than tables. Many guests per table are allowed; the feuds are the issue.
  • C Feuds must form a tree. Even cycles, like a square, split fine.

30-second answer

Colour the graph with two colours using BFS: give a starting guest table 0, their rivals table 1, and so on. If a feud ever links two guests with the same table, it is impossible. Repeat from every guest not yet seated, since the graph may have several parts. O(V + E) time and space.

Practise saying it · 45 s. No recording, just a timer

Explain the two-colouring check.

Say your answer out loud, then check which of these points you covered:

  • BFS with two colours
  • Conflict means impossible
  • Every component

Deep dive

A graph splits this way exactly when it has no cycle of odd length.

Union-find works too: union each guest's rivals together and check no guest shares a set with a rival.

Isolated guests can sit anywhere.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
from collections import deque


def two_tables(n: int, feuds: list[list[int]]) -> bool:
    rivals: list[list[int]] = [[] for _ in range(n)]
    # a feud goes both ways
    for u, v in feuds:
        rivals[u].append(v)
        rivals[v].append(u)
    table = [-1] * n
    for first in range(n):
        if table[first] >= 0:
            continue
        table[first] = 0
        queue = deque([first])
        while queue:
            g = queue.popleft()
            for r in rivals[g]:
                if table[r] < 0:
                    table[r] = 1 - table[g]
                    queue.append(r)
                elif table[r] == table[g]:
                    return False
    return True

Common mistakes

  • Only starting from guest 0 and missing other parts of the graph.
  • Treating feuds as one-way.

Senior take & follow-ups

Mention the odd-cycle fact; it shows you understand why the BFS check works.

Follow-up questions

Think of your answer first, then open the hint.

  1. How would you return the two tables?

    Show hint

    Return the colour list from the BFS.

  2. What about three tables?

    Show hint

    Three-colouring is NP-complete in general; small cases need backtracking.

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.