Under construction

Still being built. Things may change

Ancient Greek Isomorphism

Time limit5sMemory limit1024 MB

Summary
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 nn rows and mm columns, with n⋅mn \cdot m vertices and n⋅(m−1)+(n−1)⋅mn \cdot (m - 1) + (n - 1) \cdot m edges. Each grid point is joined to its adjacent grid points.

(i,j)(i, j) denotes the vertex at the intersection of row ii and column jj. Formally, there is an edge between (i,j)(i, j) and (i+1,j)(i+1, j) for all (1≤i<n,1≤j≤m)(1 \leq i < n, 1 \leq j \leq m), and an edge between (i,j)(i, j) and (i,j+1)(i, j+1) for all (1≤i≤n,1≤j<m)(1 \leq i \leq n, 1 \leq j < m).

(The picture shows a 6×56 \times 5 square grid graph.)

Surprisingly, Poseidon's favorite graph is also undirected, also has n⋅mn \cdot m vertices and n⋅(m−1)+(n−1)⋅mn \cdot (m - 1) + (n - 1) \cdot m edges, and has no self-loops or multiple edges. Its vertices are numbered with integers from 11 to n⋅mn \cdot m.

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 n,mn, m, the dimensions of the grid (2≤n,m,n⋅m≤2⋅1052 \leq n, m, n \cdot m \leq 2 \cdot 10^{5}).

Each of the next n⋅(m−1)+(n−1)⋅mn \cdot (m - 1) + (n - 1) \cdot m lines describes an edge of Poseidon's graph with two integers ui,viu_i, v_i (1≤ui,vi≤n⋅m1 \leq u_i, v_i \leq n \cdot m, ui≠viu_i \neq v_i). 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.

Examples2

  1. Example 1

    Input
    2 2
    1 2
    2 3
    3 4
    4 2
    
    Expected output
    No
    
  2. Example 2

    Input
    4 3
    2 9
    2 7
    5 9
    5 6
    7 10
    1 7
    1 6
    1 9
    1 12
    6 11
    10 12
    11 12
    8 10
    4 8
    4 12
    3 4
    3 11
    
    Expected output
    Yes