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 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 (2inputs), OR (2 inputs), XOR (2 inputs), NOT (1input). 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.

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

const int maxn=1e6+10;
struct Node{
    int t; //
    int l,r;
    int s;
    int f;//该节点是不是可以影响到最终的输出
}tr[maxn];
/*
核心思想:每个点的影响是有范围的,看看那些能影响到根节点

从根节点往下遍历,看看,是不是,改变一下值,就会改变输出
*/

int dfs(int o){
    if(tr[o].t==2)return tr[o].s;
    int l=tr[o].l,r=tr[o].r;
    if(tr[o].t==1){
        int s1=dfs(l),s2=dfs(r);
        return tr[o].s=(s1&s2);
    }
    else if(tr[o].t==3){
        int s1=dfs(l),s2=dfs(r);
        return tr[o].s=(s1^s2);
    }
    else if(tr[o].t==4){
        int s1=dfs(l),s2=dfs(r);
        return tr[o].s=(s1|s2);
    }
    else if(tr[o].t==5){
        return tr[o].s=(dfs(l)^1);
    }
}

void dfs_(int o,int x){
    tr[o].f=x;
    int l=tr[o].l,r=tr[o].r;
    if(tr[o].t==1){
        int s1=tr[l].s^1,s2=tr[r].s;int s=s1&s2;
        if(s!=tr[o].s)tr[l].f=tr[o].f;else tr[l].f=0;//如果数值变了,就说明,改的这个点,是会影响当前开关的,也就是和当前开关对根节点的影响相同
        dfs_(l,tr[l].f);
        s1=tr[l].s,s2=tr[r].s^1,s=s1&s2;
        if(s!=tr[o].s)tr[r].f=tr[o].f;else tr[r].f=0;
        dfs_(r,tr[r].f);
    }
    else if(tr[o].t==3){
        dfs_(l,x);dfs_(r,x);//XOR肯定有影响
    }
    else if(tr[o].t==4){
        int s1=tr[l].s^1,s2=tr[r].s;int s=s1|s2;
        if(s!=tr[o].s)tr[l].f=tr[o].f;else tr[l].f=0;
        dfs_(l,tr[l].f);
        s1=tr[l].s,s2=tr[r].s^1,s=s1|s2;
        if(s!=tr[o].s)tr[r].f=tr[o].f;else tr[r].f=0;
        dfs_(r,tr[r].f);
    }
    else if(tr[o].t==5){
        dfs_(l,x);
    }
}

char str[10];
int main(){
    int n;
    scanf("%d",&n);
    rep(i,1,n+1){
        scanf(" %s",str);
        if(str[0]=='A'){
            scanf("%d %d",&tr[i].l,&tr[i].r);
            tr[i].t=1;
        }
        else if(str[0]=='I'){
            scanf("%d",&tr[i].s);
            tr[i].t=2;
        }
        else if(str[0]=='X'){
            scanf("%d %d",&tr[i].l,&tr[i].r);
            tr[i].t=3;
        }
        else if(str[0]=='O'){
            scanf("%d %d",&tr[i].l,&tr[i].r);
            tr[i].t=4;
        }
        else if(str[0]=='N'){
            scanf("%d",&tr[i].l);
            tr[i].t=5;
        }
    }
    dfs(1);
    //rep(i,1,n+1) printf("i:%d %d\n",i,tr[i].s);
    dfs_(1,1);
   // rep(i,1,n+1) printf("i:%d %d\n",i,tr[i].f);
    string s="";
    rep(i,1,n+1){
        if(tr[i].t==2){
            int v=(tr[i].f^tr[1].s);
            s+=(v+'0');
        }
    }
    cout<<s<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值