zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Qt-网易云音乐界面实现-4 实现推荐列表和我的音乐列表,重要在QListWidget美化
    Qt-网易云音乐界面实现-3 音乐名片模块的实现
    Qt-网易云音乐界面实现-2 红红的程序运行图标,和相似下方音乐条
    Qt-网易云音乐界面实现-1 窗口隐藏拖拽移动,自定义标题栏
    Qt 利用XML文档,写一个程序集合 四
    promise的简单理解
    toast弹框组件的封装-插件方式
    vuex自动获取当前的时间
    用vue对tabbar的封装
    子组件与父组件的各种传递关系
  • 原文地址:https://www.cnblogs.com/thmyl/p/12181686.html
Copyright © 2011-2022 走看看