Bipartite graph coloring
Given an undirected graph on n nodes (edges [u, v]), determine if it is bipartite (2-colorable). If it is, return a two-element list [size_color_A, size_color_B] where, summed over connected components, color A gets the larger side of each component and color B the smaller (ties go to A). If the graph is NOT bipartite, return the single-element list [-1]. Isolated nodes form their own component of size 1.
Implement
bipartite_coloring(n: int, edges: list[list[int]]) → list[int]Examples
in
[4,[[0,1],[1,2],[2,3],[3,0]]]out[2,2]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 25 min
solution.py
InputExpectedGot
[4,[[0,1],[1,2],[2,3],[3,0]]][2,2]not run yetsample