Bipartite Check
You are given an undirected graph with n vertices and m edges. The graph may be disconnected.
Determine whether every connected component can be colored using two colors so that every edge joins two vertices of different colors.
Input
The first line contains two integers n and m.
The next m lines each contain two integers a and b, meaning there is an undirected edge between vertices a and b.
Vertices are numbered from 1 to n.
Output
Print YES if the graph is bipartite. Otherwise, print NO.
Constraints
- 1≤n≤200000
- 0≤m≤300000
- 1≤a,b≤n
Example 1
4 4
1 2
2 3
3 4
4 1
YES
Explanation
The graph is a cycle of even length, so alternating the two colors around the cycle works.
Example 2
3 3
1 2
2 3
3 1
NO
Explanation
The graph is a triangle, so two adjacent vertices must eventually receive the same color.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.