Under construction

Still being built. Things may change

Isomorphic?

Time limit2sMemory limit1024 MB

Summary
Given two connected graphs, each with N vertices and N edges, decide whether they are isomorphic.
Level

Medium6 of 10

Topics
Graph, Tree, String matching, Hash map
Solved
No attempts yet

Problem

You are given two undirected simple connected graphs GG and G′G'. Each graph has NN vertices numbered from 11 to NN and NN edges.

Determine whether the two graphs are isomorphic. That is, decide whether a permutation (p1,p2,…,pN)(p_1, p_2, \dots, p_N) of (1,2,…,N)(1, 2, \dots, N) exists such that for every pair of vertices uu and vv, GG has an edge between uu and vv if and only if G′G' has an edge between pup_u and pvp_v.

Input

The first line contains one integer NN (3≤N≤2×1053 \le N \le 2 \times 10^5).

Each of the next NN lines contains two integers aia_i and bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N), meaning that GG has an undirected edge between vertices aia_i and bib_i.

Each of the following NN lines contains two integers cic_i and did_i (1≤ci,di≤N1 \le c_i, d_i \le N), meaning that G′G' has an undirected edge between vertices cic_i and did_i.

Both graphs are connected, and neither has self-loops or double edges.

N
a_1 b_1
...
a_N b_N
c_1 d_1
...
c_N d_N

Output

Print Yes if GG and G′G' are isomorphic, and No otherwise.

Examples3

  1. Example 1

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

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

    Input
    6
    1 2
    1 3
    2 5
    2 6
    3 5
    4 6
    1 5
    1 6
    2 4
    2 5
    2 6
    3 4
    
    Expected output
    Yes