#P9869. [NOIP2023] 三值逻辑
[NOIP2023] 三值逻辑
Problem Description
Little L is learning Kleene three-valued logic today.
In three-valued logic, the value of a variable can be: true (, abbreviated as ), false (, abbreviated as ), or unknown (, abbreviated as ).
Logical operations can also be defined on three-valued logic. Since Little L is progressing very slowly, he has only learned the logical NOT operation , whose rules are:
$$\lnot \mathit{T} = \mathit{F}, \lnot \mathit{F} = \mathit{T}, \lnot\mathit{U} = \mathit{U}.$$Now Little L has three-valued logic variables . Little L wants to do something interesting, so he wrote down statements. There are three types of statements, where denotes assignment:
- , where is one of ;
- ;
- .
At the beginning, Little L will assign initial values to these variables, and then run the statements in order.
Little L hopes that after executing all statements, the final values of all variables are the same as their initial values. Under this condition, Little L wants the number of variables whose initial values are to be as small as possible.
In this problem, you need to help Little L find an initial assignment scheme with the minimum number of variables, such that after executing all statements, the final values of all variables are equal to their initial values. Little L guarantees that, at least for all test cases in this problem, such an initial assignment scheme must exist.
Input Format
This problem contains multiple groups of testdata.
The first line of input contains two integers and , representing the test point number and the number of testdata groups, respectively. For the samples, indicates that the sample has the same constraints as test point .
Then, for each group of testdata:
- The first line contains two integers and , representing the number of variables and the number of statements, respectively.
- The next lines give each statement in execution order.
- The first character describes the type of the statement. It is guaranteed that is one of
TFU+-. - If is one of
TFU, then an integer follows, meaning the statement is . - If is
+, then two integers follow, meaning the statement is . - If is
-, then two integers follow, meaning the statement is .
- The first character describes the type of the statement. It is guaranteed that is one of
Output Format
For each group of testdata, output one line with one integer, representing the minimum number of variables among all initial assignment schemes that satisfy the condition.
1 3
3 3
- 2 1
- 3 2
+ 1 3
3 3
- 2 1
- 3 2
- 1 3
2 2
T 2
U 2
0
3
1
Hint
[Sample Explanation #1]
In the first group of testdata, the statements in order are:
- ;
- ;
- .
A valid initial assignment scheme is $x_1 = \mathit{T}, x_2 = \mathit{F}, x_3 = \mathit{T}$, with a total of variables. Since no initial assignment can have fewer than variables, the output is .
In the second group of testdata, the statements in order are:
- ;
- ;
- .
The only initial assignment scheme is , with a total of variables, so the output is .
In the third group of testdata, the statements in order are:
- ;
- ;
An initial assignment scheme that minimizes the number of variables is . The scheme is also valid, but it does not minimize the number of variables.
[Sample Explanation #2]
This group of samples satisfies the conditions of test point .
[Sample Explanation #3]
This group of samples satisfies the conditions of test point .
[Sample Explanation #4]
This group of samples satisfies the conditions of test point .
[Constraints]
For all testdata, it is guaranteed that:
- , ;
- For each operation, is a character in
TFU+-, and .
| Test Point Number | Possible values of | |
|---|---|---|
Translated by ChatGPT 5