Problem 593767 · hard · Level 05 Advanced Algorithms & Graphs

Largest Table Where Everyone Gets Along

backtracking · branch and bound · bitmasks · graphs

A wedding planner knows which guests get along. There are n guests numbered 0 .. n-1, and each pair [a, b] in pairs means guests a and b get along (in both directions). Every other pair of guests does not.

A table is friendly when every two guests seated at it get along. Return the largest number of guests that can sit at one friendly table. A single guest is always a friendly table; with n = 0 return 0.

Examples

Input:  n = 5, pairs = [[0, 1], [1, 2], [2, 0], [2, 3], [3, 4]]
Output: 3
Explanation: guests 0, 1 and 2 all get along. No four guests do.

Input:  n = 4, pairs = []
Output: 1

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

Constraints

  • 0 <= n <= 50
  • 0 <= a, b < n and a != b for every pair; a pair may be listed more than once or in either order

Goals

  • Grow a group only with people who get along with everyone already in it
  • Abandon a branch when even taking every remaining candidate cannot beat the best group
Starting Python…