This page is still under construction.

Parts of this page are still being built. What you see may change.

Mobile

Time limit1sMemory limit128 MB

Summary
Decide whether two mobiles, given as rooted binary structures with negated weight labels, can be rotated to look identical.
Level

Medium7 of 10

Topics
Tree, DFS, Hash map, Sorting
Solved
No attempts yet

Problem

Fred is a baby. Above Fred's crib hangs a mobile, and Fred is amused by it. Fred has a twin sister, Mary, and above Mary's crib hangs another mobile. Fred wonders whether his mobile and Mary's mobile are the same. Help him.

A mobile is a collection of bars, strings, and decorative weights suspended from the ceiling. Each bar is hung by a string tied to the exact centre of the bar. From each end of a bar hangs a string that is tied either to another bar or to a weight. Bars rotate freely about their centres. Fred cannot tell two bars apart, even if they have different lengths, and he cannot tell two strings apart either. He therefore considers two mobiles to be the same if the bars of one can be rotated so that the two mobiles look identical.

Fred describes a mobile as follows. He gives each bar a distinct positive integer from 1 up to the number of bars, where bar 1 is always the bar suspended directly from the ceiling. He gives each decorative object a negative integer (for example, a biplane might be −2, a crescent moon −57, and a star −21). Fred can only count down to −9999, so no object has a number smaller than −9999. Two weights are considered identical exactly when they share the same number.

Input

The input contains two mobile descriptions. For each mobile, the first line contains a single integer n (1 ≤ n ≤ 100000), the number of bars. Each of the next n lines contains two numbers describing the two objects that hang from the two ends of bar i. A positive number refers to another bar; a negative number refers to a decorative weight. The second mobile description follows immediately after the first.

Output

Print a single line. Print Fred and Mary have different mobiles. if the information is enough to conclude that the two mobiles are different; otherwise print Fred and Mary might have the same mobile.

Examples2

  1. Example 1

    Input
    5
    2 3
    4 5
    -1 -2
    -3 -4
    -5 -6
    5
    2 5
    -1 -2
    -3 -4
    -5 -6
    3 4
    
    Expected output
    Fred and Mary might have the same mobile.
    
  2. Example 2

    Input
    5
    2 3
    4 5
    -3 -4
    -1 -2
    -5 -6
    5
    2 5
    -1 -2
    -3 -4
    -5 -6
    3 4
    
    Expected output
    Fred and Mary have different mobiles.