Imagine you need to organize a networking event where people can only talk to others from a different group. You're given a social network graph where each person is connected to their acquaintances.
Your task is to determine if you can divide all attendees into exactly two groups such that:
- Every person only knows people from the opposite group
- No one knows anyone from their same group
You're given an undirected graph with n nodes (people), where graph[u] contains all the people that person u knows. The graph has no self-connections and may have multiple disconnected components (separate friend circles).
Goal: Return true if the social network can be perfectly divided into two groups with the networking constraint, false otherwise.
Input & Output
Visualization
Time & Space Complexity
Visit each vertex once and each edge once during DFS traversal
Color array storage plus recursion stack depth
Constraints
- graph.length == n
- 1 โค n โค 100
- 0 โค graph[u].length < n
- 0 โค graph[u][i] โค n - 1
- graph[u] does not contain u
- All values of graph[u] are unique
- If graph[u] contains v, then graph[v] contains u