Possible Bipartition
Given n people and pairs who dislike each other, decide if they can be split into two groups so no two people in the same group dislike each other.
Open on LeetCode ↗Intuition
The trap is assuming everyone forms one connected group and only coloring starting from person 1. The dislikes graph can split into several disconnected pieces, so a fresh BFS or DFS must start from every uncolored person, not just the first. Within each piece, color a person, force every disliked neighbor to the opposite team, and repeat -- exactly the 2-coloring test for bipartiteness. A conflict is found the moment a neighbor is already colored the SAME as the current person, which is impossible to fix and immediately rules out any valid split.
Approach
Build the dislikes graph
For every pair [a, b], add an undirected edge -- each dislikes the other, so the adjacency list gets both directions.
Color every disconnected group
Loop over every person; if uncolored, that person starts a brand new group (the graph may not be connected). BFS/DFS from there, coloring team A/B and alternating for each newly reached neighbor.
Detect the same-color conflict
When a BFS step reaches a neighbor that is already colored, check its color: if it matches the current person's color, two people who dislike each other ended up on the same team -- return false immediately.
Solution & live demo
Edge cases
Outer loop restarts coloring at every uncolored person, not just person 1.
Trivially colored alone; never causes a conflict.
Coloring wraps around and forces a same-color conflict -- correctly reported impossible.
Every person its own group -- always possible.