Codeforces Div2 F. Mars rover

F. 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 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.

Input

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

.

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 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.

题意:
就是给出一颗树, 变换各个IN的值, 求根节点的值。

一开始想用模拟, 发现TLE。 。然后在别人的细心教诲下终于明白了写。

( 1)先求根节点的值。

( 2)然后从根节点进行搜索。

这里分5种情况。

( 1)当为AND时: 先看看此节点的值, 如果为1,那么改变任意一个子节点的值,都不会改变根节点的值, 所以继续搜索左右子树。如果为0:那么此时左右子树肯定至少有一个必为0,1.如果有一个为0,只有当为0的点改变值时,才会影响根节点,所以搜索为0的子树。2.如果都为0,那么改变其中一个值不会影响根节点。所以直接返回。

(2)当为OR时: 跟上面的情况很像,当为0时, 这时两个子树的改变都会影响根节点的值, 需要都进行搜索。

当为一的时候, 有三种情况, 当两个子树都为1时, 这种情况直接return。 当一个为0, 一个为1时, 为0的子树不会影响根节点的值, 而1的节点的改变会影响根节点的值, 所以这时候需要搜索1的子树。

(3) 当为异或时, 只要改变一个子树的值, 那么根节点的值必定改变, 所以需要搜索两颗子树。

( 4)当为NOT时, 只要改变子节点的值,根节点的值必定改变, 所以需要搜索。

( 5)当为IN时, 说明这个子节点会影响到根的值。 所以需要取反。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
    char s[10];
    int left,right;
    int data;
    int parent;
    int in;
};
node no[1000005];
int n;
int par;
int change[1000005];
int ch=0;
int re;
int dfs (int loc)
{
        if(!strcmp(no[loc].s,"IN"))
           return no[loc].data;

        else if(!strcmp(no[loc].s,"AND"))
        {
                 no[loc].data=(dfs(no[loc].left)&dfs(no[loc].right));
                 return no[loc].data;
        }
        else if(!strcmp(no[loc].s,"XOR"))
        {
            no[loc].data=(dfs(no[loc].left)^dfs(no[loc].right));
             return no[loc].data;
        }
        else if(!strcmp(no[loc].s,"NOT"))
        {
            no[loc].data=(!dfs(no[loc].left));
            return no[loc].data;
        }
        else if(!strcmp(no[loc].s,"OR"))
        {
            no[loc].data=dfs(no[loc].left)|dfs(no[loc].right);
            return no[loc].data;
        }
}
void dfs1 (int loc)
{
    if(!strcmp(no[loc].s,"IN"))
        {
            no[loc].in=1;
            no[loc].data=1^re;
        }

    else if(!strcmp(no[loc].s,"AND"))
    {
        if(!no[loc].data)
        {
            if(!no[no[loc].left].data&&!no[no[loc].right].data)
                   return ;
            else if(!no[no[loc].left].data)
                  dfs1(no[loc].left);
            else if(!no[no[loc].right].data)
                  dfs1(no[loc].right);
        }
        else
        {
             dfs1(no[loc].left);
             dfs1(no[loc].right);
        }
    }
    else if(!strcmp(no[loc].s,"OR"))
    {
        if(no[loc].data)
        {
            if(no[no[loc].left].data&&no[no[loc].right].data)
                  return;
            else if(no[no[loc].left].data)
                dfs1(no[loc].left);
            else if(no[no[loc].right].data)
                dfs1(no[loc].right);
        }
        else
        {
            dfs1(no[loc].left);
            dfs1(no[loc].right);
        }
    }
    else if(!strcmp(no[loc].s,"XOR"))
    {
        dfs1(no[loc].left);
        dfs1(no[loc].right);
    }
    else if(!strcmp(no[loc].s,"NOT"))
        dfs1(no[loc].left);
}
int main()
{
    scanf("%d",&n);
        for (int i=1;i<=n+3;i++)
        {
            no[i].parent=-1;
            no[i].data=-1;
            no[i].in=0;
        }
       for (int i=1;i<=n;i++)
      {
        int left,right;
        scanf("%s",no[i].s);
          if(!strcmp(no[i].s,"AND"))
         {
             scanf("%d%d",&left,&right);
             no[left].parent=i;
             no[right].parent=i;
             no[i].left=left;
             no[i].right=right;
         }
          if(!strcmp(no[i].s,"IN"))
         {
              scanf("%d",&no[i].data);
              no[i].left=no[i].right=-1;
         }
          if(!strcmp(no[i].s,"XOR"))
         {
             scanf("%d%d",&left,&right);
             no[left].parent=i;
             no[right].parent=i;
             no[i].left=left;
             no[i].right=right;
         }
          if(!strcmp(no[i].s,"NOT"))
         {
             scanf("%d",&left);
             no[left].parent=i;
             no[i].left=left;
             no[i].right=-1;
         }
          if(!strcmp(no[i].s,"OR"))
         {
             scanf("%d%d",&left,&right);
             no[left].parent=i;
             no[right].parent=i;
             no[i].left=left;
             no[i].right=right;
         }
       }
    for (int i=1;i<=n;i++)
        if(no[i].parent==-1)
           {
               par=i;
               break;
           }

    re=dfs(par);
    dfs1(par);
    for (int i=1;i<=n;i++)
    {

        if(no[i].s[0]=='I')
        {
            if(no[i].in)
            printf("%d",no[i].data);
            else
            printf("%d",re);
        }
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81393837