Ctrl K

Two Teams Without Feuds

Medium
A summer camp has people campers numbered from 1 to people. The counselors want to split everyone into two teams; the teams can be of any size, and one may even be empty. Some campers have fallen out: a pair [a, b] in conflicts means a and b must not be on the same team. Return True if everyone can be split into two teams so that no feuding pair ends up together, and False otherwise.
Example 1
people=5, conflicts=[[1, 2], [2, 3], [4, 5]] → True

For example, teams {1, 3, 4} and {2, 5}: no feuding pair ends up together.

Example 2
people=4, conflicts=[[1, 2], [2, 3], [1, 3]] → False

Players 1, 2 and 3 are all at odds with each other, and three people cannot be split into two teams without a clash.

Example 3
people=3, conflicts=[] → True
Constraints
  • 1 ≤ people ≤ 2000
  • 0 ≤ len(conflicts) ≤ 10⁴
  • conflicts[i] = [a, b], 1 ≤ a, b ≤ people, a ≠ b
  • Each pair of campers appears at most once
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDo the Links Form a Tree MediumIsolated Groups in a Network MediumRatio Chain Queries MediumClusters of Linked Servers MediumThe Extra Cable Medium
loading Python…
Input
people=5, conflicts=[[1, 2], [2, 3], [4, 5]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.