Codeforces 1011F

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

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.

#include<iostream>
#include<cstdio>
#define maxn 1000010
using namespace std;
int n,val[maxn],a[maxn][2],ty[maxn],ans[maxn];
char s[10];
void dfs1(int now){
    if(ty[now]==1)
        dfs1(a[now][0]),dfs1(a[now][1]),val[now]=val[a[now][0]]&val[a[now][1]];
    if(ty[now]==2)
        dfs1(a[now][0]),dfs1(a[now][1]),val[now]=val[a[now][0]]|val[a[now][1]];
    if(ty[now]==3)
        dfs1(a[now][0]),dfs1(a[now][1]),val[now]=val[a[now][0]]^val[a[now][1]];
    if(ty[now]==4)
        dfs1(a[now][0]),val[now]=!val[a[now][0]];
}
void dfs2(int now,int flag){
    if(ty[now]==5){
        ans[now]=val[1]^flag;
        return;
    }
    int x=a[now][0],y=a[now][1];
    if(ty[now]==1){
        if((val[x]==0&&val[y]==0)||(val[x]==1&&val[y]==0))dfs2(x,min(flag,0));
        else dfs2(x,min(flag,1));
        if((val[x]==0&&val[y]==0)||(val[x]==0&&val[y]==1))dfs2(y,min(flag,0));
        else dfs2(y,min(flag,2));
    }
    if(ty[now]==2){
        if((val[x]==1&&val[y]==1)||(val[x]==0&&val[y]==1))dfs2(x,min(flag,0));
        else dfs2(x,min(flag,1));
        if((val[x]==1&&val[y]==1)||(val[x]==1&&val[y]==0))dfs2(y,min(flag,0));
        else dfs2(y,min(flag,2));
    }
    if(ty[now]==3){
        dfs2(x,min(flag,1));
        dfs2(y,min(flag,1));
    }
    if(ty[now]==4){
        dfs2(x,min(flag,1));
    }
}
int main(){
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        if(s[1]=='A'){
            ty[i]=1;
            scanf("%d%d",&x,&y);
            a[i][0]=x;a[i][1]=y;
        }
        if(s[1]=='O'){
            ty[i]=2;
            scanf("%d%d",&x,&y);
            a[i][0]=x;a[i][1]=y;
        }
        if(s[1]=='X'){
            ty[i]=3;
            scanf("%d%d",&x,&y);
            a[i][0]=x;a[i][1]=y;
        }
        if(s[1]=='N'){
            ty[i]=4;
            scanf("%d",&x);
            a[i][0]=x;
        }
        if(s[1]=='I'){
            ty[i]=5;
            scanf("%d",&x);
            val[i]=x;
        }
    }
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=n;i++)
        if(ty[i]==5)printf("%d",ans[i]);
    puts("");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/thmyl/p/12181686.html
今日推荐