Intuitionistic Logic
Time limit2sMemory limit128 MB
Given a DAG and its antichain algebra, test each formula over all variable assignments and report valid or invalid.
- Level
Hard8 of 10
- Topics
- Brute force, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
Vasya recently ran into a movement in mathematics and logic called "intuitionism". Its central idea is the rejection of the law of the excluded middle, the logical law saying that any assertion is either true or false. Vasya liked the idea. He says: "Classical mathematics tells me that Fermat's Last Theorem is either true or false, but that statement is useless to me until I see a proof or a counterexample." So Vasya became an intuitionist. He tries to use intuitionistic logic in everything he does, above all in his scientific work. This logic is much harder than the classical one, and Vasya often writes formulas that are valid in classical logic but not in the intuitionistic one.
He now wants a program that checks his formulas automatically. He found a book that explains how to do it, but he is not good at programming, so you have to help him.
The construction starts from an arbitrary acyclic oriented graph , where is the set of vertices. First a partial order on is defined: holds if and only if contains a path (possibly of zero length) from to . Next, let be the set of all subsets of , and let consist of every in which any two different elements and are incomparable, meaning that neither nor holds. Note that always contains the empty set and every one-element subset of .
Now it is possible to define a map . For we put , the set of all maximal elements of .
Several operations on follow. For we put
A logical formula consists of the following symbols.
- Constants and .
- Variables, the capital letters from to .
- Parentheses. If is a formula, then is another one.
- Negation. If is a formula, then is a formula.
- Conjunction, . The conjunction is evaluated from left to right, so .
- Disjunction, . The same remark applies.
- Implication, . Unlike the previous two operations it is evaluated from right to left, so means .
- Equivalence, . This expression equals .
The operations are listed from the highest priority to the lowest.
A formula is called valid in the model defined by if it evaluates to after every substitution of elements of for the variables involved in . Otherwise it is called invalid.
You are given the graph and a set of formulas. Determine which of them are valid.
Input
The input contains one or more test cases and ends at end of file.
The first line of each test case contains two integers and separated by a single space, the number of vertices () and the number of edges () of . Each of the next lines contains two integers and , the beginning and the end of the -th edge. The next line contains (), the number of formulas to be processed, and each of the following lines contains one formula.
A formula is a string of the tokens 0, 1, A ... Z, (, ), ~, &, |, =>, =. The last five tokens stand for , , , and respectively. Tokens can be separated by an arbitrary number of spaces. No line is longer than characters, and every formula in the input is syntactically correct. You may also assume that the number of elements is at most and that , where is the number of different variables used in the -th formula.
Output
For each test case print lines, one line for each formula. Write valid on the -th line if the -th formula is valid, and invalid otherwise.