F. Mars rover Codeforces Round #499 (Div. 2)(树形dp)

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.

题意还是很好懂的

思路:树建立起来后,先处理出dp1[x] 为当前节点向上传递的值,然后再跑一点dfs 处理出 dp2[x]表示当前节点值的改变会不会改变导致根节点的变换,从上往下跑就好了,非常简单的F题
accode

#include<bits/stdc++.h>
#define LL long long
#define INF  0x3f3f3f3f
using namespace std;
const int maxn = 1e6+32;
int type[maxn];
int n;
int head[maxn];
int va[maxn];
int tot;
vector<int>E[maxn];
int fa[maxn];
int leaf[maxn];
int dp[maxn];
void dfs(int u,int fa)
{

    if(type[u]==2){
        dp[u] = va[u];
        return ;
    }
    for(int i = 0;i<E[u].size();i++){
        int v = E[u][i];
        dfs(v,u);
    } if(type[u]==1){
            dp[u] = dp[E[u][0]]&dp[E[u][1]];
        }
        else if(type[u]==4){
            dp[u] = !dp[E[u][0]];
        }
        else if(type[u]==3){
            dp[u] = dp[E[u][0]]^dp[E[u][1]];
        }
        else if(type[u] = 5){
            dp[u] = dp[E[u][0]]|dp[E[u][1]];
        }

}
int dp2[maxn];
void dfs2(int u,int falg)
{
    if(type[u]==2){
        dp2[u] = falg;
        return ;
    }
    for(int i = 0;i<E[u].size();i++){
        int v = E[u][i];
        if(type[u]==1){
            if(dp[v]==1&&dp[E[u][i^1]]==1){
                dfs2(v,1&&falg);
            }
            else if(dp[v]==0&&dp[E[u][i^1]]==1){
                dfs2(v,1&&falg);
            }
            else{
                dfs2(v,0&&falg);
            }
        }
        else if(type[u]==4){
           dfs2(v,1&&falg);
        }
        else if(type[u]==3){
           dfs2(v,1&&falg);
        }
        else if(type[u] = 5){
             if(dp[v]==1&&dp[E[u][i^1]]==0){
                dfs2(v,1&&falg);
            }
            else if(dp[v]==0&&dp[E[u][i^1]]==0){
                dfs2(v,1&&falg);
            }
            else{
                dfs2(v,0&&falg);
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    char s[10];
    int p = 0;
    for(int i = 1;i<=n;i++){
        scanf("%s",s);
        if(s[0]=='A'){
            type[i] = 1;
            int l,r;
            scanf("%d%d",&l,&r);
            E[i].push_back(l);
            E[i].push_back(r);
            fa[l] = i;
            fa[r] = i;
        }
        else if(s[0] =='I'){
            leaf[p] =i;
            int x;
            scanf("%d",&x);
            type[i] = 2;
            va[i] = x;
            p++;
        }
        else if(s[0] == 'X'){
            type[i] = 3;
            int l,r;
            scanf("%d%d",&l,&r);
            E[i].push_back(l);
            E[i].push_back(r);
            fa[l] = i;
            fa[r] = i;

        }
        else if(s[0]=='N'){
            type[i] = 4;
            int x;
            scanf("%d",&x);
            E[i].push_back(x);
            fa[x] = i;
        }
        else{
            type[i] = 5;
            int l,r;
            scanf("%d%d",&l,&r);
            E[i].push_back(l);
            E[i].push_back(r);
            fa[l] = i;
            fa[r] = i;
        }
    }
    dfs(1,-1);
    dfs2(1,1);
    //cout<<dp[1]<<endl;
    for(int i = 0;i<p;i++){
       // cout<<dp2[leaf[i]]<<' '<<leaf[i]<<endl;
        if(dp2[leaf[i]]==1){
            printf("%d",!dp[1]);
        }
        else{
            printf("%d",dp[1]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/w571523631/article/details/81240624