#P9869. [NOIP2023] 三值逻辑

    ID: 11022 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>并查集2023NOIP 提高组O2优化二分图基环树

[NOIP2023] 三值逻辑

Problem Description

Little L is learning Kleene three-valued logic today.

In three-valued logic, the value of a variable can be: true (True\mathit{True}, abbreviated as T\mathit{T}), false (False\mathit{False}, abbreviated as F\mathit{F}), or unknown (Unknown\mathit{Unknown}, abbreviated as U\mathit{U}).

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 ¬\lnot, whose rules are:

$$\lnot \mathit{T} = \mathit{F}, \lnot \mathit{F} = \mathit{T}, \lnot\mathit{U} = \mathit{U}.$$

Now Little L has nn three-valued logic variables x1,,xnx_1,\cdots, x_n. Little L wants to do something interesting, so he wrote down mm statements. There are three types of statements, where \leftarrow denotes assignment:

  1. xivx_i \leftarrow v, where vv is one of T,F,U\mathit{T}, \mathit{F}, \mathit{U};
  2. xixjx_i \leftarrow x_j;
  3. xi¬xjx_i \leftarrow \lnot x_j.

At the beginning, Little L will assign initial values to these variables, and then run the mm 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 Unknown\mathit{Unknown} 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 Unknown\mathit{Unknown} 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 cc and tt, representing the test point number and the number of testdata groups, respectively. For the samples, cc indicates that the sample has the same constraints as test point cc.

Then, for each group of testdata:

  • The first line contains two integers nn and mm, representing the number of variables and the number of statements, respectively.
  • The next mm lines give each statement in execution order.
    • The first character vv describes the type of the statement. It is guaranteed that vv is one of TFU+-.
    • If vv is one of TFU, then an integer ii follows, meaning the statement is xivx_i \leftarrow v.
    • If vv is +, then two integers i,ji,j follow, meaning the statement is xixjx_i \leftarrow x_j.
    • If vv is -, then two integers i,ji,j follow, meaning the statement is xi¬xjx_i \leftarrow \lnot x_j.

Output Format

For each group of testdata, output one line with one integer, representing the minimum number of Unknown\mathit{Unknown} 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 mm statements in order are:

  • x2¬x1x_2 \leftarrow \lnot x_1;
  • x3¬x2x_3 \leftarrow \lnot x_2;
  • x1x3x_1 \leftarrow x_3.

A valid initial assignment scheme is $x_1 = \mathit{T}, x_2 = \mathit{F}, x_3 = \mathit{T}$, with a total of 00 Unknown\mathit{Unknown} variables. Since no initial assignment can have fewer than 00 Unknown\mathit{Unknown} variables, the output is 00.

In the second group of testdata, the mm statements in order are:

  • x2¬x1x_2 \leftarrow \lnot x_1;
  • x3¬x2x_3 \leftarrow \lnot x_2;
  • x1¬x3x_1 \leftarrow \lnot x_3.

The only initial assignment scheme is x1=x2=x3=Ux_1 = x_2 = x_3 = \mathit{U}, with a total of 33 Unknown\mathit{Unknown} variables, so the output is 33.

In the third group of testdata, the mm statements in order are:

  • x2Tx_2 \leftarrow \mathit{T};
  • x2Ux_2 \leftarrow \mathit{U};

An initial assignment scheme that minimizes the number of Unknown\mathit{Unknown} variables is x1=T,x2=Ux_1 = \mathit{T}, x_2 = \mathit{U}. The scheme x1=x2=Ux_1 = x_2 = \mathit{U} is also valid, but it does not minimize the number of Unknown\mathit{Unknown} variables.

[Sample Explanation #2]

This group of samples satisfies the conditions of test point 22.

[Sample Explanation #3]

This group of samples satisfies the conditions of test point 55.

[Sample Explanation #4]

This group of samples satisfies the conditions of test point 88.

[Constraints]

For all testdata, it is guaranteed that:

  • 1t61 \le t \le 6, 1n,m1051 \le n,m \le 10 ^ 5;
  • For each operation, vv is a character in TFU+-, and 1i,jn1 \le i,j \le n.
Test Point Number n,mn,m\leq Possible values of vv
1,21,2 1010 TFU+\mathit{TFU+-}
33 10310^3 TFU\mathit{TFU}
44 10510^5
55 10310^3 U+\mathit{U+}
66 10510^5
77 10310^3 +\mathit{+-}
88 10510^5
99 10310^3 TFU+\mathit{TFU+-}
1010 10510^5

Translated by ChatGPT 5