Codeforces contest 1010 problem D. 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 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.

这道题题目太长了,看一会没看懂就去找别人的博客了
https://blog.csdn.net/zy704599894/article/details/81237684
这个的思路比较简单,就是暴力dfs,先处理一遍不改值的ans,再dfs一遍改变之后的。
a==1的时候,如果ne2==0就说明无论ne1怎么改都不会影响上层的值了,就传递0。
a==2的时候,如果ne2==1也是如此,传递0;
其他的也按照这样的思路dfs,最后输出。

#include<bits/stdc++.h>
using namespace std;
int val[1000005],son[1000005][2],a[1000005];
int ans[1000005];
vector<int>vec[1000005];
int n;
void dfs1(int x)
{
    for(int i=0;i<vec[x].size();i++)
        dfs1(vec[x][i]);
    if(a[x]==1)
        val[x]=val[vec[x][1]]&val[vec[x][0]];
    else if(a[x]==2)
        val[x]=val[vec[x][1]]|val[vec[x][0]];
    else if(a[x]==3)
        val[x]=val[vec[x][1]]^val[vec[x][0]];
    else if(a[x]==4)
        val[x]=val[vec[x][0]]^1;
}
void dfs2(int x,int flag)
{
    if(a[x]==5)
        ans[x]=val[1]^flag;
    else if(a[x]==1)
    {
        int ne1=vec[x][0],ne2=vec[x][1];
        if(val[ne2]==0)
            dfs2(ne1,0);
        else
            dfs2(ne1,min(flag,1));
        if(val[ne1]==0)
            dfs2(ne2,0);
        else
            dfs2(ne2,min(flag,1));
    }
    else if(a[x]==2)
    {
        int ne1=vec[x][0],ne2=vec[x][1];
        if(val[ne2]==1)
            dfs2(ne1,0);
        else
            dfs2(ne1,min(flag,1));
        if(val[ne1]==1)
            dfs2(ne2,0);
        else
            dfs2(ne2,min(flag,1));
    }
    else if(a[x]==3)
    {
        int ne1=vec[x][0],ne2=vec[x][1];
        dfs2(ne1,min(flag,1));
        dfs2(ne2,min(flag,1));
    }
    else
        dfs2(vec[x][0],min(flag,1));
}
int main()
{
    scanf("%d",&n);
    char s[10];
    int x,y;
    memset(ans,-1,sizeof(ans));
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        if(s[0]=='A')
        {
            scanf("%d%d",&x,&y);
            vec[i].push_back(x);
            vec[i].push_back(y);
            a[i]=1;
        }
        else if(s[0]=='O')
        {
            scanf("%d%d",&x,&y);
            vec[i].push_back(x);
            vec[i].push_back(y);
            a[i]=2;
        }
        else if(s[0]=='X')
        {
            scanf("%d%d",&x,&y);
            vec[i].push_back(x);
            vec[i].push_back(y);
            a[i]=3;
        }
        else if(s[0]=='N')
        {
            scanf("%d",&x);
            vec[i].push_back(x);
            a[i]=4;
        }
        else
        {
            scanf("%d",&x);
            a[i]=5;
            val[i]=x;
        }

    }
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=n;i++)
        if(ans[i]!=-1)
            printf("%d",ans[i]);
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/81607857