codeforces 1010D - Mars rover

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.

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");
    }

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值