This is bipartite checking, which I recognized pretty fast, but I fumbled the disconnected components part initially.
Model the problem as checking if the graph is bipartite, since two groups with no internal edges correspond to a 2-coloring. Use BFS or DFS to color each connected component, ensuring no edge connects same-colored vertices. Return false if any conflict arises; otherwise true.
Pro tip: Emphasize that the graph may be disconnected, so you must iterate over all unvisited nodes to cover every component. Also, clarify that the adjacency list is undirected, so each edge appears twice, and handle it accordingly.
Confirm that the graph is undirected, guests are vertices, and 'know each other' means an edge. The goal is to partition vertices into two independent sets.
Use BFS or DFS to attempt a 2-coloring of the graph. Initialize a color array with -1 (uncolored) and assign alternating colors to neighbors.
Loop through all vertices; if a vertex is uncolored, start a new BFS/DFS from it, assigning it color 0. This ensures every component is checked.
During traversal, if a neighbor already has the same color as the current vertex, return false immediately. Otherwise, continue until all vertices are processed.
Time complexity is O(V + E) since each vertex and edge is visited once. Space complexity is O(V) for the color array and queue/stack, plus O(V + E) for the adjacency list if counted.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.