拓扑排序,另外这道题字符串处理太多了,麻烦死了。这种第三题全都是大模拟。非常麻烦。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include <iostream> #include <algorithm> #include <map> #include <cstdio> #include <cstdlib> #include <string> #include <unordered_map> #include <vector> using namespace std; struct node { string type; int input; string signals[10]; bool value; }; void topoSortDegreeMinus(int id, vector<int> &inDegree, const vector<vector<int>> &edges) { if (inDegree[id] != 0)return; inDegree[id] = -1; vector<int> storage; for (int i = 0; i < edges[id].size(); i++) { inDegree[edges[id][i]]--; if (inDegree[edges[id][i]] == 0) storage.push_back(i); } for (int i = 0; i < storage.size(); i++) { topoSortDegreeMinus(storage[i], inDegree, edges); } } bool checkCircle(int n, unordered_map<string, node> &nodeMap) { //Topological sort vector<int> inDegree; inDegree.resize(n+1); vector<vector<int>> edges; edges.resize(n+1); for (int i = 1; i <= n; i++) { string nodeID = "O" + to_string(i); const node &curNode = nodeMap[nodeID]; for (int j = 0; j < curNode.input; j++) { if (curNode.signals[j][0] == 'O') { inDegree[i]++; int edgeStart = strtol(&curNode.signals[j].c_str()[1], NULL, 10); edges[edgeStart].push_back(i); } } } bool flag = false; do { flag = false; for (int i = 1; i <= n; i++) { if (inDegree[i] == 0) { topoSortDegreeMinus(i, inDegree, edges); flag = true; } } } while (flag); for (int i = 1; i <= n; i++) { if (inDegree[i] > 0) return true; } return false; } bool calculateLogicDFS(unordered_map<string, node> &nodeMap, unordered_map<string, bool> &visited, string ID) { node &curNode = nodeMap[ID]; if (visited[ID])return curNode.value; visited[ID] = true; if (ID[0] == 'I')return curNode.value; if (curNode.type == "NOT") { calculateLogicDFS(nodeMap, visited, curNode.signals[0]); curNode.value=!nodeMap[curNode.signals[0]].value; } else if (curNode.type == "AND") { curNode.value = true; for(int i=0;i<curNode.input;i++){ calculateLogicDFS(nodeMap, visited, curNode.signals[i]); curNode.value &= nodeMap[curNode.signals[i]].value; if(!curNode.value)break; } } else if (curNode.type == "OR") { curNode.value = false; for(int i=0;i<curNode.input;i++){ calculateLogicDFS(nodeMap, visited, curNode.signals[i]); curNode.value |= nodeMap[curNode.signals[i]].value; if(curNode.value)break; } } else if (curNode.type == "XOR") { curNode.value = false; for(int i=0;i<curNode.input;i++){ calculateLogicDFS(nodeMap, visited, curNode.signals[i]); curNode.value ^= nodeMap[curNode.signals[i]].value; } } else if (curNode.type == "NAND") { curNode.value = true; for(int i=0;i<curNode.input;i++){ calculateLogicDFS(nodeMap, visited, curNode.signals[i]); curNode.value &= nodeMap[curNode.signals[i]].value; if(!curNode.value)break; } curNode.value=!curNode.value; } else if (curNode.type == "NOR") { curNode.value = false; for(int i=0;i<curNode.input;i++){ calculateLogicDFS(nodeMap, visited, curNode.signals[i]); curNode.value |= nodeMap[curNode.signals[i]].value; if(curNode.value)break; } curNode.value=!curNode.value; } return curNode.value; } void calculateLogic(unordered_map<string, node> &nodeMap) { unordered_map<string, bool> visited; for(auto iter=nodeMap.begin();iter!=nodeMap.end();iter++){ if(!visited[iter->first]) calculateLogicDFS(nodeMap,visited,iter->first); } } void solve() { // Read Part 1 int m, n; cin >> m >> n; unordered_map<string, node> nodeMap; for (int i = 1; i <= n; i++) { node curNode; cin >> curNode.type >> curNode.input; for (int j = 0; j < curNode.input; j++) { cin >> curNode.signals[j]; } nodeMap["O" + to_string(i)] = curNode; } // Check Circle: Topological Sort bool haveCircle = checkCircle(n, nodeMap); // Read Part 2 int s; cin >> s; vector<vector<int>> inputs; inputs.resize(s); for (int i = 0; i < s; i++) { inputs[i].push_back(m); int t; for (int j = 0; j < m; j++) { cin >> t; inputs[i].push_back(t); } } vector<vector<int>> outputs; outputs.resize(s); for (int i = 0; i < s; i++) { int k; cin >> k; outputs[i].push_back(k); int t; for (int j = 1; j <= k; j++) { cin >> t; outputs[i].push_back(t); } } if (haveCircle) { cout << "LOOP\n"; return; } for (int i = 0; i < s; i++) { for (int j = 1; j <= m; j++) { node tmpNode; tmpNode.value = inputs[i][j]; nodeMap["I" + to_string(j)] = tmpNode; } calculateLogic(nodeMap); for(int j=1;j<=outputs[i][0];j++){ cout<<nodeMap["O"+to_string(outputs[i][j])].value<<" "; } cout<<"\n"; } } int main() { ios::sync_with_stdio(false); int q; cin >> q; while (q--) solve(); } |