Ancient Greek Isomorphism
Time limit5sMemory limit1024 MB
Given a graph with the same vertex and edge counts as an n by m grid, decide whether it is isomorphic to the grid graph.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Implementation
- Solved
- No attempts yet
Statement
Zeus's favorite graph is the square grid graph. It is an undirected graph shaped as a grid with rows and columns, with vertices and edges. Each grid point is joined to its adjacent grid points.
denotes the vertex at the intersection of row and column . Formally, there is an edge between and for all , and an edge between and for all .

(The picture shows a square grid graph.)
Surprisingly, Poseidon's favorite graph is also undirected, also has vertices and edges, and has no self-loops or multiple edges. Its vertices are numbered with integers from to .
Poseidon wants to know whether the two graphs are isomorphic. Formally, can each vertex of Poseidon's graph be paired with exactly one vertex of Zeus's graph, covering every vertex of both graphs exactly once, so that two vertices are adjacent in one graph if and only if their partners are adjacent in the other? Help Poseidon decide.
Input
The first line contains two integers , the dimensions of the grid ().
Each of the next lines describes an edge of Poseidon's graph with two integers (, ). It is guaranteed that Poseidon's graph has no multiple edges.
Output
If Poseidon's graph and Zeus's graph are isomorphic, print Yes. Otherwise, print No. The answer may be in any letter case.
Hint
The graph from the second example is shown in the picture below.
