Under construction

Still being built. Things may change

Dimensional Analysis

Time limit4sMemory limit1024 MB

Summary
Given equations built only from multiplication and division of physical quantities, decide whether any quantity can be proved dimensionless (equal to 1).
Level

Medium7 of 10

Topics
Math, Graph, Union-find, Number theory
Solved
No attempts yet

Problem

You are deriving some equations for physics homework and are worried you have made some mistakes. To debug them, you will apply dimensional analysis. In dimensional analysis, you remove all magnitudes and units from a set of equations until you are left only with strings representing physical quantities and the special dimensionless constant 11. The following is an example set of such equations:

velocity * time = length
frequency = 1 / time
acceleration = velocity / time
force = mass * acceleration
force = mass * length / time / time

Your equations, like the ones above, involve only multiplication and division (no addition, exponentiation, etc.). As you can see, some quantities (like velocity) are defined in terms of other, more basic quantities (length, time). But you are not sure what the correct relationships are between the quantities. You do know that none of the quantities in your equations are unitless: it should not be possible, through any set of algebraic manipulations, to prove that any quantity is equal to the dimensionless constant 11. (You may assume that all physical quantities in your equations have positive real magnitudes. In particular, you may freely divide any equation by any quantity without worrying about division by zero, and you may take square or higher roots of any quantity.)

A set of equations violating this condition is invalid (and a set of equations is valid otherwise). The above example is a valid system. Here is an invalid one:

foo * bar = xyzzy
foo = xyzzy * bar

By substituting the second equation into the first, and dividing both sides by xyzzy, you get bar * bar = 1. Taking a square root of both sides, you get bar = 1, so bar is dimensionless.

Given the set of equations in your dimensional analysis, compute whether the equations are valid.

Input

The first line of the input is a single integer NN, the number of equations (1≤N≤1001 ≤ N ≤ 100). Each of the NN subsequent lines of input contains one equation. An equation consists of two expressions, separated by an equal sign surrounded by spaces “ = ”. Each expression contains one or more atoms, separated by either “ * ” or “ / ”; each atom is either the character ‘1’ or a physical quantity, represented by a lowercase string (containing one or more ASCII characters between ‘a’ and ‘z’). At most 100100 unique physical quantities appear in total across all equations, each equation contains at most 100100 atoms, and the total number of characters in all atoms across all equations will not exceed 100 000100\,000.

There will be exactly one space before and after each ‘=’, ‘*’, and ‘/’ and the equations will contain no other whitespace or other extraneous punctuation. See the sample input for examples of how equations are formatted.

The operator * represents multiplication and / represents division. Expressions follow the usual associativity rules: a / b * c is the same as a * c / b but different from a / b / c.

Output

Print invalid if it is possible to prove that at least one of the physical quantities in an equation in the input must be dimensionless. Print valid otherwise.

Examples3

  1. Example 1

    Input
    5
    velocity * time = length
    frequency = 1 / time
    acceleration = velocity / time
    force = mass * acceleration
    force = mass * length / time / time
    
    Expected output
    valid
    
  2. Example 2

    Input
    2
    foo * bar = xyzzy
    foo = xyzzy * bar
    
    Expected output
    invalid
    
  3. Example 3

    Input
    5
    time * power = energy
    energy = work
    work = force * distance
    distance = distance
    1 / 1 = 1 * 1 / 1
    
    Expected output
    valid