F. The Moral Dilemma

Codeforces
IDCF993F
Time1000ms
Memory256MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relationship with Dita is an act of love or an act of treason. <center>![image](https://espresso.codeforces.com/03312a0f530eff5353980f3183bb930a261de657.png)</center>Hibiki prepared several binary features his decision will depend on, and built a three layer logical circuit on top of them, each layer consisting of one or more [logic gates](https://en.wikipedia.org/wiki/Logic_gate). Each gate in the circuit is either "_or_", "_and_", "_nor_" (not or) or "_nand_" (not and). Each gate in the first layer is connected to exactly two features. Each gate in the second layer is connected to exactly two gates in the first layer. The third layer has only one "_or_" gate, which is connected to all the gates in the second layer (in other words, the entire circuit produces 1 if and only if at least one gate in the second layer produces 1). The problem is, Hibiki knows very well that when the person is in love, his ability to think logically degrades drastically. In particular, it is well known that when a person in love evaluates a logical circuit in his mind, every gate evaluates to a value that is the opposite of what it was supposed to evaluate to. For example, "_or_" gates return 1 if and only if both inputs are zero, "t{nand}" gates produce 1 if and only if both inputs are one etc. In particular, the "_or_" gate in the last layer also produces opposite results, and as such if Hibiki is in love, the entire circuit produces 1 if and only if all the gates on the second layer produced 0. Hibiki can’t allow love to affect his decision. He wants to know what is the smallest number of gates that needs to be removed from the second layer so that the output of the circuit for all possible inputs doesn't depend on whether Hibiki is in love or not. ## Input The first line contains three integers $n$, $m$, $k$ ($2 \le n, m \le 50$; $1 \le k \le 50$) — the number of input features, the number of gates in the first layer, and the number of gates in the second layer correspondingly. The second line contains $m$ pairs of strings separated by spaces describing the first layer. The first string in each pair describes the gate ("_and_", "_or_", "_nand_" or "_nor_"), and the second string describes the two input features the gate is connected two as a string consisting of exactly $n$ characters, with exactly two characters (that correspond to the input features the gate is connected to) equal to '_x_' and the remaining characters equal to "_._'. The third line contains $k$ pairs of strings separated by spaces describing the second layer in the same format, where the strings that describe the input parameters have length $m$ and correspond to the gates of the first layer. ## Output Print the number of gates that need to be removed from the second layer so that the output of the remaining circuit doesn't depend on whether Hibiki is in love or not. If no matter how many gates are removed the output of the circuit continues to depend on Hibiki's feelings, print $-1$. [samples] ## Note In the first example the two gates in the first layer are connected to the same inputs, but first computes "_and_" while second computes "_nand_", and as such their output is always different no matter what the input is and whether Hibiki is in love or not. The second layer has "_or_" and "_and_" gates both connected to the two gates in the first layer. If Hibiki is not in love, the "_and_" gate will produce 0 and the "_or_" gate will produce 1 no matter what input features are equal to, with the final "_or_" gate in the third layer always producing the final answer of 1. If Hibiki is in love, "_and_" gate in the second layer will produce 1 and "_or_" gate will produce 0 no matter what the input is, with the final "_or_" gate in the third layer producing the final answer of 0. Thus, if both gates in the second layer are kept, the output of the circuit does depend on whether Hibiki is in love. If any of the two gates in the second layer is dropped, the output of the circuit will no longer depend on whether Hibiki is in love or not, and hence the answer is 1. In the second example no matter what gates are left in the second layer, the output of the circuit will depend on whether Hibiki is in love or not. In the third example if Hibiki keeps second, third and fourth gates in the second layer, the circuit will not depend on whether Hibiki is in love or not. Alternatively, he can keep the first and the last gates. The former requires removing two gates, the latter requires removing three gates, so the former is better, and the answer is 2.
Hibiki 和 Dita 彼此相爱,但他们属于长期敌对的两个群体。Hibiki 深刻关注当前局势,希望弄清楚他与 Dita 的关系是出于爱,还是背叛。 Hibiki 准备了若干二元特征,这些特征将决定他的决策,并在其上构建了一个三层逻辑电路。每层包含一个或多个逻辑门。电路中的每个门均为 "_or_"、"_and_"、"_nor_"(非或)或 "_nand_"(非与)之一。第一层的每个门恰好连接两个特征。第二层的每个门恰好连接第一层的两个门。第三层仅有一个 "_or_" 门,它连接第二层的所有门(换句话说,整个电路输出 1 当且仅当第二层至少有一个门输出 1)。 问题是,Hibiki 非常清楚,当一个人陷入爱河时,其逻辑思维能力会急剧下降。众所周知,当一个陷入爱河的人在脑海中评估逻辑电路时,每个门的输出值都会与预期值相反。例如,"_or_" 门仅在两个输入均为 0 时返回 1,"_nand_" 门仅在两个输入均为 1 时输出 1,等等。 特别地,最后一层的 "_or_" 门也会输出相反的结果。因此,如果 Hibiki 在恋爱中,整个电路输出 1 当且仅当第二层的所有门均输出 0。 Hibiki 不允许爱情影响他的决策。他想知道,最少需要从第二层移除多少个门,才能使电路对所有可能输入的输出不依赖于 Hibiki 是否在恋爱中。 第一行包含三个整数 $n$、$m$、$k$($2 lt.eq n, m lt.eq 50$;$1 lt.eq k lt.eq 50$),分别表示输入特征数、第一层门数和第二层门数。 第二行包含 $m$ 个由空格分隔的字符串对,描述第一层。每对中的第一个字符串描述门的类型("_and_"、"_or_"、"_nand_" 或 "_nor_"),第二个字符串描述该门连接的两个输入特征,为一个恰好 $n$ 个字符的字符串,其中恰好有两个字符为 '_x_'(对应连接的两个输入特征),其余字符为 "_._"。 第三行包含 $k$ 个由空格分隔的字符串对,以相同格式描述第二层,其中描述输入参数的字符串长度为 $m$,对应第一层的门。 请输出需要从第二层移除的门的最小数量,使得剩余电路的输出不依赖于 Hibiki 是否在恋爱中。 如果无论移除多少门,电路的输出始终依赖于 Hibiki 的情感状态,请输出 $-1$。 在第一个示例中,第一层的两个门连接相同的输入,但第一个计算 "_and_",第二个计算 "_nand_",因此无论输入如何,也无论 Hibiki 是否在恋爱中,它们的输出总是不同。第二层有两个门,分别为 "_or_" 和 "_and_",均连接第一层的两个门。如果 Hibiki 不在恋爱中,"_and_" 门输出 0,"_or_" 门输出 1,无论输入特征为何,第三层的最终 "_or_" 门始终输出 1。如果 Hibiki 在恋爱中,第二层的 "_and_" 门输出 1,"_or_" 门输出 0,无论输入为何,第三层的最终 "_or_" 门输出 0。因此,若保留第二层的两个门,电路输出依赖于 Hibiki 是否在恋爱中;若移除其中任意一个门,电路输出将不再依赖于 Hibiki 是否在恋爱中,故答案为 1。 在第二个示例中,无论第二层保留哪些门,电路输出始终依赖于 Hibiki 是否在恋爱中。 在第三个示例中,若 Hibiki 保留第二、第三和第四个门,电路输出将不依赖于他是否在恋爱中。或者,他也可以保留第一个和最后一个门。前者需移除两个门,后者需移除三个门,前者更优,故答案为 2。 ## Input 第一行包含三个整数 $n$、$m$、$k$($2 lt.eq n, m lt.eq 50$;$1 lt.eq k lt.eq 50$),分别表示输入特征数、第一层门数和第二层门数。第二行包含 $m$ 个由空格分隔的字符串对,描述第一层。每对中的第一个字符串描述门的类型("_and_"、"_or_"、"_nand_" 或 "_nor_"),第二个字符串描述该门连接的两个输入特征,为一个恰好 $n$ 个字符的字符串,其中恰好有两个字符为 '_x_'(对应连接的两个输入特征),其余字符为 "_._"。第三行包含 $k$ 个由空格分隔的字符串对,以相同格式描述第二层,其中描述输入参数的字符串长度为 $m$,对应第一层的门。 ## Output 请输出需要从第二层移除的门的最小数量,使得剩余电路的输出不依赖于 Hibiki 是否在恋爱中。如果无论移除多少门,电路的输出始终依赖于 Hibiki 的情感状态,请输出 $-1$。 [samples] ## Note 在第一个示例中,第一层的两个门连接相同的输入,但第一个计算 "_and_",第二个计算 "_nand_",因此无论输入如何,也无论 Hibiki 是否在恋爱中,它们的输出总是不同。第二层有两个门,分别为 "_or_" 和 "_and_",均连接第一层的两个门。如果 Hibiki 不在恋爱中,"_and_" 门输出 0,"_or_" 门输出 1,无论输入特征为何,第三层的最终 "_or_" 门始终输出 1。如果 Hibiki 在恋爱中,第二层的 "_and_" 门输出 1,"_or_" 门输出 0,无论输入为何,第三层的最终 "_or_" 门输出 0。因此,若保留第二层的两个门,电路输出依赖于 Hibiki 是否在恋爱中;若移除其中任意一个门,电路输出将不再依赖于 Hibiki 是否在恋爱中,故答案为 1。在第二个示例中,无论第二层保留哪些门,电路输出始终依赖于 Hibiki 是否在恋爱中。在第三个示例中,若 Hibiki 保留第二、第三和第四个门,电路输出将不依赖于他是否在恋爱中。或者,他也可以保留第一个和最后一个门。前者需移除两个门,后者需移除三个门,前者更优,故答案为 2。
Let $ n $, $ m $, $ k $ be given integers: number of input features, gates in layer 1, gates in layer 2. Let $ G_1 = \{g_1^{(1)}, g_2^{(1)}, \dots, g_m^{(1)}\} $ be the set of gates in layer 1, each $ g_i^{(1)} $ is a logic gate of type $ t_i \in \{\text{and}, \text{or}, \text{nand}, \text{nor}\} $, and has a binary input vector $ \mathbf{a}_i \in \{0,1\}^n $ with exactly two 1s indicating which two input features it connects to. Let $ G_2 = \{g_1^{(2)}, g_2^{(2)}, \dots, g_k^{(2)}\} $ be the set of gates in layer 2, each $ g_j^{(2)} $ is a logic gate of type $ s_j \in \{\text{and}, \text{or}, \text{nand}, \text{nor}\} $, and has a binary selector vector $ \mathbf{b}_j \in \{0,1\}^m $ indicating which layer-1 gates it connects to (1 if connected, 0 otherwise). Define for each input $ \mathbf{x} \in \{0,1\}^n $: - $ f_i(\mathbf{x}) $: output of layer-1 gate $ g_i^{(1)} $ under normal evaluation. - $ f_i'(\mathbf{x}) $: output of layer-1 gate $ g_i^{(1)} $ under "in-love" evaluation (i.e., inverted logic: OR becomes NOR, AND becomes NAND, etc.). Define the inverted logic function for each gate type: - $ \text{and}'(\alpha, \beta) = \neg(\alpha \lor \beta) = \text{nor}(\alpha, \beta) $ - $ \text{or}'(\alpha, \beta) = \neg(\alpha \land \beta) = \text{nand}(\alpha, \beta) $ - $ \text{nand}'(\alpha, \beta) = \alpha \land \beta = \text{and}(\alpha, \beta) $ - $ \text{nor}'(\alpha, \beta) = \alpha \lor \beta = \text{or}(\alpha, \beta) $ Thus, $ f_i'(\mathbf{x}) = \text{inv}(t_i)(\mathbf{x}) $, where $ \text{inv} $ maps: - $ \text{and} \mapsto \text{nor} $ - $ \text{or} \mapsto \text{nand} $ - $ \text{nand} \mapsto \text{and} $ - $ \text{nor} \mapsto \text{or} $ Define for each layer-2 gate $ g_j^{(2)} $: - $ h_j(\mathbf{x}) = s_j\left( \bigoplus_{i=1}^m \mathbf{b}_{j,i} \cdot f_i(\mathbf{x}) \right) $ — normal output - $ h_j'(\mathbf{x}) = \text{inv}(s_j)\left( \bigoplus_{i=1}^m \mathbf{b}_{j,i} \cdot f_i'(\mathbf{x}) \right) $ — in-love output The final output under normal conditions: $ F(\mathbf{x}) = \bigvee_{j=1}^k h_j(\mathbf{x}) $ Under in-love conditions: $ F'(\mathbf{x}) = \bigwedge_{j=1}^k \neg h_j'(\mathbf{x}) $ (since final layer OR is inverted → becomes AND of negated inputs) We are to find the **minimum number of gates to remove from layer 2** such that for all $ \mathbf{x} \in \{0,1\}^n $, the output of the circuit is the same under both evaluations: $$ \forall \mathbf{x} \in \{0,1\}^n, \quad \bigvee_{j \in S} h_j(\mathbf{x}) = \bigwedge_{j \in S} \neg h_j'(\mathbf{x}) $$ where $ S \subseteq \{1,2,\dots,k\} $ is the set of remaining layer-2 gates. Let $ R = \{1,2,\dots,k\} \setminus S $ be the set of removed gates. We want to **minimize $ |R| $** such that the above equality holds for all $ \mathbf{x} $. If no such $ S $ exists, output $ -1 $. --- **Formal Problem Statement:** Given: - $ n, m, k \in \mathbb{N} $, $ 2 \leq n, m \leq 50 $, $ 1 \leq k \leq 50 $ - For $ i = 1 $ to $ m $: gate type $ t_i \in \{\text{and}, \text{or}, \text{nand}, \text{nor}\} $, and input mask $ \mathbf{a}_i \in \{0,1\}^n $ with exactly two 1s - For $ j = 1 $ to $ k $: gate type $ s_j \in \{\text{and}, \text{or}, \text{nand}, \text{nor}\} $, and selector mask $ \mathbf{b}_j \in \{0,1\}^m $ Define for each $ \mathbf{x} \in \{0,1\}^n $: - $ f_i(\mathbf{x}) = \text{eval}_{t_i}(\mathbf{a}_i \cdot \mathbf{x}) $ — output of layer-1 gate $ i $ - $ f_i'(\mathbf{x}) = \text{eval}_{\text{inv}(t_i)}(\mathbf{a}_i \cdot \mathbf{x}) $ Define for each $ j \in \{1,\dots,k\} $: - $ h_j(\mathbf{x}) = \text{eval}_{s_j}\left( \sum_{i=1}^m \mathbf{b}_{j,i} \cdot f_i(\mathbf{x}) \right) $ - $ h_j'(\mathbf{x}) = \text{eval}_{\text{inv}(s_j)}\left( \sum_{i=1}^m \mathbf{b}_{j,i} \cdot f_i'(\mathbf{x}) \right) $ Define: - $ F_S(\mathbf{x}) = \bigvee_{j \in S} h_j(\mathbf{x}) $ - $ F_S'(\mathbf{x}) = \bigwedge_{j \in S} \neg h_j'(\mathbf{x}) $ Find: $$ \min_{\substack{S \subseteq \{1,\dots,k\} \\ \forall \mathbf{x} \in \{0,1\}^n: F_S(\mathbf{x}) = F_S'(\mathbf{x})}} |k - |S|| $$ If no such $ S $ exists, output $ -1 $.
Samples
Input #1
2 2 2
and xx nand xx
and xx or xx
Output #1
1
Input #2
3 2 2
and xx. nor .xx
and xx nor xx
Output #2
\-1
Input #3
4 4 5
nor x..x and ..xx and xx.. nand xx..
nand ..xx nor ..xx and xx.. nor ..xx or ..xx
Output #3
2
API Response (JSON)
{
  "problem": {
    "name": "F. The Moral Dilemma",
    "description": {
      "content": "Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relatio",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF993F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relatio...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Hibiki 和 Dita 彼此相爱,但他们属于长期敌对的两个群体。Hibiki 深刻关注当前局势,希望弄清楚他与 Dita 的关系是出于爱,还是背叛。\n\nHibiki 准备了若干二元特征,这些特征将决定他的决策,并在其上构建了一个三层逻辑电路。每层包含一个或多个逻辑门。电路中的每个门均为 \"_or_\"、\"_and_\"、\"_nor_\"(非或)或 \"_nand_\"(非与)之一。第一层的每个门恰好连接...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $, $ m $, $ k $ be given integers: number of input features, gates in layer 1, gates in layer 2.\n\nLet $ G_1 = \\{g_1^{(1)}, g_2^{(1)}, \\dots, g_m^{(1)}\\} $ be the set of gates in layer 1, each ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments