#K37152. Bipartite Graph Partition
Bipartite Graph Partition
Bipartite Graph Partition
You are given an undirected graph with n nodes and m edges. Your task is to determine whether the graph can be partitioned into exactly two non-empty subsets such that there are no edges connecting nodes within the same subset.
This is equivalent to checking if the graph is bipartite. A graph is bipartite if and only if it is possible to assign one of two colors to each vertex so that no two adjacent vertices share the same color.
Note: The graph may be disconnected. In such a case, every connected component of the graph must be bipartite for the whole graph to be considered bipartite.
The condition can be mathematically expressed as: Find a function c: V \rightarrow \{0,1\} such that for every edge (u,v), c(u) \neq c(v). If such an assignment exists, output YES
, otherwise output NO
.
Use LaTeX format for any formulas, for example: $c: V \rightarrow \{0,1\}$.
inputFormat
The first line contains two integers n and m --- the number of nodes and edges respectively.
The following m lines each contain two integers u and v indicating an edge between node u and node v.
All input is read from stdin.
outputFormat
Output a single line with the word YES
if the graph is bipartite, otherwise output NO
.
All output should be written to stdout.
## sample4 4
1 2
1 3
2 4
3 4
YES