codeforce Round 499 Div2 F Mars rover——记忆化搜索

题目描述:

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 11, 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 (22 inputs), OR (22 inputs), XOR (22 inputs), NOT (11 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.

Input

The first line contains a single integer nn (2n1062≤n≤106) — the number of vertices in the graph (both inputs and elements).

The ii-th of the next nn lines contains a description of ii-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 (00 or 11), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have 22 inputs, whereas "NOT" has 11 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 11.

Output

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。非叶子节点代表一种逻辑运算,例如:与,或,非,异或。问你仅改变一个叶子节点的数值其他不变的情况下,整棵树的逻辑值将是多少。

输出按叶子节点的大小依次改变并输出。

解决方法:记忆化搜索。

 

先介绍一下将会用到的数组的作用:

lchild数组:记录左儿子。

rchild数组:记录右儿子。

father数组:记录父亲节点。

value数组:初始节点的数值。(即0或者1)

nums数组:nums[i][j]表示第i个节点的数值为j的情况下,根节点的逻辑值。(当然前提是其他不变)

oper数组:记录节点需要进行的操作。

下面是AC代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<string.h>
 4 using namespace std;
 5 #define scanf_s scanf
 6 const  int MAXN = 1000000;
 7 int lchild[MAXN + 5], rchild[MAXN + 5], father[MAXN + 5], value[MAXN + 5], oper[MAXN + 5], nums[MAXN + 5][2];
 8 int dfs(int now)
 9 {
10     if (oper[now] == 5) return value[now];
11     int ans = 0;
12     ans = dfs(lchild[now]);
13     if (oper[now] == 1) return  value[now]=ans & dfs(rchild[now]);
14     else if (oper[now] == 2) return value[now]=ans | dfs(rchild[now]);
15     else if (oper[now] == 3) return value[now]=ans ^ dfs(rchild[now]);
16     else return value[now]=!ans;
17 }
18 void dfs2(int now)
19 {
20     if (oper[now] == 5) return;
21     if (oper[now] == 1) {
22         nums[lchild[now]][!value[lchild[now]]] = nums[now][(!value[lchild[now]])&value[rchild[now]]];
23         nums[rchild[now]][!value[rchild[now]]] = nums[now][(!value[rchild[now]])&value[lchild[now]]];
24     }
25     else if (oper[now] == 2) {
26         nums[lchild[now]][!value[lchild[now]]] = nums[now][(!value[lchild[now]])|value[rchild[now]]];
27         nums[rchild[now]][!value[rchild[now]]] = nums[now][(!value[rchild[now]])|value[lchild[now]]];
28     }
29     else if (oper[now] == 3) {
30         nums[lchild[now]][!value[lchild[now]]] = nums[now][(!value[lchild[now]])^value[rchild[now]]];
31         nums[rchild[now]][!value[rchild[now]]] = nums[now][(!value[rchild[now]])^value[lchild[now]]];
32     }
33     else if (oper[now] == 4) {
34         nums[lchild[now]][!value[lchild[now]]] = nums[now][value[lchild[now]]];
35     }
36     dfs2(lchild[now]);
37     if (rchild[now]) dfs2(rchild[now]);
38 }
39 int main()
40 {
41     memset(nums, -1, sizeof(nums));
42     nums[1][0] = 0;
43     nums[1][1] = 1;
44     char operation[10];
45     int n = 0, l = 0, r = 0;
46     scanf_s("%d", &n);
47     for (int i = 1; i <= n; ++i) {
48         scanf_s("%s", operation);
49         if (operation[0] == 'A') {
50             oper[i] = 1;
51             scanf_s("%d %d", lchild + i, rchild + i);
52             father[lchild[i]] = father[rchild[i]] = i;
53         }
54         else if (operation[0] == 'O') {
55             oper[i] = 2;
56             scanf_s("%d %d", lchild + i, rchild + i);
57             father[lchild[i]] = father[rchild[i]] = i;
58         }
59         else if (operation[0] == 'X') {
60             oper[i] = 3;
61             scanf_s("%d %d", lchild + i, rchild + i);
62             father[lchild[i]] = father[rchild[i]] = i;
63         }
64         else if (operation[0] == 'N') {
65             oper[i] = 4;
66             scanf_s("%d", lchild + i);
67             father[lchild[i]] = i;
68         }
69         else {
70             oper[i] = 5;
71             scanf_s("%d", value + i);
72         }
73     }
74     int ans=dfs(1);
75     for (int i = 2; i <= n; ++i) {
76         nums[i][value[i]] = ans;
77     }
78     dfs2(1);
79     for (int i = 2; i <= n; ++i) {
80         if (oper[i] == 5) {
81             printf("%d", nums[i][!value[i]]);
82         }
83     }
84 }
View Code

 

posted on 2019-03-08 14:09  新手n号  阅读(146)  评论(0)    收藏  举报

导航