#P5260. Is the Graph a Hypercube?
Is the Graph a Hypercube?
Is the Graph a Hypercube?
A D-dimensional hypercube has \(2^D\) vertices. It is known that there exists a labeling of its vertices with numbers from \(0\) to \(2^D-1\) such that the binary representations of any two adjacent vertices differ in exactly one bit. For example, for a 2-dimensional hypercube (a square), if we arrange the vertices in the first quadrant in counterclockwise order as \((0,0)\), \((1,0)\), \((1,1)\), and \((0,1)\) and interpret these coordinates as 2-bit binary numbers, a valid labeling is \(0,1,3,2\). Similarly, for a 3-dimensional hypercube (a cube) and higher dimensions, a proper labeling exists.
Given an undirected graph with \(N\) vertices (numbered from \(0\) to \(N-1\)) and \(M\) edges, determine whether the graph is isomorphic to a hypercube graph. In other words, check if there exists a dimension \(D\) such that \(N = 2^D\) and the graph (after a relabeling of vertices) is exactly the \(D\)-dimensional hypercube where every edge connects vertices whose binary labels differ in exactly one bit.
inputFormat
The first line contains two integers \(N\) and \(M\) \((1 \le N \le 2^{20})\), where \(N\) is the number of vertices and \(M\) is the number of edges. Each of the following \(M\) lines contains two integers \(u\) and \(v\) \((0 \le u, v < N)\), representing an undirected edge between vertices \(u\) and \(v\). There are no self-loops or multiple edges.
outputFormat
Print a single line containing Yes if the graph is isomorphic to a hypercube graph, or No otherwise.
sample
1 0
Yes