A conflict graph has n people (numbered 0 to n-1) and m undirected rivalry edges. You want to split people into two rooms so that no rivalry is inside a single room. A connected component can be split this way exactly when it is bipartite (2-colorable). A component fails when it contains a cycle of odd length.
Count how many connected components are not bipartite.
Line 1: two integers n and m.
Next m lines: two integers u v (0-indexed) — a rivalry between u and v.
The graph is undirected. A self-loop (u == v) makes its component non-bipartite. Duplicate edges may appear.
A single integer: the number of connected components that are not bipartite.
An isolated vertex forms a bipartite component (it contributes 0 to the answer).
One odd cycle
Input
5 5 0 1 1 2 2 0 3 4 3 4
Expected
1
Explanation
Component {0,1,2} is a triangle (odd cycle) so it is not bipartite. Component {3,4} is a single edge, which is bipartite. So exactly 1 component is non-bipartite.
All bipartite
Input
4 3 0 1 1 2 2 3
Expected
0
Explanation
The whole graph is one path 0-1-2-3, which is bipartite (2-colorable), so 0 components are non-bipartite.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →