CF1010D.Mars rover
普及+/提高
通过率:0%
时间限制:5.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely — the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex 1, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output.
There are four types of logical elements: AND (2 inputs), OR (2 inputs), XOR (2 inputs), NOT (1 input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input.
For each input, determine what the output will be if Natasha changes this input.
娜塔莎正在火星漫游车上环游火星。但突然间,漫游车发生了故障——其内部的逻辑电路出现了问题。该电路是一棵无向树(即连通的无环图),根节点为顶点 1;其中每个叶节点(根节点除外)均为输入端口,其余所有顶点(包括作为输出端口的根节点)均为逻辑元件。每个输入端口接收一位二进制值,输出端口返回一位二进制值。
共有四种类型的逻辑元件:AND(2 个输入)、OR(2 个输入)、XOR(2 个输入)、NOT(1 个输入)。逻辑元件从其直接子节点(即输入)获取数值,并返回其所执行逻辑运算的结果。娜塔莎已知火星漫游车的逻辑电路结构,并且知道仅有一个输入端口发生故障。为了修复漫游车,她需要更改该故障输入端口上的值。
对每个输入端口,请确定:若娜塔莎更改该输入端口的值,最终输出将为何值。
输入格式
The first line contains a single integer n (2≤n≤106) — the number of vertices in the graph (both inputs and elements).
The i-th of the next n lines contains a description of i-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vertex is "IN", then the value of this input follows (0 or 1), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have 2 inputs, whereas "NOT" has 1 input. The vertices are numbered from one.
It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex 1.
第一行包含一个整数 n(2≤n≤106)—— 图中顶点的数量(包括输入节点和逻辑门节点)。
接下来的 n 行中,第 i 行描述第 i 个顶点:首词为 "AND"、"OR"、"XOR"、"NOT" 或 "IN"(表示电路的输入)。若该顶点类型为 "IN",则其后紧跟该输入的值(0 或 1);否则,其后为该逻辑门的输入顶点编号:"AND"、"OR"、"XOR" 均有两个输入,而 "NOT" 有一个输入。所有顶点编号从 1 开始。
保证输入数据构成一个合法的逻辑电路,且电路的输出由顶点 1 产生。
输出格式
Print a string of characters '0' and '1' (without quotes) — answers to the problem for each input in the ascending order of their vertex indices.
输出一个由字符 '0' 和 '1'(不含引号)组成的字符串——该字符串表示对每个输入所对应的答案,且按顶点索引升序排列。
输入输出样例
输入#1
10 AND 9 4 IN 1 IN 1 XOR 6 5 AND 3 7 IN 0 NOT 10 IN 1 IN 1 AND 2 8
输出#1
10110
说明/提示
The original scheme from the example (before the input is changed):

Green indicates bits '1', yellow indicates bits '0'.
If Natasha changes the input bit 2 to 0, then the output will be 1.
If Natasha changes the input bit 3 to 0, then the output will be 0.
If Natasha changes the input bit 6 to 1, then the output will be 1.
If Natasha changes the input bit 8 to 0, then the output will be 1.
If Natasha changes the input bit 9 to 0, then the output will be 0.
示例中的原始方案(在输入更改之前):

绿色表示比特‘1’,黄色表示比特‘0’。
若娜塔莎将输入比特 2 改为 0,则输出为 1。
若娜塔莎将输入比特 3 改为 0,则输出为 0。
若娜塔莎将输入比特 6 改为 1,则输出为 1。
若娜塔莎将输入比特 8 改为 0,则输出为 1。
若娜塔莎将输入比特 9 改为 0,则输出为 0。
输入解题思路,AI测评打分。不知道怎么写?