Classroom Seating Conflict - Problem
In a classroom of n students numbered 0 to n - 1, some pairs of students have conflicts and cannot sit in the same group.
You are given a 2D array conflicts where conflicts[i] = [u, v] means students u and v must be in different groups.
Return true if all students can be divided into exactly two groups such that no two conflicting students are in the same group, or false otherwise.
Input & Output
Example 1 — Possible Split
$
Input:
n = 4, conflicts = [[0,1],[1,2],[2,3]]
›
Output:
true
💡 Note:
Group A: {0,2}, Group B: {1,3}. No conflicts within groups.
Example 2 — Odd Cycle
$
Input:
n = 3, conflicts = [[0,1],[1,2],[0,2]]
›
Output:
false
💡 Note:
Triangle: all three conflict with each other. Cannot split into 2 groups.
Example 3 — No Conflicts
$
Input:
n = 1, conflicts = []
›
Output:
true
💡 Note:
Single student, trivially split.
Constraints
- 1 ≤ n ≤ 2 × 10^5
- 0 ≤ conflicts.length ≤ 2 × 10^5
- 0 ≤ u, v < n
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code