Bipartite Check
View as PDF
Submit solution
Points:
100
Time limit:
1.0s
PyPy 3
2.0s
Python 3
2.0s
Memory limit:
500M
Problem type
You are given an undirected graph with vertices and
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 and
.
The next lines each contain two integers
and
, meaning there is an undirected edge
between vertices
and
.
Vertices are numbered from to
.
Output
Print YES if the graph is bipartite. Otherwise, print NO.
Constraints
Example 1
Input
4 4
1 2
2 3
3 4
4 1
Output
YES
Explanation
The graph is a cycle of even length, so alternating the two colors around the cycle works.
Example 2
Input
3 3
1 2
2 3
3 1
Output
NO
Explanation
The graph is a triangle, so two adjacent vertices must eventually receive the same color.
Comments