시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 1024 MB333100.000%

문제

You are deriving some equations for physics homework and are worried you’ve 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 $1$. 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’re 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 $1$. (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 and so bar is dimensionless.

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

입력

The first line of the input is a single integer $N$, the number of equations ($1 ≤ N ≤ 100$). Each of the $N$ 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 $100$ unique physical quantities appear in total across all equations, each equation contains at most $100$ atoms, and the total number of characters in all atoms across all equations will not exceed $100\,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.

출력

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.

예제 입력 1

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

예제 출력 1

valid

예제 입력 2

2
foo * bar = xyzzy
foo = xyzzy * bar

예제 출력 2

invalid

예제 입력 3

5
time * power = energy
energy = work
work = force * distance
distance = distance
1 / 1 = 1 * 1 / 1

예제 출력 3

valid

출처

ICPC > Regionals > North America > North America Qualification Contest > ICPC North America Qualifier 2021 D번

  • 문제를 만든 사람: Etienne Vouga