CSP-S 2020 表达式
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
using namespace std;
struct node{
char op;
int value;
int dp[2];
vector<node*>son;
node(){
dp[0]=0;
dp[1]=0;
value=0;
op='F';
}
};
stack<node*>fuel;
node* date[100000];
char str[100000];
int tot=0,quenum,que;
using namespace std;
int Dfs_solve(node* x){
if(x->op=='F'){
return 0;
}
for(int i=0;i<x->son.size();i++){
Dfs_solve(x->son[i]);
}
if(x->op=='!'){
x->value=(!x->son[0]->value);
}
else if(x->op=='&'){
x->value=(x->son[0]->value & x->son[1]->value);
}
else if(x->op=='|'){
x->value=(x->son[0]->value | x->son[1]->value);
}
return 0;
}
void Dfs_dp(node* x,node* father){
if(father==NULL){
x->dp[0]=0;
x->dp[1]=1;
}
else{
if(father->op=='!'){
x->dp[0]=father->dp[1];
x->dp[1]=father->dp[0];
}
else if(father->op=='&' || father->op=='|'){
node *other;
if(father->son[0]==x){
other=father->son[1];
}
else{
other=father->son[0];
}
if(father->op=='&'){
x->dp[0]=father->dp[0];
x->dp[1]=father->dp[other->value];
}
else{
x->dp[1]=father->dp[1];
x->dp[0]=father->dp[other->value];
}
}
}
for(int i=0;i<x->son.size();i++){
Dfs_dp(x->son[i],x);
}
return;
}
int main(){
while(scanf("%s",&str)){
if(str[0]=='x'){
int num=0;
for(int i=strlen(str)-1;i>0;i--){
num=(num<<3)+(num<<1)+(str[i]^48);
}
date[num]=new node;
fuel.push(date[num]);
}
else if(str[0]=='&' || str[0]=='|'){
node* a;
node* b;
node* c=new node;
a=fuel.top();
fuel.pop();
b=fuel.top();
fuel.pop();
c->op=str[0];
c->son.push_back(a);
c->son.push_back(b);
fuel.push(c);
}
else if(str[0]=='!'){
node* a;
node* b=new node;
a=fuel.top();
fuel.pop();
b->son.push_back(a);
b->op='!';
fuel.push(b);
}
else{
for(int i=strlen(str)-1;i>=0;i--){
tot=(tot<<3)+(tot<<1)+(str[i]^48);
}
for(int i=1;i<=tot;i++){
scanf("%d",&date[i]->value);
}
break;
}
}
node* root=fuel.top();
Dfs_solve(root);
Dfs_dp(root,NULL);
scanf("%d",&quenum);
for(int i=0;i<quenum;i++){
scanf("%d",&que);
printf("%d\n",date[que]->dp[!date[que]->value]);
}
return 0;
}
不知道为什么洛谷提交一直出错
Runing Error
请大家帮一忙,我快崩溃了