题目
正确性已验证:
需要看日志则取消 #define log 的注释
#include <bits/stdc++.h>
using namespace std;
#define int long long
//#define Log //日志开关
bool isLoop = false;
struct Gate{
#ifdef Log
string name;
#endif
vector<Gate* > inputs;
bool output;
string func;
bool calculated;
bool visited;
bool getOutput(){
#ifdef Log
cout << "calculating " << name << endl;
#endif
if(calculated){
//如果算过了就直接返回该结果
#ifdef Log
cout << "calculated! output = " << output << endl;
#endif
return output;
}else if(visited){
//如果该门访问过但是没计算出结果->出现环了
#ifdef Log
cout << "visited! there are a loop" << endl;
#endif
isLoop = true;
return false;
}else{
visited = true;//标记该门已被访问过
output = inputs[0]->getOutput();
if(func == "NOT"){
output = !output;
}else if(func == "AND"){
for (int i=1; i < inputs.size(); i++){
output &= inputs[i]->getOutput(