Under construction

Still being built. Things may change

Making a Necklace

Time limit1sMemory limit1024 MB

Summary
Given two graphs that each form a cycle with trees hanging off it, decide whether they are isomorphic and print the verdict.
Level

Medium6 of 10

Topics
Graph, Tree, String matching
Solved
No attempts yet

Problem

Changhyun was bothered by the many couples at school, so he decided to confess to Minjun, whom he had liked for a while.

Minjun likes material gifts, so Changhyun decided to make a necklace and give it to her as a present when he confesses. Changhyun has a necklace made of NN threads and NN beads. Number the beads from 1 to NN in some order. For every pair of integers ii, jj with 1≤i,j≤N1 \le i,j \le N and i≡j±1(modN)i \equiv j \pm 1 \pmod{N}, bead ii and bead jj are guaranteed to be connected by one thread. Minjun likes flashy gifts, so Changhyun decided to decorate the necklace with MM threads and MM beads.

First, Changhyun connects fewer than 30 beads with threads to make at most NN ornaments. An ornament has one more bead than it has threads. After making an ornament, Changhyun connects one bead of the ornament and one bead of the original necklace with a single thread. He repeats this until every ornament is attached to the original necklace. He does not attach two or more ornaments to the same bead, and he does not attach an ornament to a bead that was not part of the necklace before that ornament was attached.

Input

The day of the confession finally came, and Changhyun brought the necklace to Minjun. But he then realized a big problem: Minjun was wearing a necklace identical to the one he had made. Changhyun immediately decided to make a new necklace.

Changhyun wants to check whether the new necklace is the same as the old one. Given two necklaces, write a program that determines whether they are the same.

Two decorated necklaces AA and BB are the same if the following holds. Number the beads of AA and BB appropriately. For any two numbers 1≤x,y≤N+M1 \le x, y \le N+M with x≠yx \neq y, bead xx and bead yy are connected by one thread in AA if and only if bead xx and bead yy are connected by one thread in BB.

Output

The first line contains NN and MM, separated by a space.

From the second line onward, the next N+MN+M lines each contain uiu_i and viv_i, in that order. This means that bead uiu_i and bead viv_i are connected by a thread in the old necklace.

From line N+M+2N+M+2 onward, the next N+MN+M lines each contain xjx_j and yjy_j, in that order. This means that bead xjx_j and bead yjy_j are connected by a thread in the new necklace.

Constraints

If the two necklaces are different, print Go to Minjun. If they are the same, print Retry.

Hint

3≤N≤1053 \le N \le 10^5

0≤M≤min⁡(105,29N)0 \le M \le \min(10^5, 29N)

1≤ui,vi,xj,yj≤N+M1 \le u_i, v_i, x_j, y_j \le N+M

The time limit is 1000 ms and the memory limit is 1024 MB.

Examples2

  1. Example 1

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

    Input
    3 3
    1 2
    2 3
    3 1
    1 4
    4 5
    5 6
    1 2
    2 3
    3 1
    1 4
    4 5
    2 6
    
    Expected output
    Go to Minjun