Cf1010d Mars Rover [bitwise operation]

Source: Internet
Author: User

Question Portal

Mars Rover

The format is difficult to adjust.

 

Analysis:

I took an enhanced version of this question during the exam today.

In fact, it is not difficult. After establishing a tree structure, we first obtain all the initial weights and then obtain the initial values of the root node. Because only the value of one vertex is modified at a time, we only need to determine from top to bottom whether the value of each node will change the value of the root node after being modified based on the bit operation type.

Code:

  

//It is made by HolseLee on 2nd Nov 2018//CF1010D#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N=1e6+7;int n,dp[N];struct Node { int ls,rs,val,type; }t[N];inline int read(){    char ch=getchar(); int x=0; bool flag=false;    while( ch<‘0‘ || ch>‘9‘ ) {        if( ch==‘-‘ ) flag=true; ch=getchar(); }    while( ch>=‘0‘ && ch<=‘9‘ ) {        x=x*10+ch-‘0‘; ch=getchar(); }    return flag ? -x : x;}void dfs(int x){    if( t[x].type==5 ) return;    switch (t[x].type) {        case 1:            dfs(t[x].ls), dfs(t[x].rs);            t[x].val=t[t[x].ls].val&t[t[x].rs].val;            break;        case 2:            dfs(t[x].ls), dfs(t[x].rs);            t[x].val=t[t[x].ls].val^t[t[x].rs].val;            break;        case 3:            dfs(t[x].ls), dfs(t[x].rs);            t[x].val=t[t[x].ls].val|t[t[x].rs].val;            break;        case 4:            dfs(t[x].ls); t[x].val=(t[t[x].ls].val^1);            break;    }}void DP(int x){    if( t[x].type==5 ) return;    switch (t[x].type) {        case 1:            if( t[t[x].ls].val==1 && t[t[x].rs].val==1 ) {                dp[t[x].ls]=dp[t[x].rs]=1;                DP(t[x].ls), DP(t[x].rs);            } else if( t[t[x].ls].val==1 && t[t[x].rs].val==0 ) {                dp[t[x].rs]=1; DP(t[x].rs);            } else if( t[t[x].ls].val==0 && t[t[x].rs].val==1 ) {                dp[t[x].ls]=1; DP(t[x].ls);            }            break;        case 2:            dp[t[x].ls]=dp[t[x].rs]=1;            DP(t[x].ls), DP(t[x].rs);            break;        case 3:            if( t[t[x].ls].val==1 && t[t[x].rs].val==0 ) {                dp[t[x].ls]=1, DP(t[x].ls);            } else if( t[t[x].ls].val==0 && t[t[x].rs].val==1 ) {                dp[t[x].rs]=1, DP(t[x].rs);            } else if( t[t[x].ls].val==0 && t[t[x].rs].val==0 ) {                dp[t[x].ls]=dp[t[x].rs]=1;                DP(t[x].ls), DP(t[x].rs);            }            break;        case 4:            dp[t[x].ls]=1, DP(t[x].ls);            break;    }}int main(){    n=read(); char s[10];    for(int i=1; i<=n; ++i) {        scanf("%s",s);        switch (s[0]) {            case ‘A‘: t[i].type=1, t[i].ls=read(), t[i].rs=read(); break;            case ‘X‘: t[i].type=2, t[i].ls=read(), t[i].rs=read(); break;            case ‘O‘: t[i].type=3, t[i].ls=read(), t[i].rs=read(); break;            case ‘N‘: t[i].type=4, t[i].ls=read(); break;            case ‘I‘: t[i].type=5, t[i].val=read(); break;        }    }    dfs(1); DP(1);    for(int i=1; i<=n; ++i) {        if( t[i].type!=5 ) continue;        if( dp[i] ) putchar((t[1].val^1)+‘0‘);        else putchar(t[1].val+‘0‘);    }    puts("");    return 0;}

 

Cf1010d Mars Rover [bitwise operation]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.