codeforces 1010D - Mars rover

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zy704599894/article/details/81237684

D. Mars rover

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

扫描二维码关注公众号,回复: 3411652 查看本文章

For each input, determine what the output will be if Natasha changes this input.

Input

The first line contains a single integer nn (2≤n≤1062≤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.

Example

input

Copy

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

output

Copy

10110

Note

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 22 to 00, then the output will be 11.

If Natasha changes the input bit 33 to 00, then the output will be 00.

If Natasha changes the input bit 66 to 11, then the output will be 11.

If Natasha changes the input bit 88 to 00, then the output will be 11.

If Natasha changes the input bit 99 to 00, then the output will be 00.

题意:有一个以1位根的树,设每个节点的值为val则有以下输入

AND x y: val[i](第i次输入)= val[x] & val[y];

OR x y: val[i] = val[x] | val[y];

XOR x y: val[i] = val[x] ^ val[y];

NOT x: val[i] = val[x] ^ 1;

IN X: val[i] = val[x] ^ 1;

设根节点1位输出端,所有IN为输入端,求出每一个IN节点x的值 val[x] ^ 1 后输出端的值,输入端一定是叶子节点。每个节点最多有两个儿子,如果一个节点产生变化会对根节点产生影响,那么从根节点到这个节点的路径所经过的节点都需要改变才行,换句话说,如果根节点到某一个输入端所经过的节点中,只有有任意节点不受儿子的变化而变化,那么那个输入端一定不会改变根节点的值,所以只要dfs一遍即可。

  • #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    using namespace std;
    #define ll long long
    const int maxm = 1000005;
    vector<int>v[maxm];
    int a[maxm], val[maxm], ans[maxm];
    char str[maxm];
    void dfs_1(int k)
    {
    	for (int i = 0;i < v[k].size();i++)
    		dfs_1(v[k][i]);
    	if (a[k] == 1) val[k] = val[v[k][0]] & val[v[k][1]];
    	else if (a[k] == 2) val[k] = val[v[k][0]] | val[v[k][1]];
    	else if (a[k] == 3) val[k] = val[v[k][0]] ^ val[v[k][1]];
    	else if (a[k] == 4) val[k] = val[v[k][0]] ^ 1;
    }
    void dfs_2(int k, int flag)
    {
    	if (a[k] == 5) ans[k] = val[1] ^ flag;
    	else if (a[k] == 1)
    	{
    		int x = v[k][0], y = v[k][1];
    		//printf("%d %d\n", val[x], val[y]);
    		if (val[x] == 0 && val[y] == 0 || val[x] == 1 && val[y] == 0)
    			dfs_2(x, min(flag, 0));
    		else dfs_2(x, min(flag, 1));
    		if (val[x] == 0 && val[y] == 0 || val[x] == 0 && val[y] == 1)
    			dfs_2(y, min(flag, 0));
    		else dfs_2(y, min(flag, 1));
    	}
    	else if (a[k] == 2)
    	{
    		int x = v[k][0], y = v[k][1];
    		if (val[x] == 1 && val[y] == 1 || val[x] == 0 && val[y] == 1) 
    			dfs_2(x, min(flag, 0));
    		else dfs_2(x, min(flag, 1));
    		if (val[x] == 1 && val[y] == 1 || val[x] == 1 && val[y] == 0) 
    			dfs_2(y, min(flag, 0));
    		else dfs_2(y, min(flag, 1));
    	}
    	else if (a[k] == 3)
    	{
    		int x = v[k][0], y = v[k][1];
    		dfs_2(x, min(flag, 1));
    		dfs_2(y, min(flag, 1));
    	}
    	else dfs_2(v[k][0], min(flag, 1));
    }
    int main()
    {
    	int n, i, j, k, sum, x, y;
    	scanf("%d", &n);
    	for (i = 1;i <= n;i++)
    	{
    		ans[i] = -1;
    		scanf("%s", str);
    		if (str[0] == 'I')
    		{
    			scanf("%d", &k);
    			val[i] = k, a[i] = 5;
    		}
    		else if (str[0] == 'N')
    		{
    			scanf("%d", &k);
    			v[i].push_back(k), a[i] = 4;
    		}
    		else if (str[0] == 'A')
    		{
    			scanf("%d%d", &x, &y), a[i] = 1;
    			v[i].push_back(x), v[i].push_back(y);
    		}
    		else if (str[0] == 'O')
    		{
    			scanf("%d%d", &x, &y), a[i] = 2;
    			v[i].push_back(x), v[i].push_back(y);
    		}
    		else if (str[0] == 'X')
    		{
    			scanf("%d%d", &x, &y), a[i] = 3;
    			v[i].push_back(x), v[i].push_back(y);
    		}
    	}
    	dfs_1(1);
    	dfs_2(1, 1);
    	for (i = 1;i <= n;i++)
    		if (ans[i] != -1) printf("%d", ans[i]);
    	printf("\n");
    }

猜你喜欢

转载自blog.csdn.net/zy704599894/article/details/81237684